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:
- How should I call the type
Result? (I don't thinkResultis an appropriate name, I try to find some more professional convention likeDTO,Model, etc. - Is it a better practice to create types like
Resulteven the returned type from theDALis the model itself? (meaning - theDALin the example would return a result of typePersonModeland there are conversions in the service to thePersonDTO)