question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getAndCountAssociations() similar to findAndCountAll()

See original GitHub issue

Hi,

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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Jufebrowncommented, Oct 26, 2017

This would still be a great feature.

0reactions
marziplycommented, Apr 30, 2019

Any updates on this?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found