英文:
Can I use one class as Entity and ViewEntity
问题
I'm working with Typeorm using MySQL database. I have a class as Entity and another class as ViewEntity. The question is why I need two separate Classes when they serve the same goal. Could I use the same Class with Entity and ViewEntity decorators?
我正在使用Typeorm和MySQL数据库。我有一个类作为Entity,另一个类作为ViewEntity。问题是为什么我需要两个不同的类,当它们都用于相同的目的。我是否可以在同一个类中使用Entity和ViewEntity装饰器?
Example Entity of Person
class:
Person
类的示例Entity:
@Entity()
export class Person {
@PrimaryGeneratedColumn({ type: 'int', unsigned: true })
id: number;
@Column({ length: 50 })
name: string;
@Column({ type: 'date' })
birthDate: string;
}
Another class for ViewEntity with the same properties added age
property:
另一个用于ViewEntity的类,添加了age
属性:
ViewEntity({
expression: `SELECT id, name, birthDate,
TIMESTAMPDIFF(YEAR,birthDate,CURDATE()) AS age FROM person;`
})
export class PersonView {
@ViewColumn()
id: number;
@ViewColumn()
name: string;
@ViewColumn()
birthDate: string;
@ViewColumn()
age: number;
}
To reduce redundant why I can't combine these two classes by doing this:
为了减少冗余,为什么我不能通过这样做将这两个类合并:
@Entity()
@ViewEntity({
name: 'PersonView',
expression: `SELECT id, name, birthDate,
TIMESTAMPDIFF(YEAR,birthDate,CURDATE()) AS age FROM person;`
})
export class Person {
@ViewColumn()
@PrimaryGeneratedColumn({ type: 'int', unsigned: true })
id: number;
@ViewColumn()
@Column({ length: 50 })
name: string;
@ViewColumn()
@Column({ type: 'date' })
birthDate: string;
@ViewColumn()
age: number;
}
I searched in Typeorm documentation but they use a class only for ViewEntity. When I tried to test that I face a problem:
我在Typeorm文档中进行了搜索,但它们仅使用一个类来表示ViewEntity。当我尝试测试时,出现了问题:
DataTypeNotSupportedError: Data type "" in "Person.age" is not supported by "mysql" database.
Also, how would I retrieve data? By doing dataSource.getRepository(Person)
Person class has Entity and ViewEntity decorator then which type of entity will be retrieved? How would Typeorm distinguish between these two entities?
另外,我如何检索数据?通过dataSource.getRepository(Person)
来进行。Person类具有Entity和ViewEntity装饰器,那么将检索哪种类型的实体?Typeorm如何区分这两个实体?
英文:
I'm working with Typeorm using MySQL database. I have a class as Entity and another class as ViewEntity. The question is why I need two separate Classes when they serve the same goal. Could I use the same Class with Entity and ViewEntity decorators?
Example Entity of Person
class:
@Entity()
export class Person {
@PrimaryGeneratedColumn({ type: 'int', unsigned: true })
id: number;
@Column({length:50})
name: string;
@Column({type:'date'})
birthDate: string;
}
Another class for ViewEntity with the same properties added age
property:
ViewEntity({
expression: `SELECT id, name, birthDate,
TIMESTAMPDIFF(YEAR,birthDate,CURDATE()) AS age FROM person;`
})
export class PersonView {
@ViewColumn()
id: number;
@ViewColumn()
name: string;
@ViewColumn()
birthDate: string;
@ViewColumn()
age: number;
}
To reduce redundant why I can't combine these two classes by doing this:
@Entity()
@ViewEntity({
name: 'PersonView',
expression: `SELECT id, name, birthDate,
TIMESTAMPDIFF(YEAR,birthDate,CURDATE()) AS age FROM person;`
})
export class Person{
@ViewColumn()
@PrimaryGeneratedColumn({ type: 'int', unsigned: true })
id: number;
@ViewColumn()
@Column({length:50})
name: string;
@ViewColumn()
@Column({type:'date'})
birthDate: string;
@ViewColumn()
age: number;
}
I searched in Typeorm documentation but they use a class only for ViewEntity. When I tried to test that I face a problem:
DataTypeNotSupportedError: Data type "" in "Person.age" is not supported by "mysql" database.
Also, how would I retrieve data? By doing dataSource.getRepository(Person)
Person class has Entity and ViewEntity decorator then which type of entity will be retrieved? How would Typeorm distinguish between these two entities?
答案1
得分: 0
我明白了,这是代码部分,不需要翻译。
英文:
After a while, I figured out a half solution because we still need two classes (apparently necessary to distinguish between the Table and the View). But without duplicated properties.
I used extends
and Yes it works:
@Entity()
export class Person{
@ViewColumn()
@PrimaryGeneratedColumn({ type: 'int', unsigned: true })
id: number;
@ViewColumn()
@Column({ length: 50 })
name: string;
@ViewColumn()
@Column({ type: 'date' })
birthdate: string;
}
@ViewEntity({
expression: `SELECT id, name, birthdate,
TIMESTAMPDIFF(YEAR,birthDate,CURDATE()) AS age FROM person;`
})
export class PersonView extends Person {
@ViewColumn()
public age: number;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论