API with NestJS #114. Modifying data using PUT and PATCH methods with Prisma


This entry is part 114 of 158 in the API with NestJS

Developing a REST API requires us to create endpoints using various HTTP methods such as GET, POST, and DELETE. People utilizing our API expect that making a GET request lists data instead of deleting it. It’s the developer’s role to ensure that the API is easy to understand and follows the best practices. While most HTTP methods are straightforward, we can use either POST or the PATCH method to modify existing data. In this article, we learn how to use both of them and outline their differences.


In the previous parts of this series, we’ve defined a model of a post.


It has a few required properties and the optional . First, let’s take a look at one of the existing posts.


One way of modifying existing posts would be by using a PUT method. It changes existing entities by replacing them. Therefore, if the request does not contain a particular field, the field should be removed.

Request body:


Since our request body does not contain the property, it should be set to null.

Implementing the PUT method with Prisma

The method is the most straightforward way to modify an entity using Prisma.

The most important thing about the method is that it only affects the properties provided explicitly. This means that to remove the property, we need to mark it as null explicitly.

To deal with this problem, we can create a Data Transfer Object that assigns as a default value.


It is worth noting that we use the decorator above that allows to be undefined, or null.

We can now use the above DTO in our service. To prevent Prisma from modifying the id of existing posts, we can provide as the id.


An important thing above is that we are using the enum we created in some of the previous parts of this series. It helps us understand the error that happened and allows us to act accordingly.


The last step is to add a PUT method handler to our controller.


Thanks to using our DTO, if the users make a PUT request without providing the , we change it to null.


Another approach to modifying existing entities is through a PATCH method. The HTTP protocol describes it as modifying an entity partially through a set of instructions. The most straightforward way of implementing it is by sending a request body with the partial entity.

Request body:


The crucial thing about the above request is that we don’t provide the property when modifying our post. To delete a property, we need to send null explicitly. Thanks to that, we can avoid removing values by accident.

Implementing the PATCH method with Prisma

Let’s start by creating a new Data Transfer Object for the PATCH method. Since with sending a PATCH request, all properties are optional, we might think of using the decorator.


However, there is one big issue with it. Unfortunately, it causes the to allow both undefined and null as the value for the decorated property. This can cause an Internal server error in our application because most of our columns are not nullable. We can solve this problem by using the decorator.


With the above code, we validate the properties only if they match the provided condition. We can simplify our DTO a lot by creating two custom decorators that use under the hood.



Thanks to our new decorators, our code can be cleaner and shorter.


Implementing the PATCH method with Prisma is very straightforward, thanks to how the method works. It modifies only the explicitly provided properties, which is precisely what we need.


Finally, we need to add the PUT method handler to our controller.


Thanks to all of the above, we can now support a PUT method.


Both the PUT and PATCH method have their use cases and can be useful. However, with the PATCH method, we don’t expect the user to know about all of the properties of our entity. Since deleting a value requires the users to provide null explicitly, it is less likely that they will remove a property by accident.

Since, in this article, we’ve gone through both PUT and PATCH and implemented them in our application with NestJS in Prisma, you now have the necessary knowledge to use the approach that suits your project the most.

Series Navigation<< API with NestJS #113. Logging with PrismaAPI with NestJS #115. Database migrations with Prisma >>
Notify of
Inline Feedbacks
View all comments