Database Schema
Relevant source files
The following files were used as context for generating this wiki page:
The Ever Demand platform's database schema is designed for flexibility, supporting both NoSQL and SQL databases. The primary implementation uses MongoDB with the Mongoose ODM, leveraging a custom micro-framework called Pyro for model definitions. Concurrently, there is an ongoing effort to integrate SQL databases (like PostgreSQL, MySQL, etc.) using the TypeORM library. This dual-ORM strategy allows the same entity models to be used for different database backends.
The core of the schema revolves around key e-commerce concepts such as Warehouses (Merchants/Stores), Products, Orders, Customers (Users), and Carriers. These entities are defined in the @ever-platform/common package and are used by the core backend service. The backend, built with NestJS, configures and manages database connections, making entities available to the rest of the application through services and GraphQL resolvers.
Sources: README.md, packages/core/README.md, packages/core/src/app.module.ts:133-145
Technology and Architecture
The platform employs a hybrid approach to data persistence, allowing developers to choose between MongoDB and various SQL databases.
Sources: README.md, packages/core/src/app.module.ts:133-145
Dual ORM Strategy
Entity classes are decorated for both Mongoose (via the custom Pyro framework) and TypeORM. This allows a single entity definition to map to either a MongoDB collection or a SQL table.
- Mongoose (Pyro): Uses decorators like
@ModelName,@Schema, and@Types. - TypeORM: Uses standard decorators like
@Entityand@Column.
This dual-decorator pattern is evident in most entity files. For example, the Warehouse entity is defined for both ORMs simultaneously.
// packages/common/src/entities/Warehouse.ts
@ModelName('Warehouse')
@Entity({ name: 'warehouses' })
class Warehouse extends DBObject<IWarehouse, IWarehouseCreateObject>
implements IWarehouse {
// ...
@Types.String()
@Column()
name: string;
@Schema({ type: String, unique: true })
@Column()
username: string;
// ...
}
Sources: packages/common/src/entities/Warehouse.ts:25-30, 110-115, packages/core/README.md
Database Connection
The main backend application module, ApplicationModule, is responsible for configuring the database connection. It uses TypeOrmModule.forRoot() to establish a connection based on environment variables. The configuration supports SSL connections and specifies the list of entities to be loaded by TypeORM.
The primary entities loaded by the application are:
AdminCarrierDeviceInviteInviteRequestOrderProductProductsCategoryUserWarehousePromotion
Sources: packages/core/src/app.module.ts:133-145, packages/core/src/services/services.app.ts:93-107
Entity Relationship Diagram
This diagram illustrates the high-level relationships between the core entities in the Ever Demand database schema.
This diagram is a conceptual representation based on the entity definitions.
Sources: packages/common/src/entities/Order.ts, packages/common/src/entities/Warehouse.ts, packages/common/src/entities/Product.ts
Core Entities
The following sections detail the schema for the most critical entities in the system.
Warehouse
The Warehouse entity represents a store, merchant, or physical location where products are stored and from which orders are fulfilled. It contains information about the store's identity, location, contact details, and operational settings.
Sources: packages/common/src/entities/Warehouse.ts
The class diagram below shows the key properties of the Warehouse entity.
Fields
| Field | Type | Description | Decorators |
|---|---|---|---|
name | string | The name of the store/merchant. | @Types.String(), @Column() |
logo | string | The URL of the store's brand logo. | @Schema(), @Column() |
username | string | Default administrator username for the store. | @Schema(), @Column() |
hash | string | Hashed password for the store administrator. | @Schema(), @Column() |
isActive | boolean | Indicates if the warehouse is currently operational. | @Types.Boolean(false), @Column() |
isPaymentEnabled | boolean | If true, online payments are allowed. | @Types.Boolean(true), @Column() |
isCashPaymentEnabled | boolean | If true, cash on delivery is allowed. | @Types.Boolean(true), @Column() |
geoLocation | GeoLocation | The warehouse's current location and address. | @Schema(getSchema(GeoLocation)) |
deliveryAreas | object | GeoJSON object defining delivery zones. | @Schema({ type: Object }) |
products | WarehouseProduct[] | Products available for purchase at this warehouse. | @Schema([getSchema(WarehouseProduct)]) |
contactEmail | string | Primary contact email for the store. | @Schema(), @Column() |
contactPhone | string | Primary contact phone for the store. | @Schema(), @Column() |
forwardOrdersUsing | ForwardOrdersMethod[] | Preferred methods for receiving order notifications (e.g., email, phone). | @Schema([Number]), @Column() |
carriersIds | string[] | IDs of carriers assigned to this store. | @Schema([String]) |
paymentGateways | PaymentGateway[] | Configured payment gateways (e.g., Stripe, PayPal). | @Schema([getSchema(PaymentGateway)]) |
Sources: packages/common/src/entities/Warehouse.ts
Product
The Product entity represents a global catalog item. It contains generic information about a product, such as its name, description, and images. Specific pricing and stock information are managed at the warehouse level via the WarehouseProduct entity.
Sources: packages/common/src/entities/Product.ts
Fields
| Field | Type | Description | Decorators |
|---|---|---|---|
title | string | The name of the product. | @Schema(), @Column() |
description | string | A short description of the product. | @Schema(), @Column() |
images | string[] | An array of URLs for product images. | @Schema([String]) |
details | string | Detailed information about the product. | @Schema(), @Column() |
isArchived | boolean | If true, the product is archived and not in active use. | @Types.Boolean(false), @Column() |
isAvailable | boolean | Indicates if the product is generally available for stores to add. | @Types.Boolean(true), @Column() |
categories | ProductCategory[] | Categories the product belongs to. | @Schema([getSchema(ProductCategory)]) |
Sources: packages/common/src/entities/Product.ts
Order
The Order entity is central to the platform, tracking a customer's purchase from creation to delivery. It links together the customer (User), the Warehouse fulfilling the order, the Products ordered, and the Carrier responsible for delivery.
Sources: packages/common/src/entities/Order.ts
The class diagram below shows the key properties and relationships of the Order entity.
Fields
| Field | Type | Description | Decorators |
|---|---|---|---|
orderNumber | string | A unique, human-readable identifier for the order. | @Schema(), @Column() |
status | number (enum) | The current status of the order from the warehouse's perspective (e.g., Confirmed, Processing). | @Schema(), @Column() |
carrierStatus | number (enum) | The current status of the order from the carrier's perspective (e.g., Picked Up, In Delivery). | @Schema(), @Column() |
isPaid | boolean | Indicates whether the order has been paid for. | @Types.Boolean(false), @Column() |
user | User | The customer who placed the order. | @Types.Ref(User, { required: true }) |
warehouse | Warehouse | The warehouse fulfilling the order. | @Types.Ref(Warehouse, { required: true }) |
carrier | Carrier | The carrier assigned to deliver the order. | @Types.Ref(Carrier, { required: false }) |
products | IOrderProduct[] | An array of products included in the order, with quantity and price. | @Schema([getSchema(OrderProduct)]) |
payment | IPayment[] | Payment details, including gateway and transaction info. | @Schema([getSchema(Payment)]) |
delivery | IOrderDelivery | Delivery information, including address and time estimates. | @Schema(getSchema(OrderDelivery)) |
isTakeAway | boolean | If true, the order is for customer pickup, not delivery. | @Types.Boolean(false), @Column() |
notes | string | Any special notes or instructions from the customer. | @Schema(), @Column() |
Sources: packages/common/src/entities/Order.ts
Conclusion
The database schema for Ever Demand is built to be robust and adaptable. By defining entities with both Mongoose and TypeORM decorators, the platform maintains a flexible architecture that can run on a NoSQL database like MongoDB for rapid development and scalability, while also being prepared for migration to traditional SQL databases. The core entities (Warehouse, Product, Order, User) are well-structured to support the complex logistics of an on-demand and multi-vendor marketplace, forming a solid foundation for the platform's features.
Sources: README.md, packages/core/README.md, packages/common/src/entities/Warehouse.ts