我正在考虑将TypeORM用于新项目(TypeScript + express)。
如何定义嵌套关系?
以下是数据结构的一个示例:
Topic:
{
applicationId: 2,
name: 'test',
recipients: [
{
id: 1,
transports: [
{
id: 1,
templateId: 1,
},
{
id: 2,
templateId: 1,
},
],
},
{
id: 2,
transports: [
{
id: 1,
templateId: 4,
},
],
},
],
startTime: null,
}
Topic
可以包含1个或多个Recipients
。对于每个Recipient
,有一个或多个Transports
。
目前我正在使用单个连接表定义关系:
CREATE TABLE topics_recipients_transports
(
id INT IDENTITY PRIMARY KEY,
topicId INT NOT NULL REFERENCES topics,
recipientId INT NOT NULL REFERENCES recipients,
transportId INT NOT NULL REFERENCES transports,
templateId INT NOT NULL REFERENCES templates,
config NVARCHAR(MAX)
)
GO
CREATE UNIQUE INDEX topics_recipients_transports_unique
ON topics_recipients_transports (topicId, recipientId, transportId)
GO
如您所见,该关系是Topic -> Recipient -> Transport
的一对多,但Transport -> Recipient
的关系取决于Recipient -> Topic
关联。
答案 0 :(得分:0)
嵌套关系可以定义为:
this.topicRepo.find({ relations: ["recipients", "recipients.transports"] });