WEBVTT

1
00:00.650 --> 00:02.360
Hi everyone.

2
00:02.780 --> 00:06.260
Let's understand the pipes.

3
00:08.450 --> 00:11.480
Into the core concept of Nestjs.

4
00:12.590 --> 00:13.100
Okay.

5
00:13.460 --> 00:16.700
The pipe is a class within the injectable.

6
00:16.730 --> 00:19.160
It means it's a dependency injection.

7
00:22.550 --> 00:22.970
Okay.

8
00:22.970 --> 00:25.370
It implement from PipeTransform interface

9
00:25.700 --> 00:27.650
but ignore that.

10
00:28.520 --> 00:34.250
Let me show you the idea of the pipes.

11
00:34.640 --> 00:39.050
The pipe, it's have two use cases

12
00:39.650 --> 00:42.980
You can see, the first one will be transformation.

13
00:43.070 --> 00:47.750
It can be transform the input into the some output.

14
00:47.780 --> 00:50.600
For example, from string to integer.

15
00:51.770 --> 00:53.900
It has some validation rule.

16
00:54.980 --> 01:00.980
Okay, now let me show you something like this.

17
01:01.520 --> 01:07.040
And notice about built-in pipe, in NestJS.

18
01:07.070 --> 01:08.120
It has a nine.

19
01:08.300 --> 01:10.310
9 pipes

20
01:14.180 --> 01:16.100
Something like validation.

21
01:16.130 --> 01:21.620
Okay, but before understand pipes, let me show you.

22
01:21.620 --> 01:22.490
What is the @Get(':id')

23
01:23.030 --> 01:25.790
right here and @Param right here

24
01:25.790 --> 01:26.720
What is mean?

25
01:27.080 --> 01:27.500
Okay.

26
01:29.240 --> 01:33.080
After we understand that, we will understand the pipes.

27
01:33.620 --> 01:34.490
Let me show you.

28
01:34.520 --> 01:38.120
For example, inside the post controller.

29
01:38.870 --> 01:40.400
Inside the post controller.

30
01:41.330 --> 01:41.600
Okay.

31
01:41.600 --> 01:47.510
Let me collapse the code right here, inside the post controller.

32
01:50.180 --> 02:01.460
For example, I will create one more method I will call method will be the getPost, and with the

33
02:01.460 --> 02:05.750
id okay, it means I want to get the post.

34
02:05.750 --> 02:05.780
...

35
02:05.810 --> 02:17.330
For example, if I go to the postman and "/1" right here, I want it will pass the "1" inside

36
02:17.360 --> 02:23.060
the controller and find that for me, how we can do that.

37
02:23.660 --> 02:31.490
Firstly, in order to use that, to access the "id" parameter, we must.

38
02:31.520 --> 02:39.470
We must use the @Param id: number, for example like this.

39
02:40.340 --> 02:42.950
Now we can see, I will use the @Param.

40
02:45.800 --> 02:46.160
Right.

41
02:46.430 --> 02:50.030
It will be the "id" and make sure here and here will be the same.

42
02:52.190 --> 03:03.740
And "id" okay, we can pass this something like this if you want, but I will say the id: number

43
03:05.570 --> 03:07.100
and you will getOne.

44
03:08.510 --> 03:10.490
And now let me console.log.

45
03:10.490 --> 03:12.890
What is inside the "id" for you?

46
03:13.370 --> 03:14.480
check post

47
03:14.510 --> 03:14.990
id

48
03:16.610 --> 03:16.940
id

49
03:23.030 --> 03:29.120
If I send  and the check post id will be 1 because I passed the 1 right here.

50
03:29.690 --> 03:30.080
Two.

51
03:31.040 --> 03:31.550
Okay.

52
03:31.730 --> 03:32.660
It will be the post.

53
03:32.690 --> 03:33.230
Two.

54
03:34.070 --> 03:34.550
Okay.

55
03:34.580 --> 03:35.300
That's the param

56
03:39.380 --> 03:40.580
And.

57
03:43.130 --> 03:53.510
For now, let me write the code for post service, if I go to post service right here and I will.

58
03:53.600 --> 03:55.640
I will remove the throw.

59
03:56.300 --> 04:03.620
I will pass the get one and I will accept within the "id".

60
04:03.650 --> 04:05.690
I will take the "id", right?

61
04:06.140 --> 04:07.220
It will be the number.

62
04:07.550 --> 04:10.490
And I will use the data.

63
04:11.270 --> 04:14.690
.find will be "post"

64
04:15.650 --> 04:16.880
equal post

65
04:16.910 --> 04:19.610
dot id

66
04:19.640 --> 04:21.290
The "id" from parameter.

67
04:21.830 --> 04:22.400
Okay.

68
04:22.790 --> 04:26.270
After we find us, we will return.

69
04:27.620 --> 04:28.850
We will return the data.

