getAndCountAssociations() similar to findAndCountAll()
See original GitHub issueHi,
following situation: I have a User model and a Follower n:m association
User.belongsToMany(User, {
as: 'Followings',
foreignKey: 'follower_id',
through: Follow
});
User.belongsToMany(User, {
as: 'Followers',
foreignKey: 'user_id',
through: Follow
});
Now I want to get the total number of followers and at the same time the data for some of the followers: user.getFollowers({limit: 24, offset: 48});
Works fine, but currently I need to make a separate call user.countFollowers()
to also get the total number of followers.
Maybe I just haven’t seen it in the docs but if not: would it be possible to add a convenience method to count and receive all associations? Something like user.getAndCountFollowers()
Currently my workaround(?) looks like this:
let result = {count: 0, rows: []};
User.findById(req.params.id)
.then((user) => {
return user.countFollowers()
.then((count) => {
result.count = count;
user.getFollowers()
.then((followers) => {
result.rows = followers;
return res.json(result);
})
})
})
.catch((err) => {
console.error(err.stack);
return res.status(500).json(err);
});
And I was wondering if there isn’t an easier way to do just that. And if there is no such way, wouldn’t it be a nice feature to add? 😉
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Results From Across the Web
orm - sequelize: how to do equivalent of findAndCountAll for ...
getting all users from a group would be straightforward: user.getGroups().then.... but porting this to findAndCountAll just doesn't seem to work ...
Read more >Model Querying - Finders - Sequelize
The findAndCountAll method is a convenience method that combines findAll and count . This is useful when dealing with queries related to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This would still be a great feature.
Any updates on this?