In my app I need multi times map from the Entity (Database model) to DTO (local object)
Most times, Dto have the same names as the entity
For example The Entity
export class CompanyModel extends BaseEntity {
constructor(init?: Partial<CompanyModel>) {
}
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ length: 500 })
name: string;
@Column({ length: 500, unique: true })
email: string;
....
}
The DTO
export class Company {
@ApiProperty()
id: string;
@ApiProperty()
email: string;
@ApiProperty()
name: string;
...
}
Now I add static function toModel and fromModel
static toModel(companyDto :CreateCompanyDto ) : CompanyModel {
const companyModel = new CompanyModel();
const {name, email,..... } = companyDto;
companyModel.name = name;
companyModel.email =email
.....
return companyModel;
}
What is the best solution for mapping DTO to ENTITY in nestjs / node