Backend (Core) Architecture
Relevant source files
The following files were used as context for generating this wiki page:
The Ever Demand backend core is a real-time, headless commerce API server. It is built on a modern, full-stack TypeScript foundation, utilizing Node.js and the NestJS framework. The architecture is designed to be reactive and event-driven, providing rich GraphQL, REST, and WebSocket APIs to support various client applications, including web and mobile storefronts, admin panels, and merchant apps.
The system leverages MongoDB as its primary database, with an abstraction layer provided by a custom micro-framework called Pyro. There is also an ongoing effort to support multiple SQL and NoSQL databases through TypeORM. The architecture emphasizes modularity, with features organized into distinct NestJS modules, and employs patterns like CQRS for a clean separation of concerns.
Sources: packages/core/README.md, README.md, packages/core/src/app.module.ts
Technology Stack
The backend is built with a range of modern technologies to ensure scalability, reliability, and real-time capabilities.
| Technology/Library | Description | Source |
|---|---|---|
| Node.js | The JavaScript runtime environment for executing the server-side code. | packages/core/README.md |
| NestJS | A progressive Node.js framework for building server-side applications. | packages/core/README.md |
| TypeScript | The main programming language, providing static typing. | packages/core/README.md |
| MongoDB | The primary NoSQL database used for data persistence. | packages/core/README.md |
| Mongoose | An Object Data Modeling (ODM) library for MongoDB and Node.js. | packages/core/README.md |
| TypeORM | An Object-Relational Mapper (ORM) being integrated to support other databases (e.g., PostgreSQL, MySQL). | README.md:43-45 |
| GraphQL | A query language for APIs, implemented via @nestjs/graphql. | packages/core/src/app.module.ts:186-194 |
| Socket.IO | A library for real-time, bidirectional, and event-based communication. | packages/core/README.md |
| RxJS | A library for reactive programming using Observables, used heavily across the platform. | packages/core/README.md |
| InversifyJS | A lightweight inversion of control (IoC) container, used alongside NestJS's DI. | packages/core/README.md |
Application Bootstrap
The application entry point is managed by nest-bootstrap.ts, which sets up and launches the NestJS application instance. The process involves creating a Nest application from the root ApplicationModule, configuring middleware, and starting the server to listen for incoming connections.
The following diagram illustrates the bootstrap sequence.
This diagram shows the startup flow of the NestJS application.
Sources: packages/core/src/nest-bootstrap.ts
Module Architecture
The core of the backend is the ApplicationModule, which serves as the root module for the NestJS application. It imports and orchestrates all other feature modules, establishing the application's dependency graph and overall structure.
Sources: packages/core/src/app.module.ts
Key Imported Modules
The ApplicationModule aggregates functionality from various domain-specific modules:
- Core & Config:
CqrsModule,ConfigModule,AuthModule - Data & Services:
DataModule,ServicesModule,TypeOrmModule - GraphQL Feature Modules:
UsersModule,OrdersModule,ProductsModule,WarehousesModule,CarriersModule, etc. - Real-time Subscriptions:
SubscriptionsModule
The diagram below shows a high-level view of the major modules imported by ApplicationModule.
CQRS Pattern
The application utilizes the Command Query Responsibility Segregation (CQRS) pattern via @nestjs/cqrs. The ApplicationModule registers command and event handlers.
- Command Handlers: Defined in
CommandHandlersarray (e.g.,GetAboutUsHandler). - Event Handlers: Defined in
EventHandlersarray.
These handlers are registered with the CommandBus and EventBus on module initialization.
Sources: packages/core/src/app.module.ts:70-73, packages/core/src/app.module.ts:215-219
Database Integration
The platform is designed to work with MongoDB and is expanding to support other databases via TypeORM.
MongoDB and Pyro Micro-Framework
The primary database is MongoDB, accessed via Mongoose. The project includes a custom micro-framework named Pyro DB, which is a TypeScript wrapper around Mongoose to simplify model definition and data access.
Pyro DB Models
Models are defined as classes with decorators.
@ModelName('...'): Marks a class as a Mongoose model.@Schema({...}): Defines a field's schema properties.@Types.String(),@Types.Number(), etc.: Shorthand for primitive types.@Types.Ref(Model, ...): Defines a reference to another model.
Sources: packages/core/README.md
Pyro DBService
Pyro includes an abstract DBService class that provides a generic data access layer with standard CRUD operations. Services for specific models (e.g., WarehousesService) extend DBService to inherit this functionality.
The IDBService interface defines the contract for this service.
This diagram shows the structure of the generic database service interface.
Sources: packages/core/README.md, packages/core/src/@pyro/db-server/db-service.ts
TypeORM Integration
To support a wider range of databases, TypeORM is integrated into the application. The configuration is defined in app.module.ts and loaded via TypeOrmModule.forRoot().
- Connection Settings: The configuration specifies the database type (
mongodbfor now), URL, credentials, and SSL options, all sourced from environment variables. - Entities: Entities are collected from
ServicesApp.getEntities()and registered with TypeORM. - Repositories:
TypeOrmModule.forFeature(entities)makes repositories for each entity injectable throughout the NestJS application.
Sources: packages/core/src/app.module.ts:121-143, packages/core/src/app.module.ts:172-177, README.md:43-45
API Layer
The backend exposes its functionality through multiple API paradigms: GraphQL, WebSockets (via Pyro IO), and REST.
GraphQL API
The GraphQL API is configured using @nestjs/graphql.
- Schema Definition: Schemas are loaded from
.graphqlfiles located throughout the project (typePaths: ['./**/*.graphql']). - Playground: An interactive GraphQL Playground is enabled in non-production environments for API exploration and testing.
- Subscriptions: Real-time updates are handled via GraphQL subscriptions, with
installSubscriptionHandlers: true. TheSubscriptionsModulemanages the WebSocket server for subscriptions.
Sources: packages/core/src/app.module.ts:186-194
Real-time API (Pyro IO)
For more direct real-time communication, the custom Pyro IO micro-framework is used. It allows service methods to be exposed directly over a Socket.IO-based WebSocket API using decorators.
@routerName('...'): Marks a service class as a WebSocket router.@observableListener(): Exposes a method that returns anObservable. The client receives a stream of data.@asyncListener(): Exposes a method that returns aPromise. The client receives a single response.
The RoutersManager class is responsible for discovering these decorated services and binding them to Socket.IO events.
The sequence diagram below shows a client calling a method on a Pyro IO-enabled service.