DEV Community

Cover image for ECOMMERCE Website Using MERN Part-3.1 ( Product Model )
Bikramjeet Sarmah
Bikramjeet Sarmah

Posted on

ECOMMERCE Website Using MERN Part-3.1 ( Product Model )

Product APIs - productModel

In this blog we will be making the productModel for your ProductApi's.

Step-1 Lets make these folders inside of the backend directory

  • models here we will be making the models for our APIs
  • routes here we will be making the routes for our APIs
  • controllers here we will be making the controllers for our APIs

Step-2 Now lets make a file named productModel.js inside of the models directory.

Here we will be making the Schema for the products.

Step-3 Now in the productModel.js file we will be writing the following codes

const mongoose = require("mongoose");

const productSchema = mongoose.Schema({
  name: {
    type: String,
    required: [true, "Please Enter Product Name"],
    trim: true,
  },
  description: {
    type: String,
    required: [true, "Please Enter Product Description"],
  },
  price: {
    type: Number,
    required: [true, "Please Enter Product Price"],
    maxLength: [8, "Price cannot exceed 8 characters"],
  },
  images: [
    {
      public_id: {
        type: String,
        required: true,
      },
      url: {
        type: String,
        required: true,
      },
    },
  ],
 rating: {
    type: Number,
    default: 0,
  },
  category: {
    type: String,
    required: [true, "Please Enter Product Category"],
  },
  stock: {
    type: Number,
    required: [true, "Please Enter product Stock"],
    maxLength: [4, "Stock cannot exceed 4 characters"],
    default: 1,
  },
  numOfReviews: {
    type: Number,
    default: 0,
  },
  reviews: [
    {
      name: {
        type: String,
        required: true,
      },
      rating: {
        type: Number,
        required: true,
      },
      comment: {
        type: String,
        required: true,
      },
    },
  ],
  createdAt: {
    type: Date,
    default: Date.now,
  },
});

module.exports = mongoose.model("Product", productSchema);
Enter fullscreen mode Exit fullscreen mode

Here at first we are importing the mongoose package and then we created mongoose schema by writing mongoose.Schema().

This takes an object with the properties we want to have in our schema. So lets see what these properties does.

At first we have the name& descriptionwhich takes Stringsas their type and both of values must be entered and for that we set required as true. We have also set trim as true, by this unnecessary blank spaces will be removed.
Name & desc

Its the same for the price as well just we changed the type to Number, and have set its maxLength to 8 digits.
Price

After that we made a property for the product images. This property will be an Array of Objects so that we can map through them later. There we gave public_id & url as properties as we will be storing the Images in Cloudinary and then store the links of the images in our MongoDb server.
Images

After that we made properties like rating which will be 0 by default, stock which is also be Number and have can have maximum of 4 digits, and if stock is not entered then it will be set to 1 by default.
Rating & Stock

category is the same as the name property & numOfReviews is same as rating.
Category & numOfReviews

The reviews property is also an Array of Objects as there may be multiple reviews of a single product. But here we name 3 properties, name, rating & comment and all of them have required as true as if a person writes a review he/she have to give a rating as well as a comment
Reviews

At last we made a property called createdAt so that we can know when this product was created. It's type is set to Date and by default it will be the current date & time when the product will be created.
Created At

And finally we export the mongoose model as Product.
Export

So the next step will be making the Product Controllers

Discussion (0)