1
00:00:00,290 --> 00:00:04,610
Okay, so now we're going to model our data and I'm going to just do all of it now.

2
00:00:04,610 --> 00:00:10,220
So all the collections products, orders, users, we're going to create a model file for all of them

3
00:00:10,220 --> 00:00:14,360
rather than just doing products now and then doing orders later.

4
00:00:14,360 --> 00:00:16,360
Let's just get it all out of the way.

5
00:00:16,370 --> 00:00:24,740
So with MongoDB and with most NoSQL databases, they're very flexible, meaning that unlike with relational

6
00:00:24,740 --> 00:00:30,170
databases, you don't have to go in on the database level and declare all of your fields and all of

7
00:00:30,170 --> 00:00:35,840
your all the types for those fields like you would with with something like Postgres or MySQL.

8
00:00:35,840 --> 00:00:41,060
However, we do want some structure, so we do it on the application level, which is what Mongoose

9
00:00:41,090 --> 00:00:42,020
is used for.

10
00:00:42,020 --> 00:00:46,250
So in the back end, let's create a folder called Models.

11
00:00:46,250 --> 00:00:53,030
And then in models we're going to create a product model dot JS file.

12
00:00:53,270 --> 00:00:57,800
Okay, so this is where we want to create our schema and export it as a model.

13
00:00:57,800 --> 00:01:07,100
So first off, we need to import Mongoose and then we'll create a variable called product schema.

14
00:01:07,960 --> 00:01:14,860
And inside this new Mongoose schema will be an object with all the fields that we want.

15
00:01:14,890 --> 00:01:18,790
Now we're going to have a lot of fields here.

16
00:01:18,790 --> 00:01:22,090
I might paste some in, but I'll start off just typing them.

17
00:01:22,090 --> 00:01:26,140
So we're going to have a name and you can actually do something like this.

18
00:01:26,140 --> 00:01:30,400
Like you can say name String if you don't want to do anything else to it.

19
00:01:30,400 --> 00:01:38,980
Like if you don't care if it's required or if there's any kind of default value or anything like that,

20
00:01:38,980 --> 00:01:45,340
you can just define the type or you can make it an object and then you can say type string.

21
00:01:45,550 --> 00:01:52,660
Okay, Now the reason I'm doing this is because let's get rid of that is because the name I do want

22
00:01:52,660 --> 00:01:54,070
it to be required.

23
00:01:54,070 --> 00:01:59,020
So I also want to add the value for required to be true.

24
00:01:59,110 --> 00:01:59,400
All right.

25
00:01:59,410 --> 00:02:00,220
So that's the name.

26
00:02:00,220 --> 00:02:03,040
Next, we're going to have the image.

27
00:02:03,040 --> 00:02:06,130
So the image is just going to be a string as well.

28
00:02:06,130 --> 00:02:09,770
So type, string and that's also going to be required.

29
00:02:09,770 --> 00:02:14,840
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

30
00:02:14,840 --> 00:02:15,440
image.

31
00:02:15,440 --> 00:02:22,040
But either way it will have an image and the actual image gets stored in the file structure.

32
00:02:22,040 --> 00:02:26,570
But the name of the image, the location is going to get stored here.

33
00:02:27,020 --> 00:02:32,960
So we're also going to have the brand, which is going to be the type string.

34
00:02:33,230 --> 00:02:35,600
We'll make that required as well.

35
00:02:36,340 --> 00:02:41,710
And then let's do category type string required.

36
00:02:41,740 --> 00:02:42,440
True.

37
00:02:42,460 --> 00:02:45,700
So we're basically requiring pretty much everything.

38
00:02:45,700 --> 00:02:47,710
And then we want a description.

39
00:02:50,230 --> 00:02:56,380
And the reason copilot knows what usually knows what I'm looking for is because it's going by the first

40
00:02:56,380 --> 00:03:01,470
version of the app that I've had on GitHub for a long time, and it's pretty popular.

41
00:03:01,480 --> 00:03:05,890
So after description, we're going to have reviews.

