1
00:00:00,050 --> 00:00:00,380
All right.

2
00:00:00,380 --> 00:00:06,920
And once we have mock data in place now let's programmatically inject those jobs in the database.

3
00:00:07,100 --> 00:00:10,130
And in my case, I'm going to do that for two users.

4
00:00:10,160 --> 00:00:14,240
I'm going to do it with test user as well as the admin one.

5
00:00:14,270 --> 00:00:17,680
Now, please keep in mind that admin one technically is optional.

6
00:00:17,690 --> 00:00:24,230
I'm just doing it because later when we work with stats and get all jobs, I just want to showcase how

7
00:00:24,230 --> 00:00:25,460
the functionality works.

8
00:00:25,460 --> 00:00:32,710
But we definitely want to do it with test user because remember we already restricted access to create

9
00:00:32,720 --> 00:00:36,920
a job and of course we do want to display some example jobs.

10
00:00:36,950 --> 00:00:42,860
Now also, since there's quite a bit of functionality, first I want to go line by line.

11
00:00:42,860 --> 00:00:48,390
What is happening since I don't want to do 10,000 console logs and then we'll set it up together.

12
00:00:48,410 --> 00:00:53,290
So for starters, we want to grab, read, file and notice this is another approach that we can take.

13
00:00:53,300 --> 00:00:57,230
We can just go with FS and then forward slash promises.

14
00:00:57,230 --> 00:01:02,130
And I'm looking for read file since I want to read that Json file.

15
00:01:02,310 --> 00:01:06,480
And essentially it's because I just want to stick await in front of it.

16
00:01:06,480 --> 00:01:09,430
I don't want to use the callback approach.

17
00:01:09,450 --> 00:01:11,190
Then we have mongoose from Mongoose.

18
00:01:11,190 --> 00:01:16,440
Since we'll have to connect from the scratch to a database, please keep in mind that we'll run this

19
00:01:16,440 --> 00:01:18,040
file separately.

20
00:01:18,060 --> 00:01:23,610
Yes, we already have the functionality in the server where we connect to the database, but this is

21
00:01:23,610 --> 00:01:24,360
different.

22
00:01:24,360 --> 00:01:30,840
And then since we have the connection string in dot env, we're going to go with dot env and then config.

23
00:01:30,870 --> 00:01:36,270
We want to grab both of the models job and user and then we'll right away set up the try and catch first

24
00:01:36,270 --> 00:01:39,810
thing we'll communicate with the database, we'll connect to it.

25
00:01:39,930 --> 00:01:42,780
Then we'll grab the user based on email.

26
00:01:42,810 --> 00:01:48,090
Like I said, in my case, I'll have two instances, I'll have one for John and one for test user.

27
00:01:48,120 --> 00:01:51,420
Then we want to grab the jobs from the mock data.

28
00:01:52,030 --> 00:01:58,150
And this is the case where we need to do quite a bit of acrobatics because again, we're using ES6 modules.

29
00:01:58,300 --> 00:02:05,980
So first we have Json.parse, then we await read file, we pass in the new URL, the location and the

30
00:02:05,980 --> 00:02:08,850
file name, and then we add this import meta URL.

31
00:02:08,860 --> 00:02:17,470
So this is going to get us that array and we want to iterate over that array and add created by and

32
00:02:17,470 --> 00:02:19,510
then set up the user underscore ID.

33
00:02:19,780 --> 00:02:22,810
So in this case it's going to be for test user.

34
00:02:22,810 --> 00:02:25,510
And then for John, of course the ID is going to be different.

35
00:02:25,600 --> 00:02:28,870
Then we'll go with await Job, delete many.

36
00:02:29,080 --> 00:02:36,010
And essentially I'm just going to delete all the existing jobs if there are any, by this user.

37
00:02:36,010 --> 00:02:42,610
And then we can nicely run, await job dot, create, and then just pass in this array.

38
00:02:42,850 --> 00:02:49,320
And just like magic, we'll have tons of jobs associated with that specific user.

39
00:02:49,330 --> 00:02:52,940
Then we'll just log success and exit.

40
00:02:52,940 --> 00:02:58,700
And if there's any kind of error, of course we'll have console log and then process dot exit one.

41
00:02:58,700 --> 00:02:59,780
So let's try it out.

