Skip to content

User Model

UserModel Documentation

The UserModel is a Mongoose schema designed to represent user entities in a MongoDB database for a web application. It outlines the structure, constraints, and characteristics of user data stored in the database.

Schema Fields:

  1. first_name: (String) Stores the user’s first name. This field is mandatory.
  2. last_name: (String) Stores the user’s last name. This field is mandatory.
  3. email: (String) The user’s email address, unique to each user. This field is mandatory and must be unique across all users.
  4. password: (String) The user’s password. This field is mandatory. Ensure hashed passwords are stored, not plain text.
  5. is_admin: (Boolean) Indicates if the user has administrative privileges. Defaults to false.
  6. company: (String) The name of the company the user is associated with. This field is mandatory.
  7. companyAddress: (String) The address of the user’s company. This field is mandatory.
  8. facToken: (String) A token used for recurring payments. This field is optional and should be handled securely.
  9. phone: (String) The user’s phone number. This field is mandatory.

Model Behavior:

  • Validation: The schema includes validation rules to ensure necessary data is provided and in the correct format (e.g., unique email).
  • Security: Passwords should be hashed for security purposes, ideally within a pre-save hook in the schema.
  • Data Integrity: Unique constraints on the email field ensure no duplicate records.

Usage:

  • The UserModel is used to interact with the ‘users’ collection in the MongoDB database, providing a structured approach to handle user data effectively.

Import and Usage in Code:

const { UserModel } = require("path_to_user_model");
// Usage for creating, reading, updating, deleting user records

This documentation provides a comprehensive guide to understanding and using the UserModel in the application’s backend, ensuring consistent and secure handling of user data.