WEBVTT

1
00:00.800 --> 00:02.390
Hi everyone.

2
00:02.420 --> 00:07.070
In the previous video we already defined the schema.

3
00:07.310 --> 00:14.120
And in this video, let's see how we can use the schema.

4
00:14.540 --> 00:18.920
Okay, to create some data inside the database.

5
00:19.370 --> 00:19.760
Okay.

6
00:19.790 --> 00:26.570
First let me reminder for you the concept of single responsibility.

7
00:26.990 --> 00:27.410
Okay.

8
00:27.410 --> 00:30.350
In the first section we already know the controller.

9
00:30.350 --> 00:32.750
It will go to service right.

10
00:32.750 --> 00:35.690
And from the service we will call into the repo.

11
00:37.760 --> 00:40.820
And now let's understand what is the repo.

12
00:40.850 --> 00:48.470
Right now when we work with the ORM.

13
00:50.870 --> 01:00.050
The ORM right here will be the repo and the ORM object relational mapping.

14
01:00.050 --> 01:01.940
Right here will be the Mongoose.

15
01:02.420 --> 01:02.810
Mongoose

16
01:02.810 --> 01:03.230
Right here.

17
01:03.890 --> 01:04.250
Okay.

18
01:04.400 --> 01:07.220
The moongose.

19
01:07.220 --> 01:08.720
Right here is the.

20
01:08.870 --> 01:10.340
Object relational mapping.

21
01:11.000 --> 01:14.210
And so for that reason, we will treat as a "repo".

22
01:14.450 --> 01:18.230
And from the service we will call inside the "repo".

23
01:18.620 --> 01:19.250
Okay.

24
01:19.280 --> 01:24.890
We will use dependency injection to call the object relational mapping.

25
01:26.300 --> 01:28.430
And now let me show you how we can do that.

26
01:31.130 --> 01:33.140
Let's scroll down a little bit.

27
01:33.350 --> 01:39.920
And right here inside the "service" we will inject okay.

28
01:39.920 --> 01:43.490
We will inject the model.

29
01:45.230 --> 01:48.020
Now instead of

30
01:48.500 --> 01:54.320
Remember I will copy also and try to understand what happened.

31
01:55.280 --> 01:59.150
I will copy and go to the service right here.

32
02:00.020 --> 02:02.730
And I will paste it inside the constructor right here.

33
02:02.730 --> 02:08.880
And I will import InjectModel from nestjs/mongoose and cat.

34
02:08.910 --> 02:12.570
I will rename to post okay.

35
02:12.990 --> 02:14.010
And the model.

36
02:14.340 --> 02:21.150
Model and the post class

37
02:28.410 --> 02:32.400
Now let me see oh sorry.

38
02:32.400 --> 02:32.790
Sorry.

39
02:33.990 --> 02:40.500
Because we must import it from the schema okay.

40
02:40.530 --> 02:42.060
And replace with schema.

41
02:42.750 --> 02:48.630
And with this syntax, we will inject okay.

42
02:48.960 --> 02:51.300
We inject the repository.

43
02:51.570 --> 02:58.920
Now let remember the how the DI work okay.

44
02:58.920 --> 03:02.490
When we bootstrapped the application.

45
03:02.520 --> 03:06.420
It has a IOC container, right?

46
03:07.740 --> 03:18.660
With IOC container right here, the framework already create the object of the cat of the post model.

47
03:19.020 --> 03:27.540
You can say, when we run the application it already have a post model inside here.

48
03:31.500 --> 03:34.080
And when from the service.

49
03:38.550 --> 03:46.410
We will look at the IOC container and go to the post model and look.

50
03:46.500 --> 03:47.460
That's, right?

51
03:48.300 --> 03:52.830
And we will inject the post model into the service.

52
03:53.550 --> 03:58.830
So for that reason this code right here it try to do that.

53
03:59.490 --> 03:59.940
Okay.

54
03:59.940 --> 03:59.950
Go.

55
03:59.980 --> 04:01.660
That's enough for you.

56
04:01.690 --> 04:03.310
Don't try to remember us.

57
04:03.910 --> 04:04.780
Is that of?

58
04:04.810 --> 04:08.950
Understand how things work is very important.

59
04:09.340 --> 04:09.820
Okay.

