API with NestJS #112. Serializing the response with Prisma

JavaScript NestJS

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

When fetching data from the database, we do not always want to present it to the user in the original form. When working with NestJS, the most popular way of modifying the response is with the library. However, using the above library with Prisma requires a bit of work. In this article, we provide examples of how to do that in a clean and easy-to-understand way.

The class-transformer library with TypeORM

When working with libraries such as TypeORM, we create data models that use decorators from both TypeORM and the library.

user.entity.ts

Thanks to the above, when we query the database using TypeORM, it returns instances of the class defined above.

When we return instances of our class through the controller, we can use the built into NestJS.

user.controller.ts

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

Under the hood, calls the from the and applies the rules we’ve defined using the decorators. An excellent example is using the decorator to avoid sending the password in the response of our API.

Using Prisma with class-transformer

When working with Prisma, we use the Prisma schema files that are not created using TypeScript.

userSchema.prisma

Because of that, we don’t have the opportunity to use the decorators from the library. When we query our database using Prisma, it returns the  data generated under the hood.

users.service.ts

To use Prisma with the library, we need to create a separate class.

userResponse.dto.ts

Transforming the data

The most crucial thing to understand is that Prisma does not return the class when we query the database. The most straightforward way of transforming our data into the is to use the  function provided by the library.

authentication.controller.ts

With the above approach, we create instances of the that uses the decorator. The can understand this data and strip the password before the response reaches the user.

Mapping the response with an interceptor

Having to manually use the function every time we want to take advantage of the is not ideal and prone to mistakes. An alternative is creating a custom interceptor following the official documentation.

transformData.interceptor.ts

Above, we modify the controller’s response by converting it to a class instance provided as an argument. A significant advantage of the custom interceptor is that it is straightforward to use.

authentication.controller.ts

Furthermore, suppose all methods in our controller return the same data type. In that case, we can use on the whole controller instead of doing that separately for each method. This is especially useful because the function works with arrays out of the box. Therefore, we can use our custom interceptor when a method in our controller returns an array.

Summary

In this article, we’ve taken a look at how to serialize responses with NestJS and used an example of removing a field we don’t want to expose. To understand how it works, we first investigated how serializing works when using TypeORM. Doing that helped us find the missing step we need to perform when serializing the responses in a project that uses Prisma.

Since remembering to use the function manually can be quite a chore, we created the to do it for us. Since it works fine with arrays, it covers a wide variety of use cases. Thanks to doing all of the above, we can serialize our data using various decorators from the library.

Series Navigation<< API with NestJS #111. Constraints with PostgreSQL and PrismaAPI with NestJS #113. Logging with Prisma >>
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Krzysztof
Krzysztof
1 year ago

AutoMapper TypeScript could be an good alternative