WEBVTT

1
00:01.970 --> 00:03.620
Hi everyone!

2
00:06.260 --> 00:14.390
In this video I will show you the some options to reduce the amount of data.

3
00:15.260 --> 00:19.400
Okay, right now we already know.

4
00:23.420 --> 00:24.500
The first option.

5
00:27.560 --> 00:33.320
First option we were using DTO to customize the response.

6
00:33.350 --> 00:33.770
Right.

7
00:33.800 --> 00:35.600
Customize the response right here.

8
00:37.010 --> 00:46.490
And the second option is using the technique called serialization from NestJS documentation okay.

9
00:46.520 --> 00:55.070
This is the second option I will show you in this video if you click on the serialization topic right

10
00:55.070 --> 01:02.180
here and you have some exclude property, it will exclude property.

11
01:03.060 --> 01:04.170
something like this.

12
01:05.280 --> 01:14.610
Okay, you can see it's very easy to exclude some property like example like password, because when

13
01:14.610 --> 01:21.270
we respond data we don't need to return the password.

14
01:21.300 --> 01:21.870
Right.

15
01:22.680 --> 01:25.320
So this is the second option.

16
01:26.580 --> 01:32.700
But I don't like this option at all.

17
01:33.840 --> 01:36.390
I prefer using a DTO.

18
01:38.670 --> 01:51.750
The reason because when you work with this difference framework and in the MVC architecture, it's a way

19
01:51.780 --> 01:59.700
more prefer to use the DTO to reduce the amount of data.

20
02:00.240 --> 02:03.920
Okay, this is hiding of data.

21
02:05.780 --> 02:08.150
Okay, this line will be headed up.

22
02:08.150 --> 02:11.480
Like this here will be reduced.

23
02:14.540 --> 02:15.110
So.

24
02:16.520 --> 02:26.090
But anyway, let me show you how we can use last excluded property.

25
02:26.600 --> 02:28.220
Okay, let's see if it works or not.

26
02:30.080 --> 02:31.910
If I go to.

27
02:32.450 --> 02:35.150
Let me see the schema.

28
02:35.180 --> 02:35.630
Okay.

29
02:35.660 --> 02:36.740
Now you can see it.

30
02:37.400 --> 02:43.760
It use the exclude directly inside the user entity right here okay.

31
02:45.320 --> 02:55.700
Now let me use directly inside the schema and import that from class transformer.

32
02:55.790 --> 02:56.360
Okay.

33
02:57.140 --> 02:58.880
And this is not complete.

34
02:58.910 --> 03:06.360
We must use something called use interceptor with class serialize interceptor right here.

35
03:06.390 --> 03:08.070
I will copy that also.

36
03:08.670 --> 03:11.400
And I will go to the controller and import that.

37
03:12.780 --> 03:13.230
Right.

38
03:13.260 --> 03:13.830
Like this.

39
03:15.450 --> 03:18.900
Import and import the class.

40
03:20.790 --> 03:21.390
Okay.

41
03:21.930 --> 03:29.130
We already know the decorator call "@UseInterceptor", right?

42
03:30.510 --> 03:36.090
This will help me use the interceptor.

43
03:36.240 --> 03:39.510
And what is inside the class serializer?

44
03:39.540 --> 03:40.290
Interceptor.

45
03:41.400 --> 03:47.640
Also, actually, as a developer, we don't need to care about that.

46
03:48.150 --> 03:52.830
We only need to know when we @Exclude property.

47
03:52.980 --> 03:57.780
We must use the interceptor with the class serializer.

48
03:57.780 --> 03:59.220
Interceptor like that.

49
03:59.730 --> 04:00.150
Okay.

50
04:00.180 --> 04:01.740
And this will be enough.

51
04:02.730 --> 04:03.480
Okay.

52
04:03.480 --> 04:11.500
Now inside the get all method right now, I will remove, I will comment and I will return the

53
04:11.500 --> 04:12.310
directly the post

