在我的应用程序中,我需要多次将实体(数据库模型)映射到DTO(本地对象)。大多数情况下,DTO与实体具有相同的名称。例如,“The Entity”。
DTO(数据传输对象)
现在我添加了静态函数
在nestjs/node中,将DTO映射到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;
....
}
DTO(数据传输对象)
export class Company {
@ApiProperty()
id: string;
@ApiProperty()
email: string;
@ApiProperty()
name: string;
...
}
现在我添加了静态函数
toModel
和fromModel
。 static toModel(companyDto :CreateCompanyDto ) : CompanyModel {
const companyModel = new CompanyModel();
const {name, email,..... } = companyDto;
companyModel.name = name;
companyModel.email =email
.....
return companyModel;
}
在nestjs/node中,将DTO映射到ENTITY的最佳解决方案是什么?