Build an E-Commerce Website with MERN Stack - Part 2 (Designing the Models)
Tags: react, node, mongodb, webdev
Published: 2021-01-23 15:37:55

Hello friends! So, this is the second part of the MERN Stack series we have recently started. In the first part, we all learnt how to set up the project and had explanations about various things we are going to use in the project.

So, after completing the first part, we are well acquainted with the process of setting up our Express App and we understood what dependencies we will use in our project and what purpose would they serve.

Now, in the second part, we will start building models for our application. We are using MongoDB as the database to store all our data. We will use Mongoose to connect to the MongoDB database and it would make our work easier to build Database Schema and then the models based on that schema.

> **Notice**: I will publish the complete detailed version of all the articles on the Medium website. Here I will give an overview and give the codes for the various pages part by part. It would be a 6-7 part series.
So, [please click here to go to Medium and read the second part in completion](https://shubhamstudent5.medium.com/build-an-e-commerce-website-with-mern-stack-part-2-designing-the-models-e231b2454aba?sk=bfa0b70575c97c125c18e380b29ff420). *(These are friend links so do not worry about paywall)*

To keep things clean and simple, we would create a new folder named models in our root folder.

We will then create four files inside it which would represent our four models — User, Item, Cart and Order.

>Note: We do not need to give a unique id parameter to our schemas since MongoDB automatically provides a unique ID once we save any document in it.

So, we will now go into the details of each model one by one. Let’s start with the User model.

####User Model
```js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const { isEmail } = require('validator');

const UserSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: [true,'Please enter an email'],
        unique: true,
        lowercase: true,
        validate: [isEmail, 'Please enter a valid email']
    },
    password: {
        type: String,
        required: [true, 'Please enter a valid password'],
        minlength: [6, 'Minimum password length must be 6 characters']
    },
    register_date: {
        type: Date,
        default: Date.now
    }
})

module.exports = User = mongoose.model('user',UserSchema);
```

####Item Model
```js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const ItemSchema = new Schema({
    title: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    category:{
        type: String,
        required: true
    },
    price: {
        type: Number,
        required: true
    },
    date_added: {
        type: Date,
        default: Date.now
    },
});

module.exports = Item = mongoose.model('item',ItemSchema);
```

####Cart Model
```js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const CartSchema = new Schema({
    userId: {
        type: String,
    },
    items: [{
        productId: {
            type: String,
        },
        name: String,
        quantity: {
            type: Number,
            required: true,
            min: [1, 'Quantity can not be less then 1.'],
            default: 1
        },
        price: Number
    }],
    bill: {
        type: Number,
        required: true,
        default: 0
    }
});

module.exports = Cart = mongoose.model('cart',CartSchema);
```

####Order Model
```js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const OrderSchema = new Schema({
    userId: {
        type: String,
    },
    items: [{
        productId: {
            type: String,
        },
        name: String,
        quantity: {
            type: Number,
            required: true,
            min: [1, 'Quantity can not be less then 1.']
        },
        price: Number
    }],
    bill: {
        type: Number,
        required: true
    },
    date_added: {
        type: Date,
        default: Date.now
    }
})

module.exports = Order = mongoose.model('order',OrderSchema);
```

So, that was all about the models in our application. We will end the second part now since we have finished building all the models we are going to use in our application.

To read the complete tutorial, please [move to Medium and read the complete article](https://shubhamstudent5.medium.com/build-an-e-commerce-website-with-mern-stack-part-2-designing-the-models-e231b2454aba?sk=bfa0b70575c97c125c18e380b29ff420).

Now, in the next part, we would deal with the routes and the controllers. Also, we will deal with some custom middleware functions we will be making in the next part.

Hope you liked this part of the tutorial. I really hope you learnt something new and interesting today.