70
04:28.880 --> 04:32.510
This is clearly the business logic.

71
04:33.170 --> 04:36.290
Actually I must do that in the repository.

72
04:36.770 --> 04:42.230
But don't worry it for now, I will use get one.

73
04:42.710 --> 04:46.670
Get one and I will pass the "id"

74
04:47.330 --> 04:49.490
Now let's see.

75
04:55.130 --> 04:55.760
If I send.

76
04:58.850 --> 05:01.910
The word two actually is not work.

77
05:23.710 --> 05:24.580
Let me see.

78
05:31.090 --> 05:35.740
Now, the reason it not work because.

79
05:37.900 --> 05:44.140
if I change the "===" into "=="

80
05:44.710 --> 05:45.280
Okay.

81
05:46.720 --> 05:48.910
If I send, sorry.

82
05:49.030 --> 05:53.200
If I send it works, why?

83
05:53.590 --> 05:55.960
Why's that? It's a little bit weird

84
05:56.050 --> 05:56.710
Right?

85
05:57.190 --> 05:58.810
You can see this is the number.

86
05:58.930 --> 06:00.250
This is the number.

87
06:00.280 --> 06:02.350
But why it not work?

88
06:04.060 --> 06:07.660
Let me tell you the reason why it not work, firstly

89
06:07.690 --> 06:15.250
I will console.log typeof "id", can you guess it is string or number?

90
06:16.660 --> 06:18.910
now it's, I guess.

91
06:18.940 --> 06:20.800
It's clearly a string.

92
06:21.490 --> 06:24.400
You can see this is clearly a string.

93
06:26.350 --> 06:27.700
And the typescript right here.

94
06:27.700 --> 06:28.870
It doesn't check.

95
06:29.980 --> 06:30.610
Okay.

96
06:30.640 --> 06:31.570
It doesn't check.

97
06:32.530 --> 06:35.920
It leaves the developer okay.

98
06:35.950 --> 06:37.540
So it doesn't check.

99
06:38.590 --> 06:42.550
And because the string is not compare with the "===" like this.

100
06:42.730 --> 06:43.600
Right?

101
06:43.810 --> 06:48.580
And for that reason, we must convert it into the number.

102
06:48.790 --> 06:49.330
Right.

103
06:49.360 --> 06:53.710
And for that reason I can convert it using the "+"

104
06:54.430 --> 07:01.960
"+" before "id", if I send okay, it's already worked.

105
07:02.650 --> 07:03.310
But.

106
07:05.710 --> 07:06.070
With.

107
07:06.070 --> 07:11.590
NestJS we can use a pipe, right here.

108
07:11.650 --> 07:17.650
We can convert this using the ParseIntPipe, right

109
07:18.130 --> 07:23.530
So if I use the second parameter "ParseIntPipe"

110
07:24.580 --> 07:26.950
If I console.log typeof

111
07:28.420 --> 07:28.810
id

112
07:35.020 --> 07:37.420
Now you can see it already worked right.

113
07:37.420 --> 07:41.260
And the type will be the number.

114
07:41.560 --> 07:44.890
And that's the reason we use pipe.

115
07:47.020 --> 07:47.500
Okay.

116
07:49.390 --> 07:52.810
So now let's go to the use case.

117
07:52.810 --> 07:58.750
You can see, the pipe it will transform the string into the integer right.

118
07:58.780 --> 08:00.220
This is the first use case

119
08:00.400 --> 08:02.530
And let's see what is the difference.

120
08:03.070 --> 08:04.690
The second use case.

121
08:07.420 --> 08:18.010
If I scroll down a little bit and because we use the ParseIntPipe, it already validation the param

122
08:18.010 --> 08:18.610
for me.

123
08:18.850 --> 08:21.490
If I pass something here.

124
08:21.910 --> 08:23.170
ABCXYZ?

125
08:26.050 --> 08:31.240
And now you can see, it showed a message validation failed numeric

126
08:31.870 --> 08:32.260
Okay.

127
08:32.290 --> 08:33.670
String is expect.

128
08:35.980 --> 08:41.770
For that reason, we will use the numeric string like: 2,3,4

129
08:42.910 --> 08:43.330
Right.

130
08:44.020 --> 08:44.800
Very cool.

131
08:45.220 --> 08:50.260
And I hope you understand what is the "pipe" right now.

132
08:50.380 --> 09:01.030
And we can use the some different "pipe" like ParseFloatPipe, or something like this to validation

133
09:02.110 --> 09:02.560
okay.

134
09:02.590 --> 09:04.990
To validation the param

135
09:09.640 --> 09:15.580
Let me see, we have something like this or not okay.

136
09:17.590 --> 09:20.230
Anyway, this is the pipe

137
09:20.230 --> 09:21.760
I hope you understand this video.

138
09:21.790 --> 09:23.560
I will see you in the next one.