web-dev-qa-db-fra.com

Relations imbriquées avec Sequelize

J'utilise Sequelize avec Node + MySQL.

J'ai une structure de modèle similaire à celle-ci:

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

Est-ce possible? Quelles sont les solutions possibles? Merci!

31
Domi

Sequelize Docs: Nested Eager Loading

Exemple

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