60
04:10.120 --> 04:11.380
Don't try to understand.

61
04:11.380 --> 04:13.240
What's the InjectModel

62
04:14.740 --> 04:15.220
Okay.

63
04:15.250 --> 04:16.960
What is the post name?

64
04:17.170 --> 04:18.220
What is the model?

65
04:18.250 --> 04:18.910
Right here.

66
04:18.970 --> 04:19.930
Model right here.

67
04:19.960 --> 04:22.120
It's not important at all.

68
04:22.870 --> 04:23.290
Okay.

69
04:23.950 --> 04:26.050
Understand how things work?

70
04:28.540 --> 04:29.200
Right here.

71
04:29.230 --> 04:30.430
Is not important.

72
04:32.080 --> 04:35.050
Instead of show, instead of.

73
04:35.080 --> 04:38.320
Let's try to understand why we do that

74
04:38.980 --> 04:42.970
Okay, now we already define

75
04:42.970 --> 04:43.990
Constructor.

76
04:45.070 --> 04:47.650
I will create public method.

77
04:48.040 --> 04:49.240
Oh, sorry for that.

78
04:49.330 --> 04:54.130
We will create a create method to create some data.

79
04:55.060 --> 04:55.420
Okay.

80
04:56.320 --> 05:01.360
And now let's see how we can create a data, let's see.

81
05:01.750 --> 05:06.340
Inside here we will use the syntax like like this right.

82
05:07.330 --> 05:14.710
It will go to the model and use something called create cat dto

83
05:14.800 --> 05:16.360
DTO okay.

84
05:16.630 --> 05:25.540
The term "DTO" right here I will show you what is "DTO" later.

85
05:25.570 --> 05:27.940
But right now don't worry about that.

86
05:28.180 --> 05:30.220
Let's see how we can create us.

87
05:30.850 --> 05:32.740
I will define the created.

88
05:33.940 --> 05:34.300
Right.

89
05:34.330 --> 05:42.610
createdPost and new this.postModel and instead of

90
05:44.350 --> 05:46.990
I will pass it with different object.

91
05:47.260 --> 05:49.930
And in the object right here we have a title.

92
05:49.960 --> 05:50.380
Right?

93
05:50.380 --> 05:52.780
Because in the post schema we have a title.

94
05:52.960 --> 05:56.380
So right here is suggest me.

95
05:57.400 --> 06:02.420
For example "post 1" and description

96
06:04.400 --> 06:05.450
Description.

97
06:06.320 --> 06:07.010
One.

98
06:07.850 --> 06:08.420
Okay.

99
06:08.510 --> 06:12.020
And I will return createdPost

100
06:15.290 --> 06:16.790
And inside the post.

101
06:16.790 --> 06:17.720
Controller.

102
06:18.560 --> 06:18.980
Okay.

103
06:19.220 --> 06:22.220
I will create a private.

104
06:26.480 --> 06:27.140
Sorry.

105
06:27.140 --> 06:33.950
We must create a constructor to inject the service post.

106
06:33.980 --> 06:36.800
Post Service

107
06:37.280 --> 06:37.610
Okay.

108
06:38.240 --> 06:40.220
That is, we can mark it.

109
06:40.220 --> 06:41.960
Read only if you want.

110
06:44.990 --> 06:47.060
With this I will create.

111
06:47.090 --> 06:49.040
Sorry guy.

112
06:50.840 --> 06:52.550
Okay, I will call.

113
06:54.980 --> 06:56.630
data = await

114
07:01.310 --> 07:06.470
Or we can return directly because we will return this.

115
07:06.680 --> 07:09.080
To use async await right here

116
07:10.340 --> 07:12.860
Okay, I will return.

117
07:13.700 --> 07:16.670
I will return this.postService.create

118
07:18.500 --> 07:18.860
Okay.

119
07:19.040 --> 07:21.680
That is we will return for the user.

120
07:21.770 --> 07:25.550
And with the controller we will rename it to the posts.

121
07:26.030 --> 07:30.980
Okay posts, this is a common way to do the rest API.

122
07:33.440 --> 07:39.800
And we will use the @Post decorator because we will create a.

123
07:40.910 --> 07:43.460
We will create data, right?

124
07:43.490 --> 07:45.500
For that reason we use the @Post method.

