获取类型ORM中实体的关系列表?



我正在使用带有typeORM的存储库模式。对于下面定义的实体,如下所示

@Entity()
export class Apple extends BaseEntity {
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
createdDate: Date;
@ManyToOne((type) => xyz, (xyz) => xyz.fruit)
@JoinColumn({ name: 'fruit' })
fruit: Fruit;
@ManyToOne((type) => ABC, (abc) => abc.juice)
@JoinColumn({ name: 'juice' })
juice: Juice;

}

有没有办法获取所有关系的列表,即像格式 ['水果'、'果汁'] 这样的数组

这将列出属性名称。

entityManager.getRepository(Apple)
.metadata
.relations
.filter(relation => relation.isManyToOne)
.map(relation => relation.propertyName)

如果需要列名,可以使用:

entityManager.getRepository(Apple)
.metadata
.relations
.filter(relation => relation.isManyToOne)
.map(relation => relation.joinColumns.map(column => column.databaseName))

请注意,由于多列键,每个属性可能有多个列。

最新更新