Database Schema
Relevant source files
The following files were used as context for generating this wiki page:
- marketplace-backend/app/src/schemas/customer.schema.ts
- marketplace-backend/app/src/schemas/role.schema.ts
- marketplace-backend/app/src/repositories/impl/customers.repository.ts
- marketplace-backend/app/src/modules/customers/customers.module.ts
- marketplace-backend/app/src/modules/role/role.module.ts
- marketplace-backend/app/src/schemas/index.ts
The database schema defines the structure, relationships, and data constraints for the marketplace application. It utilizes Mongoose to map TypeScript classes to MongoDB collections, ensuring data consistency across the backend services. The primary entities focus on user management, specifically covering customer profiles and their associated access roles.
Sources: marketplace-backend/app/src/schemas/customer.schema.ts, marketplace-backend/app/src/schemas/role.schema.ts
Entity Relationship Model
The marketplace backend implements a relational-style document model using Mongoose references. The Customer entity maintains a reference to the Role entity, enabling role-based access control and profile categorization.
The diagram above illustrates the relationship where a Customer document contains a reference to a Role document via an ObjectId.
Sources: marketplace-backend/app/src/schemas/customer.schema.ts:31, marketplace-backend/app/src/schemas/role.schema.ts
Schema Definitions
Customer Schema
The Customer schema stores user-specific information and authentication credentials. It enforces uniqueness on the id, userName, and email fields to prevent duplicate accounts.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | String | Unique, Required | The unique identifier for the customer (UUID). |
userName | String | Unique, Required | The handle used for login and identification. |
email | String | Unique, Required | The customer's contact and login email. |
password | String | Required | The hashed authentication credential. |
firstName | String | Required | The customer's given name. |
age | Number | Optional | The age of the customer; defaults to 18 during registration if omitted. |
role | ObjectId | Reference | Reference to a document in the Role collection. |
Sources: marketplace-backend/app/src/schemas/customer.schema.ts:13-32, marketplace-backend/app/src/repositories/impl/customers.repository.ts:25-27
Role Schema
The Role schema defines the authorization levels available within the system.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | String | Unique, Required | Unique identifier for the role. |
type | String | Unique, Required | The category name of the role (e.g., "Default"). |
description | String | Optional | A textual description of the role's permissions. |
Sources: marketplace-backend/app/src/schemas/role.schema.ts:10-21
Implementation Architecture
The application uses the MongooseModule to register these schemas within specific feature modules. This allows the CustomersRepository to inject and interact with the database models.
This flow shows how schemas are imported into modules and subsequently utilized by repositories for data operations.
Sources: marketplace-backend/app/src/modules/customers/customers.module.ts:10-21, marketplace-backend/app/src/repositories/impl/customers.repository.ts:17-20
Data Access Logic
The CustomersRepository handles the persistence layer, including object population. When retrieving customer data, the repository uses the .populate('role', 'type') method to join the Role information into the Customer document.
// marketplace-backend/app/src/repositories/impl/customers.repository.ts:39-44
async findCustomerById(customerId: string): Promise<Customer | null> {
return this._customerModel
.findOne({id: customerId})
.populate('role', 'type')
.exec();
}
Sources: marketplace-backend/app/src/repositories/impl/customers.repository.ts:39-44, marketplace-backend/app/src/repositories/impl/customers.repository.ts:46-51
Summary
The Database Schema is built on MongoDB using Mongoose as the ODM. It consists of two primary collections: customers and roles. The system enforces data integrity through unique constraints and handles complex data retrieval through Mongoose population, ensuring that user profiles are always associated with valid system roles.