42
00:02:59,810 --> 00:03:07,370
We want to navigate to our route and in my case I'm going to call this populate JS For starters, let's

43
00:03:07,370 --> 00:03:08,270
set up those imports.

44
00:03:08,270 --> 00:03:09,230
And you know what?

45
00:03:09,680 --> 00:03:12,530
In order to speed this up, I will grab them.

46
00:03:13,150 --> 00:03:14,950
So let's navigate back over here.

47
00:03:15,810 --> 00:03:17,490
This is going to be our setup.

48
00:03:17,610 --> 00:03:21,390
Then we want to go with try and catch and step by step.

49
00:03:21,390 --> 00:03:22,320
Let's set it up.

50
00:03:22,620 --> 00:03:25,590
We'll start with Await then Mongoose Dot Connect.

51
00:03:25,590 --> 00:03:27,870
So first we want to connect to a database.

52
00:03:27,870 --> 00:03:33,630
So process dot env dot and then we're looking for mongo.

53
00:03:34,780 --> 00:03:35,980
Underscore.

54
00:03:36,590 --> 00:03:37,610
You are all.

55
00:03:38,900 --> 00:03:42,480
Then after that, we want to grab the user.

56
00:03:42,500 --> 00:03:48,300
So I'll run this file twice first for test one and then for the John one.

57
00:03:48,320 --> 00:03:51,560
So users equal to await.

58
00:03:51,680 --> 00:03:53,930
Then my user model.

59
00:03:54,200 --> 00:03:58,040
Then find one and based on the email.

60
00:03:58,370 --> 00:04:01,160
So email is equal to test.

61
00:04:01,980 --> 00:04:03,520
At Test.com.

62
00:04:03,540 --> 00:04:07,200
Now, of course, if you use different email, please use that one.

63
00:04:07,320 --> 00:04:09,180
Then let's grab those jobs.

64
00:04:09,180 --> 00:04:17,670
And this is where we do need to go with those acrobatics or Json jobs is equal to Json parse.

65
00:04:18,000 --> 00:04:19,620
So json.parse.

66
00:04:19,649 --> 00:04:24,210
Then we'll go with await and then read file.

67
00:04:25,060 --> 00:04:29,890
Inside of the read file, we'll provide a new URL.

68
00:04:30,510 --> 00:04:40,020
Let's go with the path in my case that is in utils and then file name is mock data and Json and then

69
00:04:40,020 --> 00:04:46,140
make sure you place this inside of the URL, not inside of the read file where you want to go with comma.

70
00:04:46,950 --> 00:04:50,190
And we want to set up as import matter.

71
00:04:50,400 --> 00:04:52,110
And you are.

72
00:04:52,590 --> 00:04:53,520
Let's save it.

73
00:04:53,520 --> 00:04:58,470
So this is going to be our list again, if you want to log it, please do.

74
00:04:58,470 --> 00:05:02,730
So In order to do that, you want to go with Node and then populate.

75
00:05:02,730 --> 00:05:06,960
But in my case, I'm going to skip that step since there are quite a few jobs.

76
00:05:06,960 --> 00:05:14,220
So essentially in here I just want to go with const jobs, then Json jobs, I want to iterate over and

77
00:05:14,220 --> 00:05:17,970
then each and every instance I'm going to reference as job.

78
00:05:18,060 --> 00:05:25,440
And then from the iteration I want to spread out all of the existing properties, basically whatever

79
00:05:25,440 --> 00:05:26,670
you have over here.

80
00:05:27,960 --> 00:05:30,420
The company position and the rest of them.

81
00:05:30,420 --> 00:05:35,610
And I just want to add that created by because remember that points to the user.

82
00:05:35,610 --> 00:05:36,180
Correct?

83
00:05:36,180 --> 00:05:39,930
So created by and we'll set it equal to user.

84
00:05:39,930 --> 00:05:42,540
So that's why we access this user over here.

85
00:05:42,540 --> 00:05:50,310
And then remember the ID property is underscore ID, so we set it up over here like this, then we save,

86
00:05:50,340 --> 00:05:55,500
then we want to go with await Job and then delete many.

87
00:05:55,860 --> 00:05:58,200
So we just want to start from the scratch.

88
00:05:58,200 --> 00:06:00,870
Now of course, with our test user, there are no jobs.