42
00:03:05,920 --> 00:03:12,280
Now reviews is actually going to be another schema and we could put that in a separate file, but we'll

43
00:03:12,280 --> 00:03:13,810
just put it in this file.

44
00:03:13,810 --> 00:03:16,270
So what I mean by schema is what we're doing now.

45
00:03:16,270 --> 00:03:23,470
It'll have a schema with certain fields, so I'm just going to have some brackets and then we're going

46
00:03:23,470 --> 00:03:31,240
to say review schema, which we'll create after now the rating, let's say rating, that's going to

47
00:03:31,240 --> 00:03:38,680
be the type of number it's going to be required and it's going to have a default of zero.

48
00:03:39,160 --> 00:03:39,850
Okay.

49
00:03:39,970 --> 00:03:46,600
And then we also want the Num reviews, So the number of reviews, that's also going to be number and

50
00:03:46,600 --> 00:03:50,810
that's going to be required and that's going to be zero by default as well.

51
00:03:50,840 --> 00:03:55,730
Then we'll have the price, which will be a number required.

52
00:03:55,760 --> 00:03:56,330
True.

53
00:03:56,330 --> 00:03:59,180
And we'll do default zero for that.

54
00:03:59,180 --> 00:04:02,870
And then finally we'll have the count in stock.

55
00:04:02,960 --> 00:04:08,090
So number required true default zero for that as well.

56
00:04:08,570 --> 00:04:08,870
All right.

57
00:04:08,870 --> 00:04:10,340
So those are the fields that we want.

58
00:04:10,340 --> 00:04:11,660
Actually, we want one more.

59
00:04:11,660 --> 00:04:13,610
And I want to save this for last.

60
00:04:13,610 --> 00:04:15,020
That's the user.

61
00:04:15,050 --> 00:04:15,440
Okay.

62
00:04:15,440 --> 00:04:21,560
So every product needs to be connected to a user that needs to be a relationship because you need to

63
00:04:21,560 --> 00:04:23,270
know which user.

64
00:04:23,270 --> 00:04:27,680
In this case it would be an admin user which one added that product.

65
00:04:27,860 --> 00:04:28,190
All right.

66
00:04:28,190 --> 00:04:33,770
So what we will do is and I usually like to put this at the top so it'll be a user.

67
00:04:34,580 --> 00:04:40,640
And as far as the type, it's going to be a mongoose object ID.

68
00:04:41,180 --> 00:04:48,350
So any time we create anything in the database, it has an underscore ID field and that's the object

69
00:04:48,350 --> 00:04:48,740
ID.

70
00:04:49,040 --> 00:04:51,230
So and that has its own type.

71
00:04:51,230 --> 00:04:54,680
So that's what we're giving this and it's going to be required.

72
00:04:54,680 --> 00:05:01,220
And then we need to we need to specify where which collection this is coming from, this object ID,

73
00:05:01,220 --> 00:05:03,320
and it's going to be from the user collection.

74
00:05:03,320 --> 00:05:05,690
So the reference, I should say.

75
00:05:06,170 --> 00:05:06,500
All right.

76
00:05:06,530 --> 00:05:08,570
And that should do it for user.

77
00:05:09,230 --> 00:05:10,130
Now.

78
00:05:10,130 --> 00:05:16,910
I also want time stamps, meaning I want to create, adapt, and modify that field so we could do those

79
00:05:16,910 --> 00:05:18,110
manually like these.

80
00:05:18,110 --> 00:05:28,160
Or we can just go under this last, this last curly brace and we could put in another set of curly braces

81
00:05:28,160 --> 00:05:29,720
and just say time stamps.

82
00:05:29,720 --> 00:05:30,440
True.

83
00:05:30,440 --> 00:05:34,190
So that will automatically add the createdat fields.

84
00:05:34,700 --> 00:05:35,030
All right.

85
00:05:35,030 --> 00:05:43,130
So then we'll just say const, I'm just going to say product and we're going to set that to mongoose

86
00:05:43,130 --> 00:05:46,460
dot model and we're going to pass in product.

