Okay, so now we're going to model our data and I'm going to just do all of it now.
So all the collections products, orders, users, we're going to create a model file for all of them
rather than just doing products now and then doing orders later.
Let's just get it all out of the way.
So with MongoDB and with most NoSQL databases, they're very flexible, meaning that unlike with relational
databases, you don't have to go in on the database level and declare all of your fields and all of
your all the types for those fields like you would with with something like Postgres or MySQL.
However, we do want some structure, so we do it on the application level, which is what Mongoose
is used for.
So in the back end, let's create a folder called Models.
And then in models we're going to create a product model dot JS file.
Okay, so this is where we want to create our schema and export it as a model.
So first off, we need to import Mongoose and then we'll create a variable called product schema.
And inside this new Mongoose schema will be an object with all the fields that we want.
Now we're going to have a lot of fields here.
I might paste some in, but I'll start off just typing them.
So we're going to have a name and you can actually do something like this.
Like you can say name String if you don't want to do anything else to it.
Like if you don't care if it's required or if there's any kind of default value or anything like that,
you can just define the type or you can make it an object and then you can say type string.
Okay, Now the reason I'm doing this is because let's get rid of that is because the name I do want
it to be required.
So I also want to add the value for required to be true.
All right.
So that's the name.
Next, we're going to have the image.
So the image is just going to be a string as well.
So type, string and that's also going to be required.
And the way it's going to work later on is if you don't upload an image, it's just going to use a sample
image.
But either way it will have an image and the actual image gets stored in the file structure.
But the name of the image, the location is going to get stored here.
So we're also going to have the brand, which is going to be the type string.
We'll make that required as well.
And then let's do category type string required.
True.
So we're basically requiring pretty much everything.
And then we want a description.
And the reason copilot knows what usually knows what I'm looking for is because it's going by the first
version of the app that I've had on GitHub for a long time, and it's pretty popular.
So after description, we're going to have reviews.
Now reviews is actually going to be another schema and we could put that in a separate file, but we'll
just put it in this file.
So what I mean by schema is what we're doing now.
It'll have a schema with certain fields, so I'm just going to have some brackets and then we're going
to say review schema, which we'll create after now the rating, let's say rating, that's going to
be the type of number it's going to be required and it's going to have a default of zero.
Okay.
And then we also want the Num reviews, So the number of reviews, that's also going to be number and
that's going to be required and that's going to be zero by default as well.
Then we'll have the price, which will be a number required.
True.
And we'll do default zero for that.
And then finally we'll have the count in stock.
So number required true default zero for that as well.
All right.
So those are the fields that we want.
Actually, we want one more.
And I want to save this for last.
That's the user.
Okay.
So every product needs to be connected to a user that needs to be a relationship because you need to
know which user.
In this case it would be an admin user which one added that product.
All right.
So what we will do is and I usually like to put this at the top so it'll be a user.
And as far as the type, it's going to be a mongoose object ID.
So any time we create anything in the database, it has an underscore ID field and that's the object
ID.
So and that has its own type.
So that's what we're giving this and it's going to be required.
And then we need to we need to specify where which collection this is coming from, this object ID,
and it's going to be from the user collection.
So the reference, I should say.
All right.
And that should do it for user.
Now.
I also want time stamps, meaning I want to create, adapt, and modify that field so we could do those
manually like these.
Or we can just go under this last, this last curly brace and we could put in another set of curly braces
and just say time stamps.
True.
So that will automatically add the createdat fields.
All right.
So then we'll just say const, I'm just going to say product and we're going to set that to mongoose
dot model and we're going to pass in product.
Okay, So that's the model we're creating.
And then the schema that we want to use is the one that we just created.
And then finally, I'm just going to export default.
Product.
All right.
Now, before we leave this file, we do have to add the review schema, because right now that doesn't
exist.
So let's go ahead and put this right above the product screen schema.
So we'll say review schema.
We create it just like we did the product schema and it's going to have a name which will be string
and required and then it's going to have a rating.
Number required and it's going to have a comment and that's going to be type string required.
And that's also going to have a user because we need to know which user is which user created it, right?
So I'll just copy this.
And I'll put that at the top of this as well.
Okay, Because obviously the user of that creates the product is not the same user in the review.
I mean, I guess it could actually it couldn't because we're going to make it.
So you can't review the your own product.
All right.
And then let's also do time stamps here as well.
So right here.
I'll just go like that and say timestamps.
True.
So that's our model for our product.
Now let's do our users.
So let's create another file.
We'll call it user.
Model dot JS.
I did singular for Productrillionight.
Yeah.
So I like to keep.
I like to make the models singular.
So the user is model or schema is pretty simple.
So let's just import.
Mongoose and then say const user schema.
Set that to a new schema.
And we're going to have a name for the user which will be required.
We're going to have an email.
And emails are going to be unique.
And then password.
Okay.
And the password obviously will be hashed.
It's not going to be just a, you know, a plain text password.
And then we also want to know if the user is an admin.
So that's going to be a Boolean value and the default is going to be false.
So in order to make an admin, we specifically have to go into the database and change that.
Change this value to true.
And then we'll do time stamps as well.
So right here.
Oops, Right here.
Let's say time stamps.
True.
And then const user.
And there we go.
And then we just want to export as default user.
Okay, so that's our user model.
Now, as far as orders go, there's quite a bit of data here I might paste in some, but let's create
a new file called Order.
Model JS.
And let's import Mongoose.
Let's create.
The order schema.
And we're going to have a user just like we did for the other things.
Okay.
And then we're going to have order items.
Now Order items is going to be an array.
So we're going to use.
Actually, let me just.
Yeah, we'll do that.
So it's going to be an array of objects.
So let's close that off and each order item will have a name, which will be a type of string.
It's going to have a quantity.
We're going to have an image.
A price, which will be a number, and then product, which is going to be a reference to the product
in the products collection are the products model.
So again, it's going to be the type of object ID required.
True, except the ref is going to be product, not user, because now we're referencing a product.
So having the the square brackets tells Mongoose that this is this is going to be an array with these
objects.
If I didn't have the brackets, then it would just be a single object.
But we want obviously the the user is going to have or could have more than one item ordered.
So we want to make that an array.
All right.
We could make it another schema, but we'll just.
We'll just do that.
That's fine.
So now let's put a comma here.
The next thing we want is the shipping address, which is going to be it's actually going to be an object
as well that will have the address, the city, the postal code and the country.
Okay, so that will be the shipping address.
Then we'll have the payment method that's going to be type string required.
True.
Then we have the actually this copilot is really helping as far as typing, I don't have to copy and
paste, but I also don't have to type it all out.
So payment result that's going to have an ID and a status and then the update time and then an email
email address.
All right.
So basically, when it goes through PayPal and they pay it, this will get updated the status.
All right, then we want the let's do the items price first or item.
Item's price.
So that's going to be type number.
It's going to be required and the default is going to be 0.0.
I'm putting that because the prices are going to be formatted as decimals.
I mean, there's still numbers in JavaScript.
You don't have a float or a decimal type, but they're going to be formatted like that.
Then we have the tax price.
So same thing number default is going to be 0.0.
Then we'll have the shipping price.
Same thing.
And then let's do.
The total price.
So this will be all of it together.
And then let's say is paid.
So it will have a status of is paid, which will be a Boolean.
And then it'll also have a status of, well, let's do paid at first.
So paid at will be a date and then we'll have is delivered.
So if it's shipped and delivered, then this will be a flag to tell us that.
And then we'll also have a delivered at which will also be type date.
And that's it.
We just want to add timestamps as well.
So we'll go right here.
Okay.
Just make sure you do that.
Don't do this time.
Stamps.
Don't put it there.
That's.
That's a mistake that I've made a lot.
All right.
So now we just want to create the variable order and set the model.
We're using the order schema and then we'll export it.
Okay, so now we should have all of our data modeled.
So what I want to do next is prepare some sample data.
So basically I want to have some products in the database.
They're actually going to be the same products that we have here, but I want them in the database along
with some users because a product belongs to a user.
So if we add a product, we need to add some users.
So what we'll do is prepare our data and then we'll create a seed or a data seed, which will be a script,
a file that we can run that will import all of our products and users for us.
So we'll get started on that next.