125
07:46.310 --> 07:49.160
And with the @Post method we can define some property.

126
07:49.550 --> 07:53.090
Sorry some URL like this.

127
07:53.480 --> 07:58.430
Because with the controller we already maked with the "posts".

128
07:59.670 --> 08:03.900
So right now we don't need it all.

129
08:03.930 --> 08:11.370
If you add, for example, like something like this, it will concatenation from the "posts" right here.

130
08:11.910 --> 08:12.480
Okay.

131
08:12.660 --> 08:17.280
It will concat it with with my string inside the @Post.

132
08:17.610 --> 08:19.320
It will concat is something like this.

133
08:20.160 --> 08:20.640
Okay.

134
08:21.150 --> 08:27.540
Hopefully you already understand what is the string right here inside the controller.

135
08:28.290 --> 08:30.090
But for now let me remove that.

136
08:31.590 --> 08:32.760
So it will use.

137
08:32.760 --> 08:35.700
Default will be "posts"

138
08:35.910 --> 08:36.420
Right.

139
08:39.750 --> 08:46.830
Now let's go to here and open the post man and create a collection.

140
08:47.010 --> 08:50.550
I will create a collection and I will rename.

141
08:50.550 --> 08:55.140
This will be the nestjs tutorial

142
08:58.680 --> 09:01.830
And I will add request.

143
09:01.860 --> 09:02.250
Okay.

144
09:02.880 --> 09:12.210
And change the methods to POST and use the http://localhost:3000

145
09:12.840 --> 09:13.470
Okay.

146
09:13.500 --> 09:17.370
And right now I will use /posts

147
09:18.810 --> 09:19.140
Okay.

148
09:19.380 --> 09:21.720
Because we already defined a "posts" over here.

149
09:21.720 --> 09:23.130
So we use /posts

150
09:23.730 --> 09:28.800
And right now we don't need to body or something like this because we already hardcode.

151
09:29.160 --> 09:32.880
We already fixed the title and description.

152
09:32.910 --> 09:33.540
Right.

153
09:33.570 --> 09:43.620
Let me send now it will show the created and something like title description.

154
09:43.620 --> 09:50.400
And because we work with MongoDB so we have a default will be underscore ID right here.

155
09:50.790 --> 10:00.100
And because we use the @Post the @Post right here, it will automatic create a status, right?

156
10:00.190 --> 10:01.090
Very cool.

157
10:04.030 --> 10:07.180
Let's check the document inside here.

158
10:07.210 --> 10:08.380
Let's check the collection.

159
10:08.650 --> 10:10.180
Let me refresh.

160
10:11.230 --> 10:14.320
Or you can click on refresh right here.

161
10:25.150 --> 10:28.660
Now you can see we don't have anything inside the posts.

162
10:29.770 --> 10:31.630
Why why that

163
10:31.630 --> 10:36.760
Because we forget return .save() method right.

164
10:37.540 --> 10:41.200
So for that reason, go to the right here and return.

165
10:41.200 --> 10:45.430
We will return .save()

166
10:47.110 --> 10:47.620
Okay.

167
10:52.660 --> 10:54.880
And .save() right here is the promise.

168
10:54.880 --> 10:58.540
So we can use a async await if you

169
11:01.090 --> 11:04.300
Know, but it's not important because we return.

170
11:04.300 --> 11:05.410
Directly right.

171
11:05.470 --> 11:09.190
It is just the normal JavaScript knowledge.

172
11:10.480 --> 11:12.160
I will not explain that.

173
11:14.050 --> 11:17.110
So because we already save, let me send again.

174
11:18.640 --> 11:23.830
Now you can see because when we save,  it has one more flag right here.

175
11:23.950 --> 11:30.400
But let let's ignore that and go to the database and I will refresh again.

176
11:36.430 --> 11:36.970
Okay.

177
11:36.970 --> 11:39.430
We already have a data right.

178
11:39.730 --> 11:40.360
Very cool.

179
11:41.170 --> 11:46.540
And that's how we can work with the database.

180
11:49.540 --> 11:52.930
So we can findAll, try to do that

181
11:54.340 --> 11:56.380
I will pause the video right here.

182
11:56.650 --> 11:59.320
And I will see you in the next one.