87
00:05:46,490 --> 00:05:48,290
Okay, So that's the model we're creating.

88
00:05:48,290 --> 00:05:52,460
And then the schema that we want to use is the one that we just created.

89
00:05:52,460 --> 00:05:55,850
And then finally, I'm just going to export default.

90
00:05:56,520 --> 00:05:57,600
Product.

91
00:05:58,650 --> 00:05:58,980
All right.

92
00:05:59,010 --> 00:06:04,410
Now, before we leave this file, we do have to add the review schema, because right now that doesn't

93
00:06:04,410 --> 00:06:05,220
exist.

94
00:06:05,220 --> 00:06:09,830
So let's go ahead and put this right above the product screen schema.

95
00:06:09,840 --> 00:06:11,340
So we'll say review schema.

96
00:06:11,340 --> 00:06:18,930
We create it just like we did the product schema and it's going to have a name which will be string

97
00:06:18,930 --> 00:06:22,110
and required and then it's going to have a rating.

98
00:06:23,920 --> 00:06:32,470
Number required and it's going to have a comment and that's going to be type string required.

99
00:06:32,470 --> 00:06:39,550
And that's also going to have a user because we need to know which user is which user created it, right?

100
00:06:39,550 --> 00:06:41,740
So I'll just copy this.

101
00:06:42,850 --> 00:06:45,640
And I'll put that at the top of this as well.

102
00:06:46,560 --> 00:06:52,620
Okay, Because obviously the user of that creates the product is not the same user in the review.

103
00:06:52,650 --> 00:06:55,920
I mean, I guess it could actually it couldn't because we're going to make it.

104
00:06:55,920 --> 00:06:59,460
So you can't review the your own product.

105
00:07:00,200 --> 00:07:00,560
All right.

106
00:07:00,560 --> 00:07:03,110
And then let's also do time stamps here as well.

107
00:07:03,110 --> 00:07:04,280
So right here.

108
00:07:04,930 --> 00:07:07,190
I'll just go like that and say timestamps.

109
00:07:07,190 --> 00:07:07,820
True.

110
00:07:08,760 --> 00:07:11,100
So that's our model for our product.

111
00:07:11,130 --> 00:07:13,850
Now let's do our users.

112
00:07:13,860 --> 00:07:16,290
So let's create another file.

113
00:07:16,290 --> 00:07:18,000
We'll call it user.

114
00:07:18,690 --> 00:07:21,090
Model dot JS.

115
00:07:22,240 --> 00:07:24,340
I did singular for Productrillionight.

116
00:07:24,340 --> 00:07:24,760
Yeah.

117
00:07:24,760 --> 00:07:25,660
So I like to keep.

118
00:07:25,750 --> 00:07:28,060
I like to make the models singular.

119
00:07:28,480 --> 00:07:32,500
So the user is model or schema is pretty simple.

120
00:07:32,500 --> 00:07:34,300
So let's just import.

121
00:07:35,210 --> 00:07:40,190
Mongoose and then say const user schema.

122
00:07:41,580 --> 00:07:44,160
Set that to a new schema.

123
00:07:45,130 --> 00:07:49,000
And we're going to have a name for the user which will be required.

124
00:07:49,000 --> 00:07:50,680
We're going to have an email.

125
00:07:52,270 --> 00:07:54,580
And emails are going to be unique.

126
00:07:55,430 --> 00:07:57,140
And then password.

127
00:07:59,000 --> 00:07:59,300
Okay.

128
00:07:59,300 --> 00:08:01,610
And the password obviously will be hashed.

129
00:08:01,610 --> 00:08:05,240
It's not going to be just a, you know, a plain text password.

130
00:08:05,240 --> 00:08:08,150
And then we also want to know if the user is an admin.

131
00:08:08,150 --> 00:08:12,620
So that's going to be a Boolean value and the default is going to be false.

132
00:08:13,220 --> 00:08:18,560
So in order to make an admin, we specifically have to go into the database and change that.

133
00:08:20,060 --> 00:08:22,550
Change this value to true.

