Okay, so now we're going to get ready to seed our data.
Now there's one optional package I want to install called Colors, and that just allows you to add colors
to your terminal or to your commands, not your commands, but your output in your terminal or console.
So we want to install this in the server dependencies.
So make sure you're in your root and let's say NPM install colors.
And again, this is completely optional.
It's really easy to use though.
So next thing we're going to do is create a file in the back end folder.
So in back end, let's say Cedar Dot.
JS So this is going to be the script that we run that will seed all of our data.
And there's quite a, quite a few things we need to bring in here.
So we're going to bring in Mongoose, we're going to bring in oops, we're going to bring in dot Env.
And the reason for that is because we need the the mongo Uri obviously because we're importing the data.
Just think of this, this file is completely separate from the rest of our application.
We're not bringing it in anywhere or anything like that.
We're just running it from the command line.
Like literally we could say Node and then Cedar and run the file.
I'll create a script for it, but basically that's what we're doing.
So we have to bring everything that we're going to use in this file directly in.
So let's bring in colors if you want.
You don't have to.
And we're also going to bring in the users data, which is in let's see.
So it's going to be data and then users.
And then we also want the products.
Okay, then we're going to bring in the user model because we are we are entering or adding a user to
the database.
And any time we make queries to the database, it's going to be through a model, whether that's user
product or whatever.
Then we're also going to need the product model.
So let's bring that in as well.
And then I'm going to bring in the order model.
Now, we're not going to be creating any orders with this cedar, but we are going to have the ability
to wipe the database clean.
And if there's any orders in there and we wipe the, you know, the users and the products, that's
not going to make much sense.
So we want to make sure we wipe everything, including any orders.
Then we want to bring in our Connect DB.
So that's just to connect to our database that's in config and then DB.
JS All right, now we want to initialize our dot env so we can use those variables.
We want to connect to our database.
Okay.
And then we can create our to our two functions.
So basically we're going to have import data and destroy data and the name is pretty self-explanatory
as to what we're doing.
So as far as import data, let's do a try catch block here.
And then we're going to, first of all, just delete everything.
So before we import any products or users, we want to delete them all.
Okay?
So to do that, we're going to await.
Because every every mongoose method that you call is going to return a promise.
So we need to use async await or you could get rid of a wait and you could do, you know, delete many
dot dot, then dot catch and so on.
So what this will do this delete many is just that it'll delete multiple records and we're using the
order model which we brought in up here.
And if we don't pass anything in, it's going to delete everything.
So we want that.
And then we also want.
To delete all of the products.
So we'll use the product model, delete many and also the users, so the user model and then delete
many.
Okay, now we want to create our users and we're going to put the created users into a variable.
So what's happening here is we're awaiting on user dot insert many and we're passing in users, which
is right here coming from the data that's in that data folder, right?
So it's going to insert those and then it's going to return that array of users to this variable.
Now when we insert the products from products JS, they have to have a user and obviously we want the
admin to be that user because those are the only users that can create products.
So what we're going to do here is get the admin user, so let's say const admin user, and the way we
can do that is just simply by taking created users, which is the three users that were created and
get the first one.
So the index of zero and then get that ID, that object ID, So that's the admin user.
Now let's put a space here, just separate this out.
And now what we want to do is insert the products.
So we're going to have a variable that's going to hold the products that we're going to insert.
So I'm just going to type this out.
So sample.
Products.
And then what we can do is take the products that are being brought in from that file and then we're
going to map through it and let's say for each.
Product.
Okay.
So for each product and then we'll open up a code block.
I want to return.
The I'm going to return an object that has all that product data plus a user value of whatever that
admin user is.
Okay.
Which would be this the ID?
So it'll be, you know, an object will have all this, but in addition to that it'll have the user
and then whatever that user ID is and we're just using the spread operator to, to add all the, all
the product data here.
All right, So now we haven't entered anything into the database, but we have this variable of sample
products that we want to enter to the database.
So the next step would be to call, insert many on the product model and just pass in the sample products.
So at that point we'll have the users inserted from this line, we get the admin, we create a variable
with all the products, including the admin user, and then we insert all that into the insert all the
products into the database.
So then we'll just do a console log and we'll just say data.
Imported.
And I'm going to add this after the string dot Green Inverse.
So this is where that colors package comes in.
Okay.
And it's a little weird because we do have to bring it in as colors, but we don't actually use colors
anywhere.
But it allows us to do stuff like this.
And if you look at the documentation, you can see all the cool things you can do.
You can do like text color, background colors, you can do strikethroughs and just all kinds of cool
stuff.
So then we just want to exit the process.
So process exit, we don't want to kill it, so we're not going to pass a one in.
Just leave it like that.
And then if there's an error, then we're going to console log the error and I'm going to add red inverse.
So that gives it a red background and that will close with with error.
All right.
So that's import.
Now we want to destroy.
And this, this one, this function is easier.
So let's say const destroy data and we're going to set that to an asynchronous function.
And then what we'll do is have a try catch.
And in the try we're just going to simply call delete many again, just like we did up here.
Okay, so before we did everything here, we deleted anything that's in the database in those three
collections and then that's it.
We're just going to have a console log that says Data destroyed will exit.
If something goes wrong, we'll have an error and then we'll exit.
Now, how do we run these methods?
Right?
Because we can run the file easily by saying node and then back end slash.
Cedar, but I want to be able to.
To choose which which one of these I run.
So the way I want it to work is if I just run the file as is, then I want to run the import.
If I if I want to run destroy, then I want to have to add dash D, So that means that we have to check
for this, this argument and the way we do that.
Let's test this out.
I'm just going to comment, basically just comment out everything for now, just so I can show you how
to get this.
And just in case you guys don't know.
So if we console.log process dot argv, that's going to be the arguments that are passed in.
So if I save this and then I run this file with this dash D.
You'll see it actually gives me an array with three strings.
So the first two are going to be these paths, the bin slash node, and then this path here.
And then the third item, which is the two index is whatever I passed in.
Now if I do dash D and then let's say dash H and I run that dash H would be the, the third index zero
one, two, three.
So to get the, the dash d we could just grab the index of two.
Right.
So now if I do that and it doesn't have to be dash d, I could say whatever Dash hello and you'll see
now it's going to console log dash.
Hello.
So knowing that let's go ahead and do an if statement here.
And let's say.
If the process dot argv and we want to test for the the index of two if that is equal to a string of
dash D.
Okay, So if that's equal to Dash D, then we're going to call destroy data.
Else then we're going to call import data.
If you wanted to say, well, if it's dash, I then import data, that's fine as well, but we're just
going to have it so that if you run it and you don't pass in Dash D, then it's going to import.
Okay, now let's uncomment this, all this.
And before I run it, I don't actually want to run it like this.
I want to create a script to do that.
So in our package.json in The Root, let's go up to where we have our scripts and let's see, we have.
Start server client.
Dev, let's.
I guess we'll just go right under Dev.
So let's put a comma here and then this is going to be, let's call it data colon import.
And what that's going to run.
Is going to be that just the file node and then back end slash cedar.
Dot JS Right, and then let's do this data colon.
Destroy and we want it to still run that file, but we want it to add on the dash D.
So let's save that.
And now we can simply do NPM run data import or NPM run data destroy.
So let's test it out.
NPM run data Colon import.
All right.
So you saw that it connected to the database and we get data imported now to check that you can either
go to Atlas or you can go to Compass.
Now you're going to have to reload this.
So in the menu bar, you'll see a view option and just say reload data.
And now check it out.
We have we have orders, products and users and this is the products, has an ID has a user.
So that object ID refers to a user in the users collection.
And then it has everything else it has created at and updated at.
Because in our schema we said to use timestamps reviews is an array.
There's nothing in there, but it is in fact an array, the rating, the name, image, all that stuff.
So now we have six products if we look in users.
So here's our admin user, that's the ID, the email and check the password out.
It's hashed.
Okay.
The password for all of them is one, two, three, four, five, six.
But we only store the hash in the database is admin is true.
And then we have our timestamps and you can see is admin is false for these other ones.
So that's it.
Now, if I want to clear the database out, I can simply do NPM run data destroy.
Okay, so data destroyed.
Now, if I go back to Compass and I go back up to view and then reload data, I have nothing in my database.
I have the collections, but there's nothing in them.
So pretty cool.
And this this will be very helpful.
Now, I do want the data there, so I'm going to once again just run the data import.
So this is really good for just prototyping and testing.
So that whoops, I think I disconnected.
Um.
See connect.
All right.
So if I go to pro shop and products, there we go.
So we have our products and users in there.
Okay, so that's it.
Now, in the next video, I'm going to introduce you to Postman, which is an Http tool client to make
Http requests, and that's what we'll be using to make requests to our back end.
While we while we don't have any front end to use.