Adding to the model the count of related model instances

1,311 views
Skip to first unread message

Aliaksandr Voitau

unread,
Nov 24, 2014, 10:30:12 PM11/24/14
to loopb...@googlegroups.com
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.

Could you suggest any ways how to approach this problem? Thanks.

Simon Ho

unread,
Nov 25, 2014, 2:32:05 AM11/25/14
to loopb...@googlegroups.com
Have a look at the example I created. It shows you everything you need to know to get started with relations in LoopBack.

https://github.com/strongloop/loopback-example-relations-basic

tl;dr
1. Book hasMany Chapters
2. Chapter belongsTo Book
3. slc run
4. localhost:3000/explorer
5. Dig around to see your relation in action

Aliaksandr Voitau

unread,
Nov 25, 2014, 2:45:13 AM11/25/14
to loopb...@googlegroups.com
Thank you for the example. I've actually seen it before as well.

But unfortunately it doesn't answer my question. That's clear how to get the count of chapters for the single book by using relation:
GET /Books/{id}/chapters/count

How can I include the count of related model instances in each item in the list of Books for the method:
GET /Books

The result should be:
[
  {
    bookid: 1,
    chaptersCount: 3
  },
  {
    bookid: 2,
    chaptersCount: 12
  },
  {
    bookid: 3,
    chaptersCount: 7
  }
]

where chaptersCount is the number of related Chapters for this book.

Thanks,
Alex.

Simon Ho

unread,
Nov 25, 2014, 2:04:26 PM11/25/14
to loopb...@googlegroups.com
You can perform your own logic through a remote method in this case. So you create a new remote hook at `GET /api/Books/chaptersCount`. In that remote method, you would do a query to the chapters model to get a count and return whatever you need to return.

See the remote methods doc for more info: http://docs.strongloop.com/display/LB/Remote+methods

Aliaksandr Voitau

unread,
Nov 25, 2014, 3:46:29 PM11/25/14
to loopb...@googlegroups.com
This may be the option. But what I need is to add this as a field to a response for a usual query, which is still `GET /Books`. Because client does not need to have to use two different methods to get this information. Moreover, the number of this relations may be more than one. For example, you might want to display the number of related Authors as well.

The only way I see it can be done is to use remoteMethod hook and for each book in results set do a count of books. What I don't like about this approach is that it requires custom code, while this kind of functionality may be useful in other places. Also, I'm not sure how easy it will be to hide this derived field using filter query parameter, if the counts are not necessary. Unless you want to implement the logic of parsing filter query params in remoteMethod hook as well. That's why I'd like it to be configured rather than coded. I think that something like a scope for relation would be a good alternative. But instead of getting the full list I would need to get the count. Is it possible to achieve this somehow?

Thanks,
Alex.

Simon Ho

unread,
Nov 25, 2014, 3:53:58 PM11/25/14
to loopb...@googlegroups.com
Not that I know of. Remote methods/remote hooks seems like the way to do this. Your idea is valid though. Please submit a feature request in the github repo for LoopBack and we'll see what can be done.

Aliaksandr Voitau

unread,
Nov 25, 2014, 4:10:19 PM11/25/14
to loopb...@googlegroups.com
Thanks for the reply, I will submit a ticket.
Reply all
Reply to author
Forward
0 new messages