【发布时间】:2018-11-28 07:51:01
【问题描述】:
考虑一下这个 lb4 模型
@model({
name: 'users'
})
export class User extends Entity {
@property({
type: 'number',
id: true,
})
id: number;
@property({
type: 'string',
required: true,
})
first_name: string;
@property({
type: 'string',
})
middle_name?: string;
@property({
type: 'string',
})
last_name?: string;
@property({
type: 'string',
required: true,
})
username: string;
@property({
type: 'string',
})
email?: string;
@property({
type: 'string',
})
phone?: string;
@property({
type: 'string',
required: true,
})
password: string;
@property({
type: 'string',
})
external_id: string;
@belongsTo(() => UserTenant)
created_by: number;
@belongsTo(() => UserTenant)
modified_by: number;
constructor(data?: Partial<User>) {
super(data);
}
}
目前,如果我们使用 lb4 cli 为该模型创建存储库和控制器,它将生成与输入/输出相同模型的路由方法 CRUD。但是,我们想要的是有一个单独的 DTO 模型(非持久化到 DB)用作控制器的输入/输出 DTO,不包括属性密码、created_by 和 modified_by。一种方法是手动创建这样一个模型类并写下一个转换器类,它将 UserDTO 对象转换为上面的用户模型(复制各个属性)。但这似乎是一个开销。此外,我们希望对更多模型执行此操作。所以,以这种方式做这件事似乎不是正确的方法。 lb4 是否提供了更好的方法来实现这一点?
【问题讨论】:
标签: node.js loopbackjs dto