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

NestJS

This entry is part 114 of 146 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.

PUT

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

postSchema.prisma

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

Response:

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:

Response:

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.

replacePost.dto.ts

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.

posts.service.ts

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.

prismaError.ts

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

posts.controller.ts

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

PATCH

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:

Response:

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.

updatePost.dto.ts

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.

updatePost.dto.ts

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.

canBeNull.ts

canBeUndefined.ts

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

updatePost.dto.ts

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.

posts.service.ts

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

posts.controller.ts

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

Summary

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 >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments