API with NestJS #177. Response serialization with the Drizzle ORM

NestJS

This entry is part 177 of 181 in the API with NestJS

When fetching data from the database, we often don’t want to present it to the user in its raw form. To prevent that, we need to serialize the response in NestJS before sending it. The most popular way to achieve that in NestJS is using the library. In this article, we explain how to set it up with the Drizzle ORM and provide practical examples.

The database we built in the previous parts of this series contains a table to store our users.

database-schema.ts

If you want to learn the basics of how to use the Drizzle ORM with NestJS, take a look at

Among other things, it contains the hashed password of each user. We don’t want to share this hash with anyone for security reasons. We can also cover a part of the phone number for privacy and display only the last few digits. To ensure that, we need to implement serialization.

A basic solution for serialization

The most straightforward way to implement serialization with NestJS is to define a simple class with a constructor.

authentication-response.dto.ts

Please notice that we don’t include the property above.

Thanks to using , we expect the exact data fetched from the database to be provided when the instance of the class is created.

authentication.controller.ts

If you want to know more about authenticating check out API with NestJS #3. Authenticating users with bcrypt, Passport, JWT, and cookies

Thanks to creating an instance of the , we serialize our response and don’t return the raw data fetched from the database.

While the above approach works, it requires us to work with our data manually in the constructor. Also, if our controller returns an array, we need to map the array to the instances of our class manually.

Using Drizzle ORM with the class-transformer

We can use the library to avoid creating the constructor manually. It allows us to transform regular objects into instances of a particular class and transform the data in the process.

authentication-response.dto.ts

It’s important to note that if we don’t want to include a particular property in our response when using the library, we still need to add it to our class. However, we have to mark it with the decorator.

To ensure that we haven’t omitted any properties from the database, we use the keyword so that the follows the correct structure.

Creating the instances of our class

Since our class does not have a constructor, we need another way of creating its instances. We can use the function from the library to do that.

authentication.controller.ts

When we use the method, the library applies the transformations we specified using the decorators.

Instead of manually calling the  method, we can use the () decorator. It calls the method under the hood.

authentication.controller.ts

The works as expected when our controller returns arrays.

Summary

In this article, we explored how to implement serialization in a project with NestJS and the Drizzle ORM. We implemented examples that include removing a field we don’t want to expose and modifying a property for privacy reasons. First, we implemented a simple approach where we manually implemented all of the transformations. We also learned how to simplify the process using the library. This way, we can leverage multiple different decorators to do the work for us. Thanks to the above, we can now serialize the responses in our API in a straightforward way.

Series Navigation<< API with NestJS #176. Database migrations with the Drizzle ORMAPI with NestJS #178. Storing files inside of a PostgreSQL database with Drizzle >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments