1
00:00:00,440 --> 00:00:03,170
Okay, so now we're going to get ready to seed our data.

2
00:00:03,170 --> 00:00:09,140
Now there's one optional package I want to install called Colors, and that just allows you to add colors

3
00:00:09,140 --> 00:00:16,250
to your terminal or to your commands, not your commands, but your output in your terminal or console.

4
00:00:16,250 --> 00:00:19,850
So we want to install this in the server dependencies.

5
00:00:19,850 --> 00:00:23,660
So make sure you're in your root and let's say NPM install colors.

6
00:00:23,660 --> 00:00:26,270
And again, this is completely optional.

7
00:00:26,690 --> 00:00:28,490
It's really easy to use though.

8
00:00:28,610 --> 00:00:34,430
So next thing we're going to do is create a file in the back end folder.

9
00:00:34,430 --> 00:00:37,310
So in back end, let's say Cedar Dot.

10
00:00:37,310 --> 00:00:42,680
JS So this is going to be the script that we run that will seed all of our data.

11
00:00:42,680 --> 00:00:45,710
And there's quite a, quite a few things we need to bring in here.

12
00:00:45,710 --> 00:00:52,160
So we're going to bring in Mongoose, we're going to bring in oops, we're going to bring in dot Env.

13
00:00:52,340 --> 00:00:59,330
And the reason for that is because we need the the mongo Uri obviously because we're importing the data.

14
00:00:59,390 --> 00:01:04,349
Just think of this, this file is completely separate from the rest of our application.

15
00:01:04,349 --> 00:01:07,170
We're not bringing it in anywhere or anything like that.

16
00:01:07,170 --> 00:01:10,050
We're just running it from the command line.

17
00:01:10,050 --> 00:01:14,580
Like literally we could say Node and then Cedar and run the file.

18
00:01:15,090 --> 00:01:18,180
I'll create a script for it, but basically that's what we're doing.

19
00:01:18,180 --> 00:01:22,500
So we have to bring everything that we're going to use in this file directly in.

20
00:01:22,650 --> 00:01:25,110
So let's bring in colors if you want.

21
00:01:25,110 --> 00:01:26,220
You don't have to.

22
00:01:26,220 --> 00:01:31,920
And we're also going to bring in the users data, which is in let's see.

23
00:01:31,920 --> 00:01:34,890
So it's going to be data and then users.

24
00:01:34,920 --> 00:01:37,170
And then we also want the products.

25
00:01:37,880 --> 00:01:45,590
Okay, then we're going to bring in the user model because we are we are entering or adding a user to

26
00:01:45,590 --> 00:01:46,340
the database.

27
00:01:46,340 --> 00:01:51,800
And any time we make queries to the database, it's going to be through a model, whether that's user

28
00:01:51,800 --> 00:01:54,080
product or whatever.

29
00:01:54,320 --> 00:01:57,380
Then we're also going to need the product model.

30
00:01:57,380 --> 00:01:59,030
So let's bring that in as well.

31
00:01:59,030 --> 00:02:01,070
And then I'm going to bring in the order model.

32
00:02:01,070 --> 00:02:06,440
Now, we're not going to be creating any orders with this cedar, but we are going to have the ability

33
00:02:06,440 --> 00:02:08,270
to wipe the database clean.

34
00:02:08,270 --> 00:02:15,590
And if there's any orders in there and we wipe the, you know, the users and the products, that's

35
00:02:15,590 --> 00:02:16,940
not going to make much sense.

36
00:02:16,940 --> 00:02:20,260
So we want to make sure we wipe everything, including any orders.

37
00:02:20,270 --> 00:02:22,460
Then we want to bring in our Connect DB.

38
00:02:22,820 --> 00:02:27,500
So that's just to connect to our database that's in config and then DB.

39
00:02:27,500 --> 00:02:33,200
JS All right, now we want to initialize our dot env so we can use those variables.

40
00:02:33,200 --> 00:02:35,390
We want to connect to our database.

