API with NestJS #175. PUT and PATCH requests with PostgreSQL and Drizzle ORM

NestJS

This entry is part 175 of 175 in the API with NestJS

When users send an HTTP request to our API, they use a specific method to indicate whether they want to retrieve, send, delete, or update data. While we could technically delete data on a GET request, it’s our responsibility to design the API in a way that is intuitive and adheres to best practices.

Most HTTP methods are simple and self-explanatory. However, both the POST and PATCH methods can modify existing data, which can sometimes cause confusion. In this article, we’ll explore how to use them with Drizzle ORM and clarify their differences.

PUT

Our database contains a straightforward table with articles.

database-schema.ts

Let’s start by fetching one of the existing articles.

Response:

One of the possible approaches to modifying existing articles is to use a PUT method. It changes the entities by replacing them. If the request does not include a particular field, the field should be removed.

Request body:

Response:

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

Implementing the PUT method with Drizzle ORM

We need to use the method to modify existing entities with the Drizzle ORM.

The crucial thing is that the method affects only the properties we provide explicitly. To remove the property, we need to explicitly set it to .

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

replace-article.dto.ts

Now, we can use the above DTO in our service. If the user doesn’t provide one of the optional properties, our API will set them to .

articles.service.ts

Finally, we can create a PUT method handler in our controller.

articles.controller.ts

Thanks to this approach, making a PUT request without providing the property changes it to .

PATCH

Alternatively, we can use the PATCH method to modify existing entities partially using a set of instructions. The most common way of implementing the PATCH method is to handle a request body with a partial entity.

Request body:

Response:

The most important thing about the PATCH requests is that to delete a property, we must send the value explicitly. Because of that, the above request does not remove the property. This prevents us from removing values by accident.

Implementing the PATCH method with Drizzle ORM

With the PATCH method, providing any of the properties is optional. Because of that, we might think of using the decorator built into the library. Unfortunately, there is a significant issue with it. It allows the property to be either undefined or , which can be confusing.

While all properties are optional when using a PATCH method, trying to set for a property that is not nullable can cause an Internal Server Error in our application. To solve this, we should use the decorator to allow the users to omit a value but not provide .

update-article.dto.ts

We can still use the decorator for because this property is nullable in our database.

With the above approach, we validate the property only if the user provides it. If the user does not provide the property, we don’t throw an error.

We can create a custom decorator that uses under the hood to prevent our code from being duplicated.

can-be-undefined.ts

Thanks to the above decorator, our code can be shorter and more explicit.

update-article.dto.ts

To implement the PATCH method with Drizzle ORM, we need to use the method like before. It fits our needs since it does not modify the properties the user didn’t explicitly provide.

articles.service.ts

The last step is to add the PATCH method to our controller.

articles.controller.ts

Summary

In this article, we’ve explained how to modify existing entities using both the and methods using NestJS and the Drizzle ORM. To achieve that, we had to dive deeper into how the handles missing values.

While we can make both approaches work, using prevents the users from accidentally removing properties since they need to provide the value explicitly. Understanding the difference between and methods is important if we want our API to be user-friendly and to follow best practices.

Series Navigation<< API with NestJS #174. Multiple PostgreSQL schemas with Drizzle ORM
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments