Hey, did everyone that they shared and in this video we'll be writing model for our product itself
and feel free to add more products.
Now you have full experience of how to handle that, so it shouldn't be a big deal.
Now, this one is going to be a little bit of a longer video because we have so much of things to add
into the into the model of the product itself.
So it's going to take a little bit while.
So have patience on that part.
First, let's go ahead and set up our model.
We have already written that.
So it's inside the product.
So how do we write our model itself?
It's fairly, fairly simple.
So let's go ahead and say, Hey, Mongoose, please help me to write a model.
And it's going to help you by saying require mongoose.
Okay, so we use a simple method from the mongoose itself.
First, let's hold that into a variable and let's call this one as product schema.
Schema.
There we go.
And the method from the mongoose itself is going to say, Hey, just help him to create a schema.
So I need to say new because it's a class here.
So new mongoose dot schema.
There we go.
And inside that we play some a whole lot of objects, whatever we actually need up there.
But first, I always love to export this one, so I'm going to go ahead and save module exports so anybody
can import that.
And we're going to say, Hey, Mongoose, help me to create a model.
So Mongoose model model takes to parameter that.
What do you want to call to a model?
And just with the convention, we always call with the uppercase letter, but I know that inside the
DB it will go as products all lowercase.
What schema do you want to use?
I want to use a schema of product schema.
There we go.
Okay.
So this is the most basics that we got up here.
Now we need to actually bring some data and we'll be bringing data from here itself.
So I'll be just copying everything from here.
It will help me to actually get easy check up here.
I'll come here and add this and obviously I'll remove this, all of that in a minute.
But first, I'm going to go ahead and comment this out.
Okay.
So let's go ahead and add all of the data one by one.
So let's go ahead and do that first.
Let's start with the name.
So we're going to go ahead and say name.
I can of course, say string directly here, but I have a lot more to handle.
So that's why I'm going to say define properties one by one.
So first one is it's going to be of type string, nice and easy.
Now the name obviously is going to be required fields.
So we're going to go ahead required and not only just true we'll pass on an array through is the first
property or zeroth property in this case and we're going to provide the error message as well.
So please provide product, product name.
Okay.
So error message is also handled.
We are also going to use a property, another one trim.
I'm pretty sure most of you have heard about this property.
It's really an easy property to work on with.
If you have any additional spaces at the end of your product name, you can go ahead and remove that.
You can actually use this property inside the email as well.
Just a cautionary stuff.
Now you can also go ahead and define what kind of maximum length you want to support in this case.
So I'm going to assume that probably 120 words is characters is not words.
120 characters is more than enough for a product name.
Let's give a provide a message to the user as well.
That product name should not be more than 120 characters.
So there we go.
Okay, let's go ahead and do a copy of this so that it helps us a little bit.
First, put a comma and now let's go ahead and make a copy of this.
Okay.
So next one that we have is the pricing.
So let's go ahead and say I want to give you a price here.
Now, the price is obviously not going to be a string.
It's rather going to be a number required.
True.
We are going to say please provide product price.
Nice error message.
They're very consistent trim.
We don't need to do a trim.
We're going to go ahead and remove this maxlength surely I can go ahead and say, hey, maxlength is
going to be, let's just say five now these are digits since we are talking in the case of numbers.
So these are the digits.
I don't want any product to be more than five digits.
Surely I would love to sell a t shirt of probably five lakh rupees or 20 lakh rupees, but I think five
digit is good enough.
So product price should not be more than.
Five digits.
And again, these are not digits counting from the zero.
So five digit, I guess it's more than if you can go ahead and change this probably to six.
Maybe you want to sell expensive one.
You get the idea how this is being done.
Okay.
Next up is the description.
So let's go ahead and add this one.
Okay.
Now let's go ahead and change this one to this.
Corruption.
Description.
There we go.
Okay.
So this one is going to be of type string.
And in the description, we are going to not put a maximum length because description can be long and
maybe there are future plans of supporting more stuff.
But we're going to say required because this is kind of important.
Otherwise our application will look weird if this will be optional.
So we're going to say please provide product description.
Next one is a photos.
So let's go ahead and work on with the photos.
So let's go ahead and copy this, paste that OC The photos is going to be an interesting one.
Not really interesting because something like this, we have already handled that.
So shouldn't be a big deal there.
Let's go ahead and call this one as photos now this time at the at the model that we have previously
worked the user itself, this is just a photo because we were adding just one photo in this case, this
is going to be a photos because we are adding a whole lot more there.
So this time it's not going to be an object directly.
It's rather going to be an array so that somebody can actually loop through it.
And these are all the things you have to take care of while designing a model itself.
And inside this we're going to add an object.
Each individual object will have two properties ID and the security URL.
So something like we have already worked with that.
So ID again, we are going to go ahead and say, hey, this one is going to be of type.
There we go.
Type of the ID is going to be string and this one is going to be required.
So yes, we need images up there.
So we are going to say required.
So at least somebody passes us one image and that one image is going should have an ID and should have
a security model as well.
So required is going to be.
Come on.
Required is going to be true.
And now we can put up a comma and make a copy of this one, and we're going to call this one as a secure
URL type a string required is true.
We don't need this trailing.
And there we go.
Now we can have as many objects inside this ETA, but each object should carry these two properties.
So this is how we are going to handle the photos.
Okay, so this is all good.
Now, coming up after the photos, I need to copy this one just to save some minutes, some seconds.
There we go.
So after the photos, we got into the categories.
Now, this is interesting one.
Now surely I could have created another model just for the categories, but since in the discussion
we came to know that it's short sleeves, long sleeves, sweatshirts and the hoodies as well.
So that's why we are super easy on that part.
But once you know how to inject users into the product itself, I'm pretty sure it shouldn't be a tough
to add categories into the product because they are just models.
Once you know how to inject one model, you can add other models as well.
So let's go ahead and call this one as category, not categories.
It is kind of a makes sense.
Okay, this is going to be a type string.
And again, did we made a mistake in the string?
Yep, we made a mistake.
There we go.
So a small typo there just fixed it.
And this one also.
Okay, so categories is going to be type string and we are going to say true and we are going to provide
a message.
Please.
Select categories.
Category from.
We are going to go ahead and save from and let's say Daesh and this one is going to be only short.
Dash sleeves.
And we're going to say long dash sleeves or it can be.
Sweat does shirt.
Or it can be hoodies.
So we're going to go ahead and say hoodies.
Now we are why we are providing message like this, because we'll be providing the option to select
here.
Now, in case you want to any time restrict the users option in is always your best friend.
So we're going to say, hey, we're going to provide an enum and inside this enum we are going to go
ahead and provide the values.
So there we go.
Values is always an error in the enum, so let's go ahead and provide these.
So we're going to go ahead and say, hey, this one you can choose from short dash sleeves.
And again, you have to provide the exact name that you are mentioning here.
Make sure it is not case and not uppercase lowercase.
Make sure you are consistent there.
Long dash sleeves.
We are going to go ahead.
Another one which is going to be sweat again in the codes.
Come on.
Sweat shirt.
If I can write that finally.
And the final one is hoodies.
Now, it makes sense in this case, since we have only these ones, in case you want to give the user
ability to just kind of a CRUD functionality with the categories as well.
We have already done that in the main course.
You might want to check that out, but I don't think so.
It should it be a big deal for you now?
Okay, so this is all good and it looks great.
And by the way, you can also provide a message and in case somebody provides a category which is something
else than this, then specifically you can go ahead and provide a message for that.
Now this message required is going to come in whenever somebody says that I'm not providing you any
category, but if somebody chooses something else, then we can go ahead and provide a message here.
So the property is known as message.
There we go.
And the message goes just like in the codes that please.
We can actually copy this.
That would be easier.
We're going to go ahead and say please select category only from short sleeves.
There we go.
From short sleeves, long sleeves, sweatshirt.
And howdy.
So there we go.
So now this is really easy and gave us a chance to explore it a little bit more about how to explode
enemies.
Okay, so category is all done.
Now let's go ahead and work on with the brand.
So after the category, let's go ahead and add a brand.
So brand is going to be really simple.
Let's go ahead and work on like that.
And in the brand we're going to say type.
The idea behind the brand is really simple right now.
It's just learn code online, but maybe tomorrow I want to sell t shirts of Adidas or maybe something
else, some cocreator on YouTube.
So that's why we are adding a brand here as well.
The required is going to be same, just like we have talked probably many times.
True.
And we're going to say please add a brand for clothing.
What else we got?
We got rating here as well.
Now, this rating is interesting ratings.
So let's go ahead and add this one.
So there we go.
Really simple.
We don't need too much explanation on ratings.
It's going to be of type number so that later on I can average it out.
I can find a lot more stuff.
And this is not going to be required because user will not pass on this one.
We need to just provide a default because every single product will obviously have some of the ratings,
but by default everything is off is going to start with a zero rating as the user will give on more
ratings.
Five star to start, three star.
I'll add on an average into this rating.
So I have to do that.
I'll manually work on these things.
Let's move on.
What else we got?
We got ratings.
We got a number of reviews as well.
So a number of reviews.
Really simple.
How many people have actually reviewed it?
As soon as a new rating comes in, I'll just add one to this one.
So really simple.
We're going to go ahead and say number of reviews and that's going to be like this.
This is going to be a type of number and obviously by default, there is going to be no rating.
So the default says zero.
Okay.
Just a few more then this reviews.
Now, this is an interesting one.
We already got a discussion on this one, but we'll have a talk, small talk here as well.
Let's go ahead and copy this.
Put up in this guy.
Okay.
So what is this?
This is actually reviews.
So all the reviews, every single product is going to get a reviews.
So whenever you add a review, it is associated with the product itself.
That's why we are keeping it in the model itself.
Now surely you can keep it into separate model and add an injection or something like this, but we
won't be doing that.
We'll be just keeping it as reviews.
Now obviously this reviews is going to be array because maybe there are so many errors somebody wants
to iterate through with it.
So that is obvious.
Now what do you want to inject into this one?
The first thing that I would like to add is who is the user who has given me the ratings?
Only logged in user can give me the rating.
So how we inject this one, this is really, really simple.
Like ridiculously simple.
All you got to do is simply use Mongoose again after the column, of course.
So I'll go to say is Mongoose dot schema and then just go ahead and say object id make sure this is
exactly looks like this.
Oh capital and I capital.
So what I'm saying here is let me walk you through inside this.
You have noticed that every single object that you add inside the database, it has this object.
ID remember it is written as o capital and a capital so object ID.
So what I'm saying is I will give you this object ID now in the object ID when you actually add them.
It looks like just a long string with numbers and some letters, but it's actually a based on field
just like JSON but based on field.
Now this field has all of this numbers.
So what I'm saying is whenever I'll be creating this, I'll inject this entire value up here.
That's why I'm referencing it.
That that there will be an object ID, mongoose, object ID that will come to you.
You also have to pass on that.
Hey, anything can be an object ID.
Maybe it can come up from user, it can come up from product itself.
Maybe you are creating categories from itself.
So from where it should be coming up so that I can go ahead and validate it myself.
So then you have to go ahead and provide a ref here, which is a reference that I'm going to go ahead
and say user.
Now this user needs to be exactly same as what you are calling it up here.
Let me show you at the end of it.
So this is what I'm calling it.
I need to pass on exactly that in the reference.
If you're calling it as category or whatever you are calling in, you just have to do this.
Now, this two line of code, that's it.
That's how you can inject another document value into other document.
That's all it takes.
Now we're going to go ahead and say this is a required field, so we're going to go ahead and say required
a is true.
So now any time a review comes in, they have to give me this user object.
Now further down the road, we probably also would like to add a name of this.
It will look good at the time of showing the review.
So let's go ahead and say, hey, I need to provide a type.
The type is going to be a string scroll a little bit and let's go ahead and move on to required.
Okay.
Now you can see it.
Now you're going to see that this is a type and required is going to be true.
Okay.
So these are the basic field.
Of course, whenever you are taking a rating, you need to take the rating a number between one and
five.
You can provide an enum here as well.
That would be awesome, but I don't think so.
It is required again, but surely having that is not a bad idea at all.
And a comment that what do you want to say in the rating itself?
So really simple.
So we're going to go ahead and say, hey, this is the rating that you are providing.
This rating will be of type number.
Not like that, of course, like this type number.
Come on.
There we go.
Okay.
So this is going to be a type of number.
Surely this is also required field.
So what I'll do is I'll copy this and I'll paste it up here.
Okay, so this is all done.
Now, next thing is the comment itself.
So let's go ahead and add a comma and give itself a copy.
There we go.
So this one is going to be a feedback, comment, whatever you want to call that, it's up to you.
This is going to be a string.
So all these fields.
So now my review itself, or reviews rather itself is an array.
And whenever you want to add any value into this array, you have to provide me for things.
User and again users object ID, so you need to grab this somehow and then we have to provide the name
rating and the common.
And again grabbing the user ID shouldn't be a problem at all because only logged in user can give rating.
So we have injected an object request dot user.
So we'll be able to grab name and everything from there.
So shouldn't be a big deal at all.
Okay.
Moving on now.
The next thing is user.
So what is the meaning of this user itself?
So let me go ahead and shrink this review.
It's too big.
Okay.
Now, the idea behind this user is because we might be having so many roles and user at the later point
of time.
Maybe you have a dedicated manager who adds product into your shop, so you want to see who has added
this new product.
So you might want to somebody who is accountable for that.
And that is the only reason why we are adding this user field.
So it's a great revision.
Again, I want to see who is the logged in user who has created this project.
So all I got to do is simply same.
It's going to come up from another document.
So we have to say this is going to be coming from mongoose dot schema, dot object id make sure the
case sensitivity is there.
I also need to provide a ref that hey from where I should reference this one.
So in this case that's the user.
So that's it.
Now again, this is going to be required.
Field So let's go ahead and say, hey, required is going to be true.
I really want to know who has actually came in and added the field.
Okay, what else is remaining?
Just one.
Come on, just one.
We can do it.
This one is going to be created at.
And this one is going to be an object.
This is going to be again, same date we have done it so many times.
Shouldn't be a big deal and the default is going to be date dot now.
Should I write this one?
Yeah.
Great that you have denied.
No, we shouldn't be writing this way.
We should be writing this.
We don't want to execute it right now.
Now, I can go ahead and remove this one now because it's of no use but helped us a lot.
Now we don't really write too many of the methods and inside the mongoose model itself.
But in case you want to go ahead and write more methods as simple something like tokens we have done
in the eight of us, not eight of us in the user.
Like JWT tokens and stuff, we can go ahead and do that, but in this case we won't be needing that.
So quite a long video.
Now, let's go ahead and catch up in the next one.