Hi everyone!
In this video I will show you the some options to reduce the amount of data.
Okay, right now we already know.
The first option.
First option we were using DTO to customize the response.
Right.
Customize the response right here.
And the second option is using the technique called serialization from NestJS documentation okay.
This is the second option I will show you in this video if you click on the serialization topic right
here and you have some exclude property, it will exclude property.
something like this.
Okay, you can see it's very easy to exclude some property like example like password, because when
we respond data we don't need to return the password.
Right.
So this is the second option.
But I don't like this option at all.
I prefer using a DTO.
The reason because when you work with this difference framework and in the MVC architecture, it's a way
more prefer to use the DTO to reduce the amount of data.
Okay, this is hiding of data.
Okay, this line will be headed up.
Like this here will be reduced.
So.
But anyway, let me show you how we can use last excluded property.
Okay, let's see if it works or not.
If I go to.
Let me see the schema.
Okay.
Now you can see it.
It use the exclude directly inside the user entity right here okay.
Now let me use directly inside the schema and import that from class transformer.
Okay.
And this is not complete.
We must use something called use interceptor with class serialize interceptor right here.
I will copy that also.
And I will go to the controller and import that.
Right.
Like this.
Import and import the class.
Okay.
We already know the decorator call "@UseInterceptor", right?
This will help me use the interceptor.
And what is inside the class serializer?
Interceptor.
Also, actually, as a developer, we don't need to care about that.
We only need to know when we @Exclude property.
We must use the interceptor with the class serializer.
Interceptor like that.
Okay.
And this will be enough.
Okay.
Now inside the get all method right now, I will remove, I will comment and I will return the
directly the post
With any.
Okay, let's see what happens.
If I go to the post and I send a request, now you can see it.
Return for me is something like this.
That's not what we want, right?
I want it will return only the data title, it don't return the @Exclude property description.
Okay, I want only show the title.
You can see it's not work. I don't know why it's not work with the MongoDB, but in
the SQL.
Okay.
Inside a normal SQL this line will be worked.
But I don't know why this case didn't work right now, but anyway, don't worry about us.
I will rollback the code
But even if the line work.
Okay?
Even if.
This work, I will not also use that.
Why?
For example.
Later I will have a some property.
For example like role.
Okay, let's assume I have a role with string.
Okay.
And if I mark it @Exclude
What happened right now?
Well, if for example like staff of the company.
company
I don't want
The staff can see the "role".
But if I'm an admin.
Okay, if I am the admin, I want it see the "role"
Right.
But in this case, if we you directly the @Exclude.
I don't have any chance to do this stuff.
I don't have a ability to check the condition in right here.
So for that reason I will not use the @Exclude
Okay.
This is the downside of this approach.
And for that reason, I will use the first approach, using the DTO like this.
Okay.
Let me run back code you don't need to write the code with me in this video.
Okay.
Then we discard the changes.
Now.
I will use this approach with you using the response DTO.
Okay.
But the downside of this ResponseDTO here, it will have some duplicate code like this.
Right.
Because this is only what we need.
But we use something like here to convert it inside the DTO.
This will increase the code base.
Right.
The number of codes I don't want that.
So for that reason in the next video I will show you how we can customize this thing.
So I will see you in the next video.