41
00:02:35,600 --> 00:02:35,960
Okay.

42
00:02:35,960 --> 00:02:39,750
And then we can create our to our two functions.

43
00:02:39,750 --> 00:02:46,980
So basically we're going to have import data and destroy data and the name is pretty self-explanatory

44
00:02:46,980 --> 00:02:48,510
as to what we're doing.

45
00:02:48,510 --> 00:02:55,110
So as far as import data, let's do a try catch block here.

46
00:02:55,110 --> 00:02:58,800
And then we're going to, first of all, just delete everything.

47
00:02:58,800 --> 00:03:03,750
So before we import any products or users, we want to delete them all.

48
00:03:03,840 --> 00:03:04,060
Okay?

49
00:03:04,140 --> 00:03:06,810
So to do that, we're going to await.

50
00:03:07,380 --> 00:03:13,830
Because every every mongoose method that you call is going to return a promise.

51
00:03:13,830 --> 00:03:19,680
So we need to use async await or you could get rid of a wait and you could do, you know, delete many

52
00:03:19,710 --> 00:03:23,250
dot dot, then dot catch and so on.

53
00:03:23,250 --> 00:03:30,630
So what this will do this delete many is just that it'll delete multiple records and we're using the

54
00:03:30,630 --> 00:03:33,060
order model which we brought in up here.

55
00:03:33,060 --> 00:03:36,660
And if we don't pass anything in, it's going to delete everything.

56
00:03:36,660 --> 00:03:37,980
So we want that.

57
00:03:37,980 --> 00:03:39,420
And then we also want.

58
00:03:40,250 --> 00:03:42,830
To delete all of the products.

59
00:03:42,830 --> 00:03:50,630
So we'll use the product model, delete many and also the users, so the user model and then delete

60
00:03:50,630 --> 00:03:51,260
many.

61
00:03:52,370 --> 00:04:00,290
Okay, now we want to create our users and we're going to put the created users into a variable.

62
00:04:00,290 --> 00:04:08,660
So what's happening here is we're awaiting on user dot insert many and we're passing in users, which

63
00:04:08,660 --> 00:04:12,710
is right here coming from the data that's in that data folder, right?

64
00:04:12,710 --> 00:04:18,500
So it's going to insert those and then it's going to return that array of users to this variable.

65
00:04:18,500 --> 00:04:25,640
Now when we insert the products from products JS, they have to have a user and obviously we want the

66
00:04:25,640 --> 00:04:30,980
admin to be that user because those are the only users that can create products.

67
00:04:30,980 --> 00:04:38,510
So what we're going to do here is get the admin user, so let's say const admin user, and the way we

68
00:04:38,510 --> 00:04:44,570
can do that is just simply by taking created users, which is the three users that were created and

69
00:04:44,570 --> 00:04:45,920
get the first one.

70
00:04:45,920 --> 00:04:52,680
So the index of zero and then get that ID, that object ID, So that's the admin user.

71
00:04:52,680 --> 00:04:56,400
Now let's put a space here, just separate this out.

72
00:04:56,610 --> 00:05:02,850
And now what we want to do is insert the products.

73
00:05:02,850 --> 00:05:08,040
So we're going to have a variable that's going to hold the products that we're going to insert.

74
00:05:08,040 --> 00:05:09,870
So I'm just going to type this out.

75
00:05:09,870 --> 00:05:11,040
So sample.

76
00:05:11,580 --> 00:05:12,540
Products.

77
00:05:12,540 --> 00:05:17,820
And then what we can do is take the products that are being brought in from that file and then we're

78
00:05:17,820 --> 00:05:21,540
going to map through it and let's say for each.

79
00:05:22,240 --> 00:05:23,380
Product.

80
00:05:24,970 --> 00:05:25,180
Okay.

81
00:05:25,180 --> 00:05:28,610
So for each product and then we'll open up a code block.

