根据实体类型(TypeORM)设置不同的属性

时间:2019-10-13 19:34:47

标签: database-design typeorm

我有以下实体BotEntity。我要实现的目标是根据机器人的类型来具有不同的属性。例如,botType'instagram'的漫游器具有username属性,而botType的{​​{1}}具有'gmail'属性。

googleId

还有另一个实体@Entity('bot') class BotEntity { @PrimaryGeneratedColumn('uuid') id: string; @Column({ type: 'character varying' }) botType: 'instagram' | 'gmail' | 'facebook'; @OneToOne(type => ???) // type depends on 'botType' property properties: InstagramBotPropertiesEntity | GmailBotPropertiesEntity | FacebookBotPropertiesEntity; @ManyToOne(type => ProxyEntity, proxy => proxy.id) proxy: ProxyEntity; } 。这与问题无关,但是如果ProxyEntity易于查询,那就太好了。

ProxyEntity.associatedBots

使用@Entity('proxy') class ProxyEntity { @PrimaryGeneratedColumn('uuid') id: string; @OneToMany(type => BotEntity, bot => bot.proxy) associatedBots: BotEntity[]; } 装饰器的“单表继承”是一种可行的方法(https://github.com/typeorm/typeorm/blob/master/docs/entity-inheritance.md#single-table-inheritance),但是如果说有50种类型的漫游器,则意味着@TableInheritance表将包含所有不同种类的机器人的列

如何根据bot设置一组不同的属性BotEntity.properties?上面的代码绝不是一成不变的,而且我对TypeORM / SQL还是很陌生,因此,如果有另一种结构化方式,那么我100%愿意接受它

0 个答案:

没有答案