Backend Data Access Layer
Relevant source files
The following files were used as context for generating this wiki page:
- marketplace-backend/app/src/repositories/impl/customers.repository.ts
- marketplace-backend/app/src/repositories/abstract-customers.repository.ts
- marketplace-backend/app/src/schemas/customer.schema.ts
- marketplace-backend/app/src/modules/customers/customers.module.ts
- marketplace-backend/app/src/app.module.ts
The Backend Data Access Layer (DAL) manages the persistence and retrieval of application data. It provides an abstraction between the business logic and the MongoDB database using the Mongoose Object Data Modeling (ODM) library within the NestJS framework.
Architecture Overview
The DAL follows the Repository Pattern to decouple the data access logic from the service layer. It consists of abstract repository definitions, concrete implementations, and Mongoose schemas that define the data structure.
Component Interaction
The following diagram illustrates how the DAL components interact with the database and the rest of the application.
The DAL uses dependency injection to provide concrete repository implementations to the service layer.
Sources: marketplace-backend/app/src/modules/customers/customers.module.ts:1-42, marketplace-backend/app/src/repositories/impl/customers.repository.ts:10-15
Data Models and Schemas
Schemas define the structure of documents stored in MongoDB. The Customer schema includes fields for identification, authentication, and profile information.
Customer Schema Definition
The Customer class uses decorators to define Mongoose properties and constraints.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | string | Unique, Required | Internal UUID for the customer |
userName | string | Unique, Required | Unique display name |
email | string | Unique, Required | Contact and login email |
password | string | Required | Hashed credentials |
firstName | string | Required | Customer's first name |
age | number | Customer's age (defaults to 18) | |
role | ObjectId | Ref: 'Role' | Reference to the Role schema |
Sources: marketplace-backend/app/src/schemas/customer.schema.ts:12-42, marketplace-backend/app/src/repositories/impl/customers.repository.ts:25-27
Repository Implementation
Repositories encapsulate the logic required to access data sources. The CustomersRepository implements the AbstractCustomersRepository and interacts with the Customer model.
Key Operations
The repository performs CRUD operations and complex queries:
- createNewCustomer: Generates a unique
idusinguuidv4, hashes the password, assigns a default role, and saves the new customer. - findCustomerByUserName: Retrieves a single customer document matching the provided username.
- findCustomerById: Retrieves a customer by
idand populates the associatedrolefield. - findAllCustomers: Returns all customer documents with populated roles.
The repository uses bcrypt with a salt factor of 10 for password hashing.
Sources: marketplace-backend/app/src/repositories/impl/customers.repository.ts:17-57
Dependency Injection and Configuration
The MongooseModule facilitates the connection to the database and the registration of schemas.
- Global Connection: The
AppModuleinitializes the connection using themongoURIenvironment variable. - Feature Registration: The
CustomersModuleregisters theCustomerandRoleschemas for use within its scope. - Provider Mapping: The module maps the
AbstractCustomersRepositorytoken to theCustomersRepositoryimplementation.
@Module({
imports: [
MongooseModule.forFeature([
{ name: Customer.name, schema: CustomerSchema },
{ name: Role.name, schema: RoleSchema },
]),
],
providers: [
{ provide: AbstractCustomersRepository, useClass: CustomersRepository },
],
})
export class CustomersModule {}
Sources: marketplace-backend/app/src/app.module.ts:20-23, marketplace-backend/app/src/modules/customers/customers.module.ts:10-33
Conclusion
The Backend Data Access Layer provides a structured and type-safe approach to database interactions. By utilizing the Repository Pattern and Mongoose schemas, the system ensures data integrity and maintainability while abstracting the complexities of MongoDB operations from the application's business logic.