【发布时间】:2023-04-06 23:53:01
【问题描述】:
我正在使用NestJS 和TypeORM 创建一个GraphQL API。从经典的用户实体开始,我按照 Nestjs 文档的描述创建了 user.type.ts 和 user.entity.ts。
这是内容示例:
user.entity.ts
@Entity({ schema: 'mydb', name: 'userList' })
export class User {
@Column('varchar', { name: 'guid', unique: true, length: 36 })
guid: string;
@Column('varchar', { name: 'firstName', length: 50 })
firstName: string;
@Column('varchar', { name: 'lastName', length: 100 })
lastName: string;
// ...
user.type.ts
@ObjectType()
export class UserType {
@Field({
name: 'ID',
description: 'Global Universal ID of the User',
})
guid: string;
@Field()
firstName: string;
@Field()
lastName: string;
// ...
问题是:由于它们使用相同的字段,我可以创建一个将两个类的装饰器组合在一起的类吗?
例如:
@Entity({ schema: 'mydb', name: 'userList' })
@ObjectType()
export class User {
@Column('varchar', { name: 'guid', unique: true, length: 36 })
@Field({
name: 'ID',
description: 'Global Universal ID of the User',
})
guid: string;
@Field()
@Column('varchar', { name: 'firstName', length: 50 })
firstName: string;
@Field()
@Column('varchar', { name: 'lastName', length: 100 })
lastName: string;
它是反模式吗?这样做有什么限制或缺点吗?
提前致谢
【问题讨论】:
标签: typescript graphql nestjs dry typeorm