Overview
The User model represents users in the construction backend system. It stores authentication credentials, personal information, and user role classification. The schema includes unique constraints on document and email fields to ensure data integrity.Schema Definition
The User model is built using Mongoose and includes pagination support through themongoose-paginate-v2 plugin.
Source Location
src/models/user.js
Fields
User’s identification document number. Must be unique across all users.Constraints:
- Required: Yes
- Unique: Yes
User’s email address used for authentication and communication.Constraints:
- Required: Yes
- Unique: Yes
Encrypted password for user authentication. Should be hashed before storage.Constraints:
- Required: Yes
User’s first name.Constraints:
- Required: Yes
User’s last name or surname.Constraints:
- Required: Yes
User’s mobile phone number for contact purposes.Constraints:
- Required: Yes
Classification or role of the user in the system (e.g., admin, client, contractor).Constraints:
- Required: Yes
Plugins
mongoose-paginate-v2
The User schema includes themongoose-paginate-v2 plugin, which adds pagination capabilities to queries. This allows for efficient retrieval of large user datasets with built-in pagination methods.
Enabled Methods:
User.paginate(query, options)- Paginated query with customizable page size and sorting
Example Document
Validation Rules
Usage Examples
Creating a New User
Querying with Pagination
Related Models
- Quotation: Users can be associated with quotations as creators or recipients
Notes
Security Consideration: The password field should always be hashed using a secure algorithm (e.g., bcrypt) before storing in the database. Never store plain text passwords.
Email Validation: Consider adding email format validation using Mongoose validators or a validation library to ensure valid email addresses are stored.