Reputation: 13
I have already tried to search for this problem, I did not find a solution.
In Laravel (PHP) you can define the following on a ORM (Eloquent) model:
class User extends Eloquent
{
protected $with = ['roles'];
}
In the example, querying for a User
will always eager load the roles
relation (not defined in the example).
I want to do the same with Sequelize.js for Node.
Can you define auto-eager loading (included) releations?
Upvotes: 0
Views: 444
Reputation: 1638
You can uses scopes to achieve this, they support include
and you can define a defaultScope
.
For example (code not tested but should give you the idea you need to implement this in your own code):
const Role = sequelize.define('role', {
name: DataTypes.STRING,
}, {
timestamps: false,
});
const User = sequelize.define('user', {
name: DataTypes.STRING
}, {
timestamps: false,
defaultScope: {
include: Role,
},
});
User.hasMany(Role);
Role.belongsTo(User);
Upvotes: 2