82
00:05:28,630 --> 00:05:30,460
I want to return.

83
00:05:31,560 --> 00:05:41,310
The I'm going to return an object that has all that product data plus a user value of whatever that

84
00:05:41,310 --> 00:05:42,660
admin user is.

85
00:05:42,850 --> 00:05:43,030
Okay.

86
00:05:43,110 --> 00:05:44,760
Which would be this the ID?

87
00:05:45,120 --> 00:05:52,260
So it'll be, you know, an object will have all this, but in addition to that it'll have the user

88
00:05:52,260 --> 00:06:03,930
and then whatever that user ID is and we're just using the spread operator to, to add all the, all

89
00:06:03,930 --> 00:06:05,340
the product data here.

90
00:06:05,760 --> 00:06:11,220
All right, So now we haven't entered anything into the database, but we have this variable of sample

91
00:06:11,220 --> 00:06:13,800
products that we want to enter to the database.

92
00:06:13,800 --> 00:06:21,500
So the next step would be to call, insert many on the product model and just pass in the sample products.

93
00:06:21,510 --> 00:06:30,030
So at that point we'll have the users inserted from this line, we get the admin, we create a variable

94
00:06:30,030 --> 00:06:36,880
with all the products, including the admin user, and then we insert all that into the insert all the

95
00:06:36,880 --> 00:06:38,440
products into the database.

96
00:06:38,440 --> 00:06:42,700
So then we'll just do a console log and we'll just say data.

97
00:06:43,650 --> 00:06:44,730
Imported.

98
00:06:44,730 --> 00:06:49,680
And I'm going to add this after the string dot Green Inverse.

99
00:06:49,680 --> 00:06:52,680
So this is where that colors package comes in.

100
00:06:53,010 --> 00:06:53,580
Okay.

101
00:06:53,760 --> 00:06:59,250
And it's a little weird because we do have to bring it in as colors, but we don't actually use colors

102
00:06:59,250 --> 00:06:59,820
anywhere.

103
00:06:59,820 --> 00:07:02,220
But it allows us to do stuff like this.

104
00:07:02,220 --> 00:07:05,700
And if you look at the documentation, you can see all the cool things you can do.

105
00:07:05,730 --> 00:07:13,530
You can do like text color, background colors, you can do strikethroughs and just all kinds of cool

106
00:07:13,530 --> 00:07:14,250
stuff.

107
00:07:14,370 --> 00:07:16,650
So then we just want to exit the process.

108
00:07:16,650 --> 00:07:22,200
So process exit, we don't want to kill it, so we're not going to pass a one in.

109
00:07:22,200 --> 00:07:23,520
Just leave it like that.

110
00:07:23,520 --> 00:07:29,460
And then if there's an error, then we're going to console log the error and I'm going to add red inverse.

111
00:07:29,460 --> 00:07:34,410
So that gives it a red background and that will close with with error.

112
00:07:35,700 --> 00:07:36,180
All right.

113
00:07:36,180 --> 00:07:38,670
So that's import.

114
00:07:38,700 --> 00:07:40,590
Now we want to destroy.

115
00:07:40,590 --> 00:07:43,110
And this, this one, this function is easier.

116
00:07:43,110 --> 00:07:52,080
So let's say const destroy data and we're going to set that to an asynchronous function.

117
00:07:52,080 --> 00:07:54,810
And then what we'll do is have a try catch.

118
00:07:54,810 --> 00:07:59,550
And in the try we're just going to simply call delete many again, just like we did up here.

119
00:08:00,270 --> 00:08:05,130
Okay, so before we did everything here, we deleted anything that's in the database in those three

120
00:08:05,130 --> 00:08:07,170
collections and then that's it.

121
00:08:07,200 --> 00:08:11,310
We're just going to have a console log that says Data destroyed will exit.

122
00:08:11,340 --> 00:08:15,090
If something goes wrong, we'll have an error and then we'll exit.

123
00:08:15,360 --> 00:08:19,410
Now, how do we run these methods?

