I am using Sequelize with Node + MySQL.
I have a model structure similar to this:
// models:
var Group, Issue,
If you want to eager load all nested associations use this function.
Issue.find({
include:getNestedAssociations(Issue)
});
//Recursively load all bested associtiaons
function getNestedAssociations(_model) {
const associations = [];
for (const association of Object.keys(_model.associations)) {
const model = _model.associations[association].target;
const as = association;
const include = getNestedAssociations(model);
associations.push({
model: model,
as: as,
...(include && { include: include }),
});
}
return associations;
}
Sequelize Docs: Nested Eager Loading
Example
Issue.find({
include: [
{
model: Invite,
include: [Group]
}
]
});