Filter Model by a Property on a Related Model

345 views
Skip to first unread message

Jennifer Louthan

unread,
Aug 7, 2014, 3:02:46 PM8/7/14
to loopb...@googlegroups.com
Hi there.

I have two models:  posts and tags. They have a hasAndBelongsToMany relationship with one another. I want to filter posts by their tags over REST, something like "give me all the posts with the tag with id 12345". But I also want to preserve other filtering capabilities for the posts, and don't see anything in the docs that would accomplish this. I've been looking here http://docs.strongloop.com/display/LB/Include+filter.

I know I can GET /tags/:tagId/posts, but the problem with this is that I can't apply any query strings to further filter the posts, for example: 
GET /tags/:tagId/posts?filter[include]=tags does not add a tags array to the post objects in the response. 

I've tried something like GET /posts?filter[include]=tags?filter[where][id]=12345 with no luck. This seems like something that might exist but be undocumented or that I just couldn't find. Is there a query string that accomplished what I want? Or is there any way to force the /tags/:tagId/posts endpoint to apply filters?

Thanks so much!!

Jenny

Jennifer Louthan

unread,
Aug 8, 2014, 2:25:07 PM8/8/14
to loopb...@googlegroups.com
I was hoping to get some answers on this. Otherwise, I think must go ahead and override the /tags/:tagId/posts endpoint and manually look for filters in the query string and apply them myself. Is there currently no way around this?

Raymond Feng

unread,
Aug 8, 2014, 2:31:14 PM8/8/14
to Jennifer Louthan, loopb...@googlegroups.com
I don’t think we support recursive filtering at the moment for two reasons:

1. We’re trying to improve strong-remoting to support hierarchical uri to JS methods resolution. See discussions at https://github.com/strongloop/loopback/pull/435
2. It’s hard to express the nesting filters on a URL :-(

BTW, this new feature might be useful in your context:


Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jennifer Louthan

unread,
Aug 8, 2014, 2:55:19 PM8/8/14
to loopb...@googlegroups.com, jbllo...@gmail.com
Thanks for your response! Makes sense that nesting filters in a URL gets messy. I would expect to be able to apply filters to the URL  /tags/:tagId/posts endpoint though, but it doesn't look like that's supported, unless I'm missing something?

Jennifer Louthan

unread,
Aug 8, 2014, 3:19:23 PM8/8/14
to loopb...@googlegroups.com, jbllo...@gmail.com
Okay so now I'm attempting to pass a query from the URL to a relation method, however the following does not work:

tag.posts({ include: 'tags' }, function(err, posts){
if(err)return next(err);
res.send(200, posts);
});

The tags array is not included in the posts response, but from the docs it looks like it should work:  http://docs.strongloop.com/display/LB/HasAndBelongsToMany+relations#HasAndBelongsToManyrelations-Methodsaddedtothemodel

Am I doing something wrong or missing some syntax somewhere?

Miroslav Bajtoš

unread,
Aug 12, 2014, 4:23:53 AM8/12/14
to loopb...@googlegroups.com
On Thursday, August 7, 2014 9:02:46 PM UTC+2, Jennifer Louthan wrote:
I have two models:  posts and tags. They have a hasAndBelongsToMany relationship with one another. I want to filter posts by their tags over REST, something like "give me all the posts with the tag with id 12345". But I also want to preserve other filtering capabilities for the posts, and don't see anything in the docs that would accomplish this. I've been looking here http://docs.strongloop.com/display/LB/Include+filter.

I know I can GET /tags/:tagId/posts, but the problem with this is that I can't apply any query strings to further filter the posts, for example: 
GET /tags/:tagId/posts?filter[include]=tags does not add a tags array to the post objects in the response. 

What version of loopback do you use? This issue should have been fixed in loopback v1.7.4 and v2.1.1, see the following pull requests:


I've tried something like GET /posts?filter[include]=tags?filter[where][id]=12345 with no luck. This seems like something that might exist but be undocumented or that I just couldn't find. Is there a query string that accomplished what I want? Or is there any way to force the /tags/:tagId/posts endpoint to apply filters?

You can't query models related via a "through" table, which is the case of "hasAndBelongsToMany" relationship. There is already a GH issue tracking this request:


Feel free to leave a comment there, it will increase our priority for the issue.

Miroslav
Reply all
Reply to author
Forward
0 new messages