2014-06-18 27 views
21

Używam Sequelize z węzłem + MySQL.Zagnieżdżone relacje z Sequelize

Mam modelu strukturę podobną do tej:

// 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; 
    // ... 
}); 

Czy to w ogóle możliwe? Jakie są możliwe obejścia? Dziękuję Ci!

Odpowiedz