134
00:08:23,190 --> 00:08:25,230
And then we'll do time stamps as well.

135
00:08:25,230 --> 00:08:26,220
So right here.

136
00:08:26,250 --> 00:08:28,170
Oops, Right here.

137
00:08:29,220 --> 00:08:30,510
Let's say time stamps.

138
00:08:30,510 --> 00:08:31,110
True.

139
00:08:31,110 --> 00:08:34,110
And then const user.

140
00:08:35,960 --> 00:08:36,919
And there we go.

141
00:08:36,919 --> 00:08:41,270
And then we just want to export as default user.

142
00:08:42,130 --> 00:08:43,990
Okay, so that's our user model.

143
00:08:45,100 --> 00:08:53,110
Now, as far as orders go, there's quite a bit of data here I might paste in some, but let's create

144
00:08:53,110 --> 00:08:55,180
a new file called Order.

145
00:08:55,770 --> 00:08:57,810
Model JS.

146
00:08:58,850 --> 00:09:01,850
And let's import Mongoose.

147
00:09:03,420 --> 00:09:04,680
Let's create.

148
00:09:05,930 --> 00:09:07,670
The order schema.

149
00:09:11,660 --> 00:09:15,620
And we're going to have a user just like we did for the other things.

150
00:09:17,510 --> 00:09:17,780
Okay.

151
00:09:17,780 --> 00:09:19,790
And then we're going to have order items.

152
00:09:19,790 --> 00:09:23,060
Now Order items is going to be an array.

153
00:09:23,750 --> 00:09:25,460
So we're going to use.

154
00:09:28,830 --> 00:09:29,970
Actually, let me just.

155
00:09:31,670 --> 00:09:32,630
Yeah, we'll do that.

156
00:09:32,630 --> 00:09:34,790
So it's going to be an array of objects.

157
00:09:34,790 --> 00:09:42,350
So let's close that off and each order item will have a name, which will be a type of string.

158
00:09:42,350 --> 00:09:44,480
It's going to have a quantity.

159
00:09:45,410 --> 00:09:47,270
We're going to have an image.

160
00:09:48,430 --> 00:09:55,600
A price, which will be a number, and then product, which is going to be a reference to the product

161
00:09:55,600 --> 00:09:58,150
in the products collection are the products model.

162
00:09:58,150 --> 00:10:02,410
So again, it's going to be the type of object ID required.

163
00:10:02,410 --> 00:10:07,600
True, except the ref is going to be product, not user, because now we're referencing a product.

164
00:10:07,870 --> 00:10:18,070
So having the the square brackets tells Mongoose that this is this is going to be an array with these

165
00:10:18,100 --> 00:10:19,020
objects.

166
00:10:19,030 --> 00:10:22,720
If I didn't have the brackets, then it would just be a single object.

167
00:10:22,720 --> 00:10:29,980
But we want obviously the the user is going to have or could have more than one item ordered.

168
00:10:29,980 --> 00:10:31,870
So we want to make that an array.

169
00:10:33,170 --> 00:10:33,500
All right.

170
00:10:33,500 --> 00:10:35,510
We could make it another schema, but we'll just.

171
00:10:35,510 --> 00:10:36,230
We'll just do that.

172
00:10:36,230 --> 00:10:37,180
That's fine.

173
00:10:37,190 --> 00:10:39,050
So now let's put a comma here.

174
00:10:39,050 --> 00:10:44,870
The next thing we want is the shipping address, which is going to be it's actually going to be an object

175
00:10:44,870 --> 00:10:51,680
as well that will have the address, the city, the postal code and the country.

176
00:10:52,880 --> 00:10:54,830
Okay, so that will be the shipping address.

177
00:10:54,830 --> 00:10:59,090
Then we'll have the payment method that's going to be type string required.

178
00:10:59,120 --> 00:10:59,990
True.

179
00:11:00,320 --> 00:11:07,070
Then we have the actually this copilot is really helping as far as typing, I don't have to copy and

180
00:11:07,070 --> 00:11:09,410
paste, but I also don't have to type it all out.

