UserSchema Demystified: A Guide with Examples for Structuring User Data with Mongoose.

What is a schema?

In web development, a schema is a blueprint or framework that defines the organization, presentation, and attributes of data. It functions as a model or template for data storage and retrieval, assisting in the maintenance of consistency and correctness in data management. A schema is very significant when dealing with databases since it allows you to determine the structure of the data that will be kept in the database.

In our example, we will go through how to set up a schema in Mongoose. Mongoose heavily uses schemas to specify the structure of the data stored in a MongoDB database. Mongoose schemas enable developers to clearly and consistently specify the structure of data, ensuring that the data is correctly formatted and tested before it is put in the database.

What is UserSchema?

Based on the definition of schema above, we can conclude that userSchema is the blueprint of the structure and properties of user data in a web application. It often contains attributes such as username, email address, password, first name, last name, and any other relevant user data required by an application.

Tutorial to create an userSchema with mongoose:

In this example, I will also show you how you can use the validator.js library to validate the email when it is provided by the client. This schema has the following properties: name, email, password, and location. A basic explanation of the properties is available below the code.

import mongoose from "mongoose";
import validator from "validator";

//defines a new schema for a "User" collection in a MongoDB database using the Mongoose library.
//UserSchema is the schema that we have defined for the "User" collection
const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, "Please provide name"],
    minlength: 3,
    maxlength: 20,
    trim: true,
  },
  email: {
    type: String,
    required: [true, "Please provide email"],
    validate: {
      validator: validator.isEmail,
      message: "Please provide a valid email",
    },
    unique: true,
  },
  password: {
    type: String,
    required: [true, "Please provide the password"],
    minlength: 6,
  },
  location: {
    type: String,
    trim: true,
    maxlength: 20,
    default: "New York",
  },
});

export default mongoose.model("User", UserSchema);
//exports a Mongoose model for a "User" collection in a MongoDB database.

This will generate a Mongoose model for the "User" collection, which we can then use to execute CRUD actions.

Explanation of properties used in the schema:
required: It indicates that a field is required and must have a value. If a document is saved without a value for a required field, Mongoose will throw a validation error. In the above example, required is used with the name, email, and password properties, so when a user registers, they need to provide these values.

trim: This attribute is used to eliminate whitespace from the beginning and end of a string value. If this is true, Mongoose will remove whitespace from the start and end of the value before storing it in the database.

unique: This attribute specifies that a field must have a unique value inside the collection. If this field is set to true, Mongoose will verify that no two documents in the collection have the same value. In the above example, unique is used with the email property, so that no two users have the same email.

default: This attribute is used to set a field's default value. Mongoose will use the default value if a document is saved without a value for this field. In the above example, default is used with the location property, so when a user registers and does not provide any value about their location it will use the default value of "New York" as their location.

validate: To validate, we use the validator.js library to check if the string is an email.

type: This attribute specifies the data type of the field. String is our example.

minLength: This attribute specifies the minimum length(shortest) of the string that can be stored in the field.

maxLength: This attribute specifies the maximum length(longest) of the string that can be stored in the field.

Creating a customized UserSchema for your web application based on your preferences can unlock a plethora of advantages, such as ensuring consistent data structure, reinforcing application security, streamlining code organization, and simplifying data retrieval.

I hope this article helped you in case you had any doubts.

Happy hacking!