NestJS - what are the best practices to consider return types of custom queries from the DAL?

359 Views Asked by At

I have a project with NestJS where I use the classic controller-service-dal architecture.

Until recently, I performed only simple queries from the DAL which returned the models themselves (or entities if you prefer). Now I work on a query that alters the result with projection and lookup - so actually my return type is not the exact model. Here's an example: consider having a Person model which is the following:

type PersonModel = {
  firstName: string;
  lastName: string;
  birthDate: Date;
}

but my query in the DAL returns an object in the following type:

type Result = {
  name: string; // this is the combination of firstName and lastName of the model
  birthDate: Date;
}

After that in the service I perform some conversions to the appropriate DTO which is the following:

type PersonDTO = {
  name: string;
  age: number; // this is computed by conversion in the service level
}

My main questions are:

  1. How should I call the type Result? (I don't think Result is an appropriate name, I try to find some more professional convention like DTO, Model, etc.
  2. Is it a better practice to create types like Result even the returned type from the DAL is the model itself? (meaning - the DAL in the example would return a result of type PersonModel and there are conversions in the service to the PersonDTO)
0

There are 0 best solutions below