#Processing DTO before saving it into Mongoose

20 messages · Page 1 of 1 (latest)

wheat eagle
#

Hi all 👋🏼 !

Simple architecture question : where do you process your DTOs before creating / saving them into the database ?

The fact is, my DTO doesn't have the same structure as the targeted Schema. So, I have two questions : where and how ?

Where : I don't know if I need to transform my DTO right when it arrives into the controller (so through a Pipe), or right before the save function in my repository. To my mind, it's better to pass the DTO structure until it reaches the repository. Right ?

How : My DTO doesn't have the same structure as the targeted schema. For example, the same field in my DTO can be a string, but an object in my schema (because I want my DTO to be the simplest possible). I have no other idea than creating a mapper which will take the DTO and produce the desired Schema thanks to some logic and with plainToClass.

I'm a bit lost there, what's your advice ? Thanks for your help again and have a great day.

spring harbor
#

@wheat eagle - So you are speaking of two processes which answer the following questions, which are

validation - is what is coming into my app what I want it to be?
serialization/ deserialization - is what is coming into my app in the form I want it to be?

Both are explained in the docs under Techniques (and show when they should be done).

https://docs.nestjs.com/techniques/validation
https://docs.nestjs.com/techniques/serialization

There is one more exercise you should take into account:

sanitization - is the data in the request safe to use (it can be seen as part of validation, but I like to look at it separately)

I'd also say that your deserialization should be as simplistic as possible to save on compute effort (and avoid performance hits). In other words, simplistic DTOs aren't the better solution i.e. if you store an object in your DB, you should try to get the same or very similar object from the request too. There are exceptions of course, but that should be the norm.

Scott

wheat eagle
#

Thanks a lot for the clarification @spring harbor , I will dig into those docs then. Have a great day

#

So, what you suggest is that I should not manipulate DTOs at all in my services / repositories but instead convert them directly into Schema during the deserialization process ?

#

Aka, the controller should call the service with Schema and not DTO

spring harbor
#

Yeah, the general rule is to catch errors as early as possible. So, your controller/ resolver layer should be doing the deserialization/ validation and sanitization processes.

wheat eagle
#

Makes sense to me !

#

Should I use a Pipe for converting my DTO to Schema right after the Validation Pipe ?

spring harbor
#

I've also seen deserialization happening just before the service too.

#

I believe interceptors are better for transformation.

wheat eagle
#

Thanks a lot for all of these, I will try interceptors right away

#

Oh… I don't think interceptors is good for my use case, because I only want this transformation for the request, not the response

#

I think https://docs.nestjs.com/pipes#transformation-use-case is the best way to tranform my DTO into a Schema ! No ?

spring harbor
#

@wheat eagle - The docs speak of serialization. But, deserialization is the same (albeit opposite) kind of process.

wheat eagle
#

Yeah but with interceptors, the function is applied to both request or response, and I don't want any treatment on responses

spring harbor
#

You can handle either a request or a response or both. @wheat eagle

wheat eagle
#

After reading both interceptors and pipes docs, I don't get the benefit of using interceptors over transformation pipe for my specific use case. For a specific route, I want to transform my CreateDTO into a Schema that I will send to the repository

spring harbor
#

If you are only doing schema validation then yeah. Use a pipe. If you are transforming, interceptors are better.

#

Although, I guess it really doesn't matter. Both can do the transformation job.