【发布时间】:2021-06-16 19:39:17
【问题描述】:
在我的应用中,我需要从实体(数据库模型)到 DTO(本地对象)的多次映射
大多数时候,D 与实体同名
例如实体
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的最佳解决方案是什么
【问题讨论】:
标签: node.js rest mapping nestjs