54
04:15.250 --> 04:15.970
With any.

55
04:16.480 --> 04:19.540
Okay, let's see what happens.

56
04:22.750 --> 04:27.790
If I go to the post and I send a request, now you can see it.

57
04:27.790 --> 04:29.830
Return for me is something like this.

58
04:30.250 --> 04:32.080
That's not what we want, right?

59
04:32.500 --> 04:43.060
I want it will return only the data title, it don't return the @Exclude property description.

60
04:43.090 --> 04:46.210
Okay, I want only show the title.

61
04:46.840 --> 04:58.270
You can see it's not work. I don't know why it's not work with the MongoDB, but in

62
04:58.270 --> 04:59.380
the SQL.

63
04:59.980 --> 05:00.400
Okay.

64
05:00.970 --> 05:05.320
Inside a normal SQL this line will be worked.

65
05:06.100 --> 05:12.480
But I don't know why this case didn't work right now, but anyway, don't worry about us.

66
05:14.340 --> 05:15.690
I will rollback the code

67
05:17.640 --> 05:21.900
But even if the line work.

68
05:23.670 --> 05:24.060
Okay?

69
05:24.990 --> 05:25.800
Even if.

70
05:28.740 --> 05:31.620
This work, I will not also use that.

71
05:31.650 --> 05:33.690
Why?

72
05:33.690 --> 05:34.530
For example.

73
05:35.940 --> 05:38.700
Later I will have a some property.

74
05:38.730 --> 05:41.100
For example like role.

75
05:42.450 --> 05:45.750
Okay, let's assume I have a role with string.

76
05:45.780 --> 05:46.380
Okay.

77
05:48.780 --> 05:52.050
And if I mark it @Exclude

78
05:55.620 --> 05:57.060
What happened right now?

79
05:58.320 --> 06:08.220
Well, if for example like staff of the company.

80
06:08.220 --> 06:08.230
company

81
06:11.260 --> 06:12.820
I don't want

82
06:12.910 --> 06:15.700
The staff can see the "role".

83
06:15.790 --> 06:18.760
But if I'm an admin.

84
06:19.360 --> 06:25.990
Okay, if I am the admin, I want it see the "role"

85
06:28.120 --> 06:28.720
Right.

86
06:28.930 --> 06:32.860
But in this case, if we you directly the @Exclude.

87
06:33.280 --> 06:38.110
I don't have any chance to do this stuff.

88
06:38.320 --> 06:46.030
I don't have a ability to check the condition in right here.

89
06:46.600 --> 06:49.780
So for that reason I will not use the @Exclude

90
06:49.900 --> 06:50.620
Okay.

91
06:50.650 --> 06:53.320
This is the downside of this approach.

92
06:53.710 --> 06:59.890
And for that reason, I will use the first approach, using the DTO like this.

93
07:00.970 --> 07:01.240
Okay.

94
07:01.240 --> 07:06.340
Let me run back code you don't need to write the code with me in this video.

95
07:06.370 --> 07:06.940
Okay.

96
07:09.450 --> 07:11.370
Then we discard the changes.

97
07:16.440 --> 07:17.160
Now.

98
07:19.650 --> 07:25.800
I will use this approach with you using the response DTO.

99
07:27.120 --> 07:27.570
Okay.

100
07:27.780 --> 07:36.750
But the downside of this ResponseDTO here, it will have some duplicate code like this.

101
07:36.750 --> 07:37.260
Right.

102
07:38.160 --> 07:40.770
Because this is only what we need.

103
07:42.720 --> 07:49.650
But we use something like here to convert it inside the DTO.

104
07:50.400 --> 07:53.820
This will increase the code base.

105
07:53.820 --> 07:54.390
Right.

106
07:54.420 --> 07:57.480
The number of codes I don't want that.

107
07:57.480 --> 08:08.040
So for that reason in the next video I will show you how we can customize this thing.

108
08:09.000 --> 08:11.400
So I will see you in the next video.