Origin Backend - @energyweb/origin-backend
Overview
The Origin Backend is a NestJS application that provides services for user and organization authorization and management. The Backend application can be used in conjunction with one, several or all of the Origin SDKs to provide integrated user management and authorization.
The below gives an overview the of the package architecture, however the NestJS documentation provides further detail into the fundamentals of NestJS Architecture that may help to understand the elements of this application:
- Custom Providers as Services
- Dependency Injection
- CQRS (Command and Query Responsibility Segregation)
- Modules
- NestJS TypeORM Integration
- TypeORM repository design pattern
Origin Backend Architecture
The Origin Backend package is broken down into seven NestJS modules:
Each module contains code relevant for a specific feature. In general, each NestJS module has:
- A controller that manages requests and responses to the client
- A .entity file that maps an entity to the database repository
- A .service file that provides methods to fetch and transform data
- Data Transfer Object (DTO) file(s) that provide Data Transfer Objects, which are representations of the data that are exposed to the endpoint consumer
- A module class that is used by NestJS to structure the application
Persistence
The Origin Backend uses PostgreSQL for persistence with TypeORM as a database integration library. The application creates a repository for each entity. Entities are defined in the .entity.ts file in each module, and are marked with the @Entity decorator. (You can read more about entities in the TypeORM documentation here).
@Entity('email_confirmation')
export class EmailConfirmation extends ExtendedBaseEntity implements IEmailConfirmation {
@PrimaryGeneratedColumn()
id: number;
@OneToOne(() => User)
@JoinColumn()
user: User;
@Column()
@IsBoolean()
confirmed: boolean;
@Column()
@IsString()
token: string;
@Column()
@IsInt()
@Min(0)
expiryTimestamp: number;
}
Repositories are injected into services or command handlers so they are available to use in methods:
@Injectable()
export class EmailConfirmationService {
constructor(
@InjectRepository(EmailConfirmation)
private readonly repository: Repository<EmailConfirmation>,
private readonly eventBus: EventBus
) {}
Modules
admin
The admin module's controller uses the user service to fetch and update user information.
configuration
The configuration module fetches and updates the platform's configuration values. These values are used to populate the platform's market options.
email-confirmation
The email-confirmation module provides methods to manage the email confirmation process. When users register to join the platform, they must confirm their email. Confirmations are persisted in the Email Confirmation repository.
file
The email-confirmation module provides methods to fetch, update and store files. Files are uploaded to the system when registering an organization, a device or requesting certification. Files are persisted in the File repository.
invitation
The invitation module provides methods to manage invitations for users to join organizations. Invitations are persisted in the Invitation repository.
organization
The invitation module provides methods to manage fetch, create and update Organization data, including Organization Blockchain Account Addresses. Organizations are persisted in the Organization Repository.
user
The user module provides methods to manage fetch, create and update users. Users are persisted in the User Repository.
Origin Backend-Core Package - @energyweb/origin-backend-core
The Origin Backend Core package provides the interfaces, types and enums for Origin Backend.