DEV Community

Cover image for Build an E-Commerce Website with MERN Stack - Part 2 (Designing the Models)
Kumar Shubham
Kumar Shubham

Posted on • Edited on • Originally published at shubhamstudent5.Medium

Build an E-Commerce Website with MERN Stack - Part 2 (Designing the Models)

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. (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

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);
Enter fullscreen mode Exit fullscreen mode

Item Model

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);
Enter fullscreen mode Exit fullscreen mode

Cart Model

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);
Enter fullscreen mode Exit fullscreen mode

Order Model

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);
Enter fullscreen mode Exit fullscreen mode

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.

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.

Top comments (6)

Collapse
 
lcprog profile image
LC

this would have been such a great tutorial article, but unfortunately you fell prey almost every other tutorial where you present only a portion of the material and then forget the rest. where the hell is the client portion.

this article should have been titled "how to create a node express back-end" or better yet, you should have had your coffee and then skipped the exercise altogether, you would have done us a great favor by not enticing us with something you were not going to complete.

where are the mongo urls samples, where are the config samples, and where is the client code

what a shame....

Collapse
 
shubham1710 profile image
Kumar Shubham

I have not forgotten the rest. I will complete each and every part including the client portion and config file. It is planned to be a 6-7 part series. I have only published 3 parts till now. More parts will come in coming days. It would be complete.

So, please wait for the parts to come. I hope you enjoy the articles.

Collapse
 
dballowe7912 profile image
Donald Ballowe • Edited

I think its a great approach, I have found that creating the backend for an existing client side template is great practice and is how I have learned a ton. If you just follow tutorial after tutorial, you really don't retain as much as setting out on your own. I use tutorials now only as references when I need a hand in figuring things out. So keep up the great work bc you helped at least a few of us!

Collapse
 
joseph_muvevi profile image
JosephSam

Hi, amazing content; you have a typo though at cart > items > quantity > deafult instead of default

Collapse
 
shubham1710 profile image
Kumar Shubham

Corrected the typo!

Collapse
 
shubham1710 profile image
Kumar Shubham

Thanks a lot! Sorry for the typo.