89
00:06:00,870 --> 00:06:01,930
But you know what?

90
00:06:01,950 --> 00:06:10,710
Just in case so created by and we want to set it equal to user dot underscore ID and now let's inject

91
00:06:10,710 --> 00:06:11,490
those jobs.

92
00:06:11,490 --> 00:06:19,200
So here, of course, we're looking for await job, then dot create and we pass in the array.

93
00:06:19,560 --> 00:06:22,440
And then lastly, we have log over here.

94
00:06:23,070 --> 00:06:24,480
Let's just write success.

95
00:06:25,700 --> 00:06:27,230
And let's just exit the process.

96
00:06:27,230 --> 00:06:31,400
So we'll go with Process dot Exit and we're looking for zero.

97
00:06:31,400 --> 00:06:35,270
Now, if there's some kind of error, we definitely want to log that.

98
00:06:35,540 --> 00:06:37,580
So let's set up the log.

99
00:06:38,730 --> 00:06:39,780
We're looking for error.

100
00:06:39,780 --> 00:06:44,460
And in this case, the exit is going to be with a value of one.

101
00:06:45,430 --> 00:06:46,960
Let's set it up over here.

102
00:06:47,200 --> 00:06:50,020
Let's open up the integrated terminal.

103
00:06:50,170 --> 00:06:51,850
We want to clear everything.

104
00:06:52,520 --> 00:07:00,190
So type clear and then instead of running npm run dev, we want to go with node and then the file name.

105
00:07:00,200 --> 00:07:02,150
So in this case populate.

106
00:07:02,150 --> 00:07:06,530
And if we can see the success in the console, of course we are in good shape.

107
00:07:06,530 --> 00:07:11,990
So now I added those jobs and the user is my test user.

108
00:07:12,020 --> 00:07:17,330
Now, like I said, I also want to do that with my admin one and therefore I'm going to go here with

109
00:07:17,330 --> 00:07:19,940
John at gmail.com.

110
00:07:19,940 --> 00:07:21,830
I believe that's the email I use.

111
00:07:21,860 --> 00:07:28,250
Again, let me run it one more time and if everything is correct again, I will see a success message

112
00:07:28,250 --> 00:07:29,180
in the console.

113
00:07:29,320 --> 00:07:31,130
Now of course we can nicely test it out.

114
00:07:31,130 --> 00:07:33,740
So I'm going to close this tab over here if you want.

115
00:07:33,740 --> 00:07:40,060
You can obviously check it in MongoDB, but you can also nicely see it right over here.

116
00:07:40,070 --> 00:07:46,130
Now, I do right away have the error message simply because I forgot to spin up my server.

117
00:07:46,130 --> 00:07:48,950
So let me go here with NPM run dev.

118
00:07:49,190 --> 00:07:51,080
Make sure you also do that.

119
00:07:52,160 --> 00:07:52,790
Yep.

120
00:07:52,830 --> 00:07:54,350
Looks like everything is correct.

121
00:07:54,350 --> 00:07:56,010
And check it out now.

122
00:07:56,050 --> 00:07:57,620
I'll have tons of jobs.

123
00:07:57,620 --> 00:07:58,790
And guess what?

124
00:07:58,850 --> 00:08:03,500
Of course, the owner is my test user by the name of Zippy.

125
00:08:03,530 --> 00:08:07,280
Now this is the case where I can showcase that it's not going to work.

126
00:08:07,280 --> 00:08:13,100
So if I try to delete the job, we'll have demo user read only and pretty much the same goes for edit.

127
00:08:13,130 --> 00:08:19,430
If I navigate over here and try to change something, let's say I'll try to remove some characters.

128
00:08:19,700 --> 00:08:22,550
The demo user is not able to do that.

129
00:08:22,730 --> 00:08:24,440
And lastly, I just want to showcase that.

130
00:08:24,440 --> 00:08:27,310
Of course the same is with John.

131
00:08:27,320 --> 00:08:35,720
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

132
00:08:35,720 --> 00:08:40,309
setting up the stats and complete the all jobs page.

133
00:08:40,309 --> 00:08:43,220
And with this in place we have successfully.

134
00:08:43,799 --> 00:08:52,050
Programmatically populated our database with a bunch of job instances for our admin user as well as

135
00:08:52,050 --> 00:08:53,310
the test user.

