DTOs and Data Mapping
Relevant source files
The following files were used as context for generating this wiki page:
- marketplace-backend/app/src/dto/requests.dto.ts
- marketplace-backend/app/src/mappers/customer-to-dto.mapper.ts
- marketplace-backend/app/src/schemas/customer.schema.ts
- marketplace-backend/app/src/repositories/impl/customers.repository.ts
- marketplace-backend/app/src/services/impl/roles.service.ts
- marketplace-backend/app/src/dto/index.ts
- marketplace-backend/app/src/mappers/index.ts
Data Transfer Objects (DTOs) and Mappers facilitate structured data exchange between the application layers. DTOs define the shape of data for API requests and responses, while Mappers transform internal database entities into public-facing objects.
The system ensures that sensitive internal data, such as hashed passwords, remains isolated from the client-side responses. This architecture separates the persistence concerns of Mongoose schemas from the interface requirements of the REST API.
Data Transfer Objects (DTOs)
DTOs act as contracts for incoming and outgoing data. The backend uses specific DTOs for registration, role creation, and customer profile delivery.
Key DTO Definitions
- RegisterRequestDto: Captures input for new customer accounts. It includes the plain-text password and initial profile details.
- CustomerDto: Defines the public profile of a customer. It excludes the
passwordfield andidfield for security. - RoleDto: Manages the structure for creating new user roles.
| DTO Name | Purpose | Key Fields |
|---|---|---|
RegisterRequestDto | Account Registration | userName, email, password, firstName, age |
CustomerDto | Data Retrieval | userName, firstName, email, age, role |
RoleDto | Role Creation | id, type |
Sources: marketplace-backend/app/src/dto/index.ts:1-3, marketplace-backend/app/src/mappers/customer-to-dto.mapper.ts:4-13, marketplace-backend/app/src/repositories/impl/customers.repository.ts:18-32
Data Mapping Logic
The mapping layer converts ICustomer documents into CustomerDto objects. This transformation occurs before data leaves the service layer to ensure the client receives only authorized fields.
Customer Mapping Process
The customerToDtoMapper function extracts specific properties from the Customer document. It uses object destructuring to select userName, firstName, email, age, and role.
The diagram shows the transformation flow from a database entity to a filtered DTO.
Sources: marketplace-backend/app/src/mappers/customer-to-dto.mapper.ts:4-14
Data Flow and Persistence
Repositories use DTOs to populate database models. During customer creation, the repository transforms the RegisterRequestDto into a Customer entity after performing necessary operations like password hashing.
Registration Flow Logic
- The repository receives a
RegisterRequestDto. - It generates a unique UUID for the new customer.
- It hashes the plain-text password using
bcryptwith a salt factor of 10. - It assigns a default
UserRoleretrieved from theRolesService. - It sets the age to 18 if the input is missing.
- The repository saves the transformed object into the Mongoose model.
The sequence diagram illustrates the internal processing of a DTO before database persistence.
Sources: marketplace-backend/app/src/repositories/impl/customers.repository.ts:18-32, marketplace-backend/app/src/schemas/customer.schema.ts:12-32
Schema vs DTO Structure
The Mongoose schema contains internal metadata and sensitive information that DTOs omit.
| Field | Customer Schema | CustomerDto | Description |
|---|---|---|---|
id | Required (String) | No | Unique internal identifier |
userName | Required (String) | Yes | Unique public username |
password | Required (String) | No | Hashed user password |
email | Required (String) | Yes | Unique contact email |
age | Optional (Number) | Yes | User age (Defaults to 18) |
role | ObjectId Ref | Yes | Associated user role object |
Sources: marketplace-backend/app/src/schemas/customer.schema.ts:12-32, marketplace-backend/app/src/mappers/customer-to-dto.mapper.ts:4-13
Role Management DTOs
The RolesService uses the RoleDto for creating new system roles. The service generates a unique UUID for each role before saving the data to the persistence layer.
Sources: marketplace-backend/app/src/services/impl/roles.service.ts:16-25, marketplace-backend/app/src/dto/role.dto.ts
DTOs and Mappers maintain a clean separation between the database and the external API. This structure provides a secure method for handling user registration and profile management.