124
00:08:19,410 --> 00:08:19,720
Right?

125
00:08:19,740 --> 00:08:26,670
Because we can run the file easily by saying node and then back end slash.

126
00:08:27,300 --> 00:08:29,790
Cedar, but I want to be able to.

127
00:08:30,520 --> 00:08:34,289
To choose which which one of these I run.

128
00:08:34,299 --> 00:08:39,789
So the way I want it to work is if I just run the file as is, then I want to run the import.

129
00:08:39,820 --> 00:08:47,530
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

130
00:08:47,530 --> 00:08:51,760
for this, this argument and the way we do that.

131
00:08:51,760 --> 00:08:52,660
Let's test this out.

132
00:08:52,660 --> 00:08:57,160
I'm just going to comment, basically just comment out everything for now, just so I can show you how

133
00:08:57,160 --> 00:08:57,970
to get this.

134
00:08:58,000 --> 00:08:59,860
And just in case you guys don't know.

135
00:08:59,860 --> 00:09:07,630
So if we console.log process dot argv, that's going to be the arguments that are passed in.

136
00:09:07,630 --> 00:09:12,280
So if I save this and then I run this file with this dash D.

137
00:09:13,360 --> 00:09:16,810
You'll see it actually gives me an array with three strings.

138
00:09:16,810 --> 00:09:23,650
So the first two are going to be these paths, the bin slash node, and then this path here.

139
00:09:23,650 --> 00:09:28,930
And then the third item, which is the two index is whatever I passed in.

140
00:09:28,930 --> 00:09:37,510
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

141
00:09:37,540 --> 00:09:38,920
one, two, three.

142
00:09:39,040 --> 00:09:45,640
So to get the, the dash d we could just grab the index of two.

143
00:09:45,820 --> 00:09:46,240
Right.

144
00:09:46,240 --> 00:09:51,580
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

145
00:09:51,580 --> 00:09:53,740
now it's going to console log dash.

146
00:09:53,740 --> 00:09:54,480
Hello.

147
00:09:54,490 --> 00:09:59,890
So knowing that let's go ahead and do an if statement here.

148
00:10:01,010 --> 00:10:02,840
And let's say.

149
00:10:03,890 --> 00:10:16,160
If the process dot argv and we want to test for the the index of two if that is equal to a string of

150
00:10:16,160 --> 00:10:17,450
dash D.

151
00:10:18,920 --> 00:10:23,180
Okay, So if that's equal to Dash D, then we're going to call destroy data.

152
00:10:23,300 --> 00:10:27,240
Else then we're going to call import data.

153
00:10:27,260 --> 00:10:34,850
If you wanted to say, well, if it's dash, I then import data, that's fine as well, but we're just

154
00:10:34,850 --> 00:10:40,130
going to have it so that if you run it and you don't pass in Dash D, then it's going to import.

155
00:10:41,470 --> 00:10:45,610
Okay, now let's uncomment this, all this.

156
00:10:46,060 --> 00:10:50,080
And before I run it, I don't actually want to run it like this.

157
00:10:50,080 --> 00:10:53,230
I want to create a script to do that.

158
00:10:53,230 --> 00:11:02,410
So in our package.json in The Root, let's go up to where we have our scripts and let's see, we have.

159
00:11:03,500 --> 00:11:05,570
Start server client.

160
00:11:05,570 --> 00:11:06,390
Dev, let's.

161
00:11:06,410 --> 00:11:08,390
I guess we'll just go right under Dev.

162
00:11:08,390 --> 00:11:14,930
So let's put a comma here and then this is going to be, let's call it data colon import.

163
00:11:15,450 --> 00:11:17,400
And what that's going to run.

164
00:11:18,030 --> 00:11:25,380
Is going to be that just the file node and then back end slash cedar.

165
00:11:25,440 --> 00:11:32,010
Dot JS Right, and then let's do this data colon.

166
00:11:33,130 --> 00:11:39,100
Destroy and we want it to still run that file, but we want it to add on the dash D.

167
00:11:40,450 --> 00:11:41,620
So let's save that.

168
00:11:41,620 --> 00:11:47,520
And now we can simply do NPM run data import or NPM run data destroy.

169
00:11:47,530 --> 00:11:48,910
So let's test it out.

170
00:11:48,910 --> 00:11:52,840
NPM run data Colon import.

171
00:11:56,130 --> 00:11:56,610
All right.

172
00:11:56,610 --> 00:12:01,890
So you saw that it connected to the database and we get data imported now to check that you can either

173
00:12:01,920 --> 00:12:05,010
go to Atlas or you can go to Compass.

174
00:12:05,040 --> 00:12:06,750
Now you're going to have to reload this.

175
00:12:06,750 --> 00:12:11,700
So in the menu bar, you'll see a view option and just say reload data.

176
00:12:11,820 --> 00:12:14,190
And now check it out.

177
00:12:14,190 --> 00:12:21,180
We have we have orders, products and users and this is the products, has an ID has a user.

178
00:12:21,180 --> 00:12:26,160
So that object ID refers to a user in the users collection.

179
00:12:26,900 --> 00:12:30,500
And then it has everything else it has created at and updated at.

180
00:12:30,500 --> 00:12:35,870
Because in our schema we said to use timestamps reviews is an array.

181
00:12:35,870 --> 00:12:43,480
There's nothing in there, but it is in fact an array, the rating, the name, image, all that stuff.

182
00:12:43,490 --> 00:12:48,080
So now we have six products if we look in users.

183
00:12:48,380 --> 00:12:53,150
So here's our admin user, that's the ID, the email and check the password out.

184
00:12:53,150 --> 00:12:54,260
It's hashed.

185
00:12:54,710 --> 00:12:55,070
Okay.

186
00:12:55,070 --> 00:12:57,860
The password for all of them is one, two, three, four, five, six.

187
00:12:57,860 --> 00:13:02,120
But we only store the hash in the database is admin is true.

188
00:13:02,120 --> 00:13:07,700
And then we have our timestamps and you can see is admin is false for these other ones.

189
00:13:08,590 --> 00:13:09,290
So that's it.

190
00:13:09,310 --> 00:13:16,750
Now, if I want to clear the database out, I can simply do NPM run data destroy.

191
00:13:19,450 --> 00:13:20,860
Okay, so data destroyed.

192
00:13:20,860 --> 00:13:27,610
Now, if I go back to Compass and I go back up to view and then reload data, I have nothing in my database.

193
00:13:27,610 --> 00:13:30,160
I have the collections, but there's nothing in them.

194
00:13:31,110 --> 00:13:32,280
So pretty cool.

195
00:13:32,280 --> 00:13:34,050
And this this will be very helpful.

196
00:13:34,050 --> 00:13:38,490
Now, I do want the data there, so I'm going to once again just run the data import.

197
00:13:41,120 --> 00:13:45,770
So this is really good for just prototyping and testing.

198
00:13:45,920 --> 00:13:48,770
So that whoops, I think I disconnected.

199
00:13:50,170 --> 00:13:50,740
Um.

200
00:13:51,850 --> 00:13:54,170
See connect.

201
00:13:55,050 --> 00:13:55,350
All right.

202
00:13:55,350 --> 00:13:59,280
So if I go to pro shop and products, there we go.

203
00:13:59,280 --> 00:14:01,650
So we have our products and users in there.

204
00:14:02,740 --> 00:14:03,640
Okay, so that's it.

205
00:14:03,640 --> 00:14:12,880
Now, in the next video, I'm going to introduce you to Postman, which is an Http tool client to make

206
00:14:12,880 --> 00:14:17,170
Http requests, and that's what we'll be using to make requests to our back end.

207
00:14:17,170 --> 00:14:20,680
While we while we don't have any front end to use.