181
00:11:09,970 --> 00:11:19,650
So payment result that's going to have an ID and a status and then the update time and then an email

182
00:11:20,920 --> 00:11:22,510
email address.

183
00:11:23,560 --> 00:11:23,980
All right.

184
00:11:23,980 --> 00:11:29,860
So basically, when it goes through PayPal and they pay it, this will get updated the status.

185
00:11:30,880 --> 00:11:36,220
All right, then we want the let's do the items price first or item.

186
00:11:38,060 --> 00:11:39,340
Item's price.

187
00:11:39,350 --> 00:11:41,690
So that's going to be type number.

188
00:11:42,020 --> 00:11:47,150
It's going to be required and the default is going to be 0.0.

189
00:11:47,190 --> 00:11:51,650
I'm putting that because the prices are going to be formatted as decimals.

190
00:11:52,600 --> 00:11:55,000
I mean, there's still numbers in JavaScript.

191
00:11:55,000 --> 00:12:01,360
You don't have a float or a decimal type, but they're going to be formatted like that.

192
00:12:02,030 --> 00:12:04,130
Then we have the tax price.

193
00:12:04,490 --> 00:12:08,480
So same thing number default is going to be 0.0.

194
00:12:08,510 --> 00:12:11,390
Then we'll have the shipping price.

195
00:12:14,320 --> 00:12:15,340
Same thing.

196
00:12:15,340 --> 00:12:17,200
And then let's do.

197
00:12:18,320 --> 00:12:20,120
The total price.

198
00:12:20,120 --> 00:12:22,100
So this will be all of it together.

199
00:12:23,660 --> 00:12:25,610
And then let's say is paid.

200
00:12:25,610 --> 00:12:28,940
So it will have a status of is paid, which will be a Boolean.

201
00:12:29,460 --> 00:12:35,190
And then it'll also have a status of, well, let's do paid at first.

202
00:12:35,960 --> 00:12:39,830
So paid at will be a date and then we'll have is delivered.

203
00:12:39,830 --> 00:12:44,570
So if it's shipped and delivered, then this will be a flag to tell us that.

204
00:12:45,200 --> 00:12:49,730
And then we'll also have a delivered at which will also be type date.

205
00:12:50,470 --> 00:12:51,520
And that's it.

206
00:12:51,550 --> 00:12:53,710
We just want to add timestamps as well.

207
00:12:53,710 --> 00:12:55,150
So we'll go right here.

208
00:12:57,170 --> 00:12:57,500
Okay.

209
00:12:57,500 --> 00:12:58,490
Just make sure you do that.

210
00:12:58,490 --> 00:13:00,140
Don't do this time.

211
00:13:00,140 --> 00:13:00,650
Stamps.

212
00:13:00,650 --> 00:13:01,850
Don't put it there.

213
00:13:02,030 --> 00:13:02,420
That's.

214
00:13:02,540 --> 00:13:04,430
That's a mistake that I've made a lot.

215
00:13:05,250 --> 00:13:05,580
All right.

216
00:13:05,580 --> 00:13:10,990
So now we just want to create the variable order and set the model.

217
00:13:11,010 --> 00:13:14,460
We're using the order schema and then we'll export it.

218
00:13:17,030 --> 00:13:21,170
Okay, so now we should have all of our data modeled.

219
00:13:21,200 --> 00:13:24,890
So what I want to do next is prepare some sample data.

220
00:13:24,890 --> 00:13:29,330
So basically I want to have some products in the database.

221
00:13:29,330 --> 00:13:34,640
They're actually going to be the same products that we have here, but I want them in the database along

222
00:13:34,640 --> 00:13:38,060
with some users because a product belongs to a user.

223
00:13:38,060 --> 00:13:40,760
So if we add a product, we need to add some users.

224
00:13:40,760 --> 00:13:49,640
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,

225
00:13:49,640 --> 00:13:54,890
a file that we can run that will import all of our products and users for us.

226
00:13:54,890 --> 00:13:56,960
So we'll get started on that next.

