0

I'm having a very simple setup with _User entity having a likes Relation with itself (reflective).

A common use case is list users. I'm listing very few users (ex: 15), but i would also like to display the amount of likes he has.

Following standard suggested technique from Parse.com that would require a query for each of the 15 _User(s). I don't think this is acceptable, maybe 2 queries are enough:

  • first one getting the first 15 _User(s)
  • second one getting the amount of likes each of the _User haves

But I have no idea if that's even possible with Parse API, so I'm asking for help ;)

1 Answer 1

2

If the column is a relation, then yes, getting the count will require a query per user.

If you expect the number of likes per user to be low (<100 is my semi-arbitrary rule of thumb), you could instead model likes as an array of pointers.

With that, you can know the count just by having the record in hand (i.e. someUser.get("likes").length). Even better, query include will eagerly fetch the related users...

userQuery.include("likes");
userQuery.find().then(function(users) {
    if (users.length) {
        var someUser = users[0];
        var likes = someUser.get("likes");
        if (likes.length) { // see, we can get the count without query
            var firstLike = likes[0];  // we can even get those other users!
            var firstLikeEmail = firstLike.get("email");
        }
    }
});

Otherwise, using relations, you're stuck with another query...

userQuery.find().then(function(users) {
    if (users.length) {
        var someUser = users[0];
        var likes = someUser.get("likes");
        return likes.query().count();
    } else {
        return 0;
    }
}).then(function(count) {
    console.log("the first user has " + count + " likes");
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for confirmation, I was hoping there was another way. I'll try and use arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.