Hi everyone.
In this video I will show you how to do PATCH operation.
Okay, now I will create one more method with PATCH.
And I will use updateOne()
Okay will be the @Param() will be the same
id: string, @Body() requestBody
Body.
Now what is the request body.
Right now I cannot use the
UpdatePostDTO because
Because it is not empty like this, right?
I cannot remove one of that.
I want it have a validation.
But I want this only work for the title or the description.
Okay.
How we can do that.
So I will create another DTO
Post
Patch
.dto
.ts
And now?
If I create a class called Update.
Post
Patch
DTO
Now how?
I can write a validation.
I cannot copy anything here, but it's inside here, right?
So for that reason, I will use a special syntax in the typescript.
I will extends.
Okay.
I will extend the class.
I will inherit from the Partial.
Partial
Oh, I don't know why.
I.
Let
Let me find a package.
nestjs
Partial type
Right here.
We have a something called mapped type, inside the open api library here.
Now you can see.
You can see, it use the special syntax call PartialType and it'll use something like this
Right.
But.
But why.
I cannot use the PartialType right here.
PartialType import from @nestjs/swagger
I think I must install this package.
Okay, let me install.
Let me use PartialType from nestjs/swagger , and I will inherit it from the CreatePostDTO
And technical
We can easily,
use the UpdatePostDTO, but
I will inherit from CreatePostDTO because it later we.
We may or may not have UpdatePostDTO
anymore
Right.
So for that reason, I will inherit it from CreatePost
DTO
And what happened right now with, with the update right here.
It will extend the Partial of CreatePost
DTO
It means it will ignore one of that, or it will ignore the title.
Okay, that's the case of the PartialType
Okay.
That's it.
Just enough for you.
And let me show you how we can practice.
Let me return.
Actually, I will create one more method call Update
Let me copy all of that
And I will change the name to updateOne
And we have a id and requestBody
will be UpdatePostPatchDTO
And now you can see, it's work, right?
Because it's accept one or more property.
And that for the job, but in this way, it'll not work
And I can use a special syntax like Object.assign(post, requestBody)
Now let's see it work or not
I will return this.postService.updateOne
and id and requestBody
Right here will be
UpdatePostPatchDTO
And let me, let me update.
Let me duplicate and change to PATCH right here.
I will only update the title.
Let's see, if I send, now you can see, it update, right?
Very cool.
If I specify the description it also work because it's aceept 1 or 2.
If I send, okay it's work, right.
If I remove the title and I will use UpdatePost, then I send, now you can see it's worked very well
and that's for the job of the PartialType, the PartialType, it just ignore one or more property inside
the original DTO.
Okay, in this case we will CreatePostDTO
So that is, that how we can use the PartialType
And I will not explain what is inside the Object.assign
You can read more about that.
In here because it is just the JavaScript knowledge.
It just merge the type
Okay, we just merge from here into the target type.
Okay, now you can see, for example I have a target with a and B okay I have another object with b
and c.
So if I use the object.assign, it will merge it inside the final object like this.
So that's just the object.assign. I will see you the next one.