Based on google groups conversation: https://groups.google.com/forum/#!topic/loopbackjs/Vrrl1Zi3q64/discussion
Let's assume there are two models: Chapter and Book, which hasMany Chapters via chapters relation.
Requirement is to list all Books with the count of Chapters for each of them in one REST API query. Example:
Request: GET /Books
Response:
[
{
bookid: 1,
chaptersCount: 3
},
{
bookid: 2,
chaptersCount: 12
},
{
bookid: 3,
chaptersCount: 7
}
]
where chaptersCount is the number of related Chapters for this book.
Some options:
Is there any way to achieve any of these options without a lot of custom coding? If not, I wonder if it makes sense to implement it in the framework.
I use the persistent approach. But this would be a tricky problem.
Currently, I listen to the related model's created event (fire in afterCreate hook, see https://github.com/strongloop/loopback/issues/838) and deleted event, then update the counter. Since I use mongodb, and counting things might be slow, so I take the advantage of $inc.
But this is a very common counter: it's just counting the total of exist related models. But what if this is a scoped relation, such as _confirmed but un-purchased orders of a product_? This require to increase the counter when order is confirmed, and decrease the counter when order is purchased. So the trigger of increasing counter and decreasing counter must be configurable.
Another problem is for no-SQL database like mongodb, there is no transaction, so the counter might be not reliable. It's better to verify the counter at a checkpoint and update if not match.
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
Seems like the way to go would be to add something in the include filter.
What do you think about:
relation: 'owner',
scope: {
type: "count"
}
+1
+1
+1
+1
+1
I think it can be set in the limit filter
+1
+1
+1
+1
+1
Thank you all for the feedback :)
Please ADD 👍 REACTION on the top comment instead of posting new "+1"
We are using vote system https://www.bountysource.com/teams/strongloop/issues to list issues with high numbers of thumb ups
+1
This would be fantastic +1
+1
:+1:
Status update?
Don't expect any update for a long time, this project is like molasses on a
winter day.
On Sun, Jul 9, 2017 at 7:36 PM Byron Mejia notifications@github.com wrote:
Status update?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/strongloop/loopback/issues/917#issuecomment-313987554,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACKNRDdYdJ_-wPSAYsiNa0JTxZQRWtFDks5sMY47gaJpZM4DHbfP
.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
Hello.
As this Finally been implemented?
Thanks and Regards
I hope this is not dead because this is desperate needed.... especially in the world of comments/posts with reaction counting (likes, dislikes, etc).
Is there a status update on this?
I need this function, too.
Any updates?
I found a mixin: loopback-counts-mixin
It looks like what I'm looking for.
@ahadortiz - this issues has been up since 2014. If you want to try a reliable, stronger contender, may I suggest Strapi.
_I am not associated with strapi in any ways. Strapi just ended up being the option we used in the past when loopback's pitfalls ended up being too much._
As a wise man once said...
Don't expect any update for a long time, this project is like molasses on a winter day.
A simple and quick workaround for this is to replace the "hasMany" relation by a "referencesMany" relation like this :
"relations": {
"chapters": {
"type": "referencesMany",
"model": "chapter",
"foreignKey": "chapterIds",
"options": {
"validate": true,
"forceId": false
}
}
}
So you will be able to count how many ids there are in the chapterIds property like this :
book.chapterIds.length.
This count can then be done either client-side or server-side if you create an observer or afterRemote .
Notice that when you will delete the book, it will not delete chapters because it only references them.
If you want to do such you can add an observer and delete all related chapters depending on ids listed in chapterIds .
Most helpful comment
+1
Seems like the way to go would be to add something in the include filter.
What do you think about: