web-dev-qa-db-ja.com

Sequelizeとのネストされた関係

Node + MySQLでSequelizeを使用しています。

私はこれに似たモデル構造を持っています:

// models:
var Group, Issue, Invite;

// many Issues per Group
Group.hasMany(Issue);
Issue.belongsTo(Group);

// Groups can invite other Groups to work on their Issues
Issue.hasMany(Invite, {foreignKey: groupId});
Invite.belongsTo(Issue, {foreignKey: groupId});
Group.hasMany(Invite, {foreignKey: inviteeId});
Invite.belongsTo(Group, {foreignKey: inviteeId});

// given an Issue id, include all Invites + invited Groups (inviteeId) - But how?
var query = {
    where: {id: ...}, 
    include: ???
};
Issue.find(query).complete(function(err, issue) {
    var invites = issue.invites;
    var firstInvitedGroup = issue.invites[0].group;
    // ...
});

これはまったく可能ですか?可能な回避策は何ですか?ありがとうございました!

31
Domi

ドキュメントのシークレット化:ネストされたEager Loading

Issue.find({
    include: [
        {
            model: Invite,
            include: [Group]
        }
    ]
});
87