All right.
And once we have mock data in place now let's programmatically inject those jobs in the database.
And in my case, I'm going to do that for two users.
I'm going to do it with test user as well as the admin one.
Now, please keep in mind that admin one technically is optional.
I'm just doing it because later when we work with stats and get all jobs, I just want to showcase how
the functionality works.
But we definitely want to do it with test user because remember we already restricted access to create
a job and of course we do want to display some example jobs.
Now also, since there's quite a bit of functionality, first I want to go line by line.
What is happening since I don't want to do 10,000 console logs and then we'll set it up together.
So for starters, we want to grab, read, file and notice this is another approach that we can take.
We can just go with FS and then forward slash promises.
And I'm looking for read file since I want to read that Json file.
And essentially it's because I just want to stick await in front of it.
I don't want to use the callback approach.
Then we have mongoose from Mongoose.
Since we'll have to connect from the scratch to a database, please keep in mind that we'll run this
file separately.
Yes, we already have the functionality in the server where we connect to the database, but this is
different.
And then since we have the connection string in dot env, we're going to go with dot env and then config.
We want to grab both of the models job and user and then we'll right away set up the try and catch first
thing we'll communicate with the database, we'll connect to it.
Then we'll grab the user based on email.
Like I said, in my case, I'll have two instances, I'll have one for John and one for test user.
Then we want to grab the jobs from the mock data.
And this is the case where we need to do quite a bit of acrobatics because again, we're using ES6 modules.
So first we have Json.parse, then we await read file, we pass in the new URL, the location and the
file name, and then we add this import meta URL.
So this is going to get us that array and we want to iterate over that array and add created by and
then set up the user underscore ID.
So in this case it's going to be for test user.
And then for John, of course the ID is going to be different.
Then we'll go with await Job, delete many.
And essentially I'm just going to delete all the existing jobs if there are any, by this user.
And then we can nicely run, await job dot, create, and then just pass in this array.
And just like magic, we'll have tons of jobs associated with that specific user.
Then we'll just log success and exit.
And if there's any kind of error, of course we'll have console log and then process dot exit one.
So let's try it out.
We want to navigate to our route and in my case I'm going to call this populate JS For starters, let's
set up those imports.
And you know what?
In order to speed this up, I will grab them.
So let's navigate back over here.
This is going to be our setup.
Then we want to go with try and catch and step by step.
Let's set it up.
We'll start with Await then Mongoose Dot Connect.
So first we want to connect to a database.
So process dot env dot and then we're looking for mongo.
Underscore.
You are all.
Then after that, we want to grab the user.
So I'll run this file twice first for test one and then for the John one.
So users equal to await.
Then my user model.
Then find one and based on the email.
So email is equal to test.
At Test.com.
Now, of course, if you use different email, please use that one.
Then let's grab those jobs.
And this is where we do need to go with those acrobatics or Json jobs is equal to Json parse.
So json.parse.
Then we'll go with await and then read file.
Inside of the read file, we'll provide a new URL.
Let's go with the path in my case that is in utils and then file name is mock data and Json and then
make sure you place this inside of the URL, not inside of the read file where you want to go with comma.
And we want to set up as import matter.
And you are.
Let's save it.
So this is going to be our list again, if you want to log it, please do.
So In order to do that, you want to go with Node and then populate.
But in my case, I'm going to skip that step since there are quite a few jobs.
So essentially in here I just want to go with const jobs, then Json jobs, I want to iterate over and
then each and every instance I'm going to reference as job.
And then from the iteration I want to spread out all of the existing properties, basically whatever
you have over here.
The company position and the rest of them.
And I just want to add that created by because remember that points to the user.
Correct?
So created by and we'll set it equal to user.
So that's why we access this user over here.
And then remember the ID property is underscore ID, so we set it up over here like this, then we save,
then we want to go with await Job and then delete many.
So we just want to start from the scratch.
Now of course, with our test user, there are no jobs.
But you know what?
Just in case so created by and we want to set it equal to user dot underscore ID and now let's inject
those jobs.
So here, of course, we're looking for await job, then dot create and we pass in the array.
And then lastly, we have log over here.
Let's just write success.
And let's just exit the process.
So we'll go with Process dot Exit and we're looking for zero.
Now, if there's some kind of error, we definitely want to log that.
So let's set up the log.
We're looking for error.
And in this case, the exit is going to be with a value of one.
Let's set it up over here.
Let's open up the integrated terminal.
We want to clear everything.
So type clear and then instead of running npm run dev, we want to go with node and then the file name.
So in this case populate.
And if we can see the success in the console, of course we are in good shape.
So now I added those jobs and the user is my test user.
Now, like I said, I also want to do that with my admin one and therefore I'm going to go here with
John at gmail.com.
I believe that's the email I use.
Again, let me run it one more time and if everything is correct again, I will see a success message
in the console.
Now of course we can nicely test it out.
So I'm going to close this tab over here if you want.
You can obviously check it in MongoDB, but you can also nicely see it right over here.
Now, I do right away have the error message simply because I forgot to spin up my server.
So let me go here with NPM run dev.
Make sure you also do that.
Yep.
Looks like everything is correct.
And check it out now.
I'll have tons of jobs.
And guess what?
Of course, the owner is my test user by the name of Zippy.
Now this is the case where I can showcase that it's not going to work.
So if I try to delete the job, we'll have demo user read only and pretty much the same goes for edit.
If I navigate over here and try to change something, let's say I'll try to remove some characters.
The demo user is not able to do that.
And lastly, I just want to showcase that.
Of course the same is with John.
So if I log in as my admin, I'll also have tons of jobs and I'll stick with this profile while we're
setting up the stats and complete the all jobs page.
And with this in place we have successfully.
Programmatically populated our database with a bunch of job instances for our admin user as well as
the test user.