1

I found this example to use the Node API to apply filters to related models, but I was wondering if it was possible to achieve the same result using REST?

Node Example:

Post.find({
  include: {
    relation: 'owner', // include the owner object
      scope: { // further filter the owner object
      fields: ['username', 'email'], // only show two fields
      include: { // include orders for the owner
        relation: 'orders', 
        scope: {
          where: {orderId: 5} // only select order with id 5
        }
      }
    }
  }
}, function() { ... });

The closest version of a REST url I can get to work is:

...?filter[include][owners][orders]

Is it possible to create a REST url that behaves the same way as the above Node example, by limiting the results based on a related model filter... in this case orders?

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
dave-o
  • 47
  • 5
  • Have you seen https://github.com/strongloop/loopback-example-model-relations? – superkhau Mar 03 '15 at 04:25
  • Yes, but there wasn't an example using REST where filters are applied to the related models. I did find that you can just stringify the Node API JSON and use it as a query string, so that works, though it's not quite as easy to read as the REST version of the url. – dave-o Mar 03 '15 at 16:32
  • There are examples at the bottom of http://docs.strongloop.com/display/public/LB/Include+filter – superkhau Mar 03 '15 at 17:41
  • That said, you might be looking for `...?filter[include][orders][where][id]=5&fields[username]=true&fields[email]=true`. I haven't tested this though. – superkhau Mar 03 '15 at 17:44
  • I'll give that a shot and report back. Thanks! – dave-o Mar 03 '15 at 18:26

1 Answers1

0

I have this functions so when I call the Hdates/coming REST API it shows the events with date greater than today and also includes the venues... Hope it helps.

  Hdate.coming = function(cb) {
    Hdate.find({         
         where : { 
            event_date :{gt: Date.now()}
          },
          include : {
            relation: 'event', 
            scope : {
              include: {
                relation: 'venue'
              }
            }
          }   
    }, cb);
  };
  Hdate.setup = function() {
    Hdate.base.setup.apply(this, arguments);
    this.remoteMethod('coming', {
      description: 'Find Coming Events by Date',
      returns: {arg: 'events', root: true},
      http: { verb: 'GET' }
    });
  };
  Hdate.setup();