Reputation: 16871
I am looking at using TypeORM for a new project (TypeScript + express).
How can I define a nested relationship?
Here's an example of the data structure:
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,
}
A Topic
can have 1 or more Recipients
. For each Recipient
, there is 1 or more Transports
.
Currently I am defining the relationship with a single join table:
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
As you can see, the relationship is one-to-many from Topic -> Recipient -> Transport
but the relationship from Transport -> Recipient
is dependent on the Recipient -> Topic
association.
Upvotes: 3
Views: 2127
Reputation: 81
Nested relationship can be defined as:
this.topicRepo.find({ relations: ["recipients", "recipients.transports"] });
Upvotes: 3