Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback.io Multiple Include Model Query Conditions

For a single model query, we can apply conditional operations on the conditions defined in a query, where the and/or operator is applied to the series of conditions:

{ where: {<and|or>: [condition1, condition2, ...]}}

Is there a neat way to apply conditional operations for the models included in include?

As an example:

Model.find({
    include: [
        {
            relation: "relation1",
            scope: {
                where: { condition1 }
            }
        },
         {
            relation: "relation2",
            scope: {
                where: { condition2 }
            }
        },
        ...
    ]
});

Is it possible to apply and/or operation on condition1, condition2 above, i.e. return all models such that they match the conditions defined in the scope of the included models.

like image 481
HioLeong Avatar asked Sep 08 '26 03:09

HioLeong


1 Answers

I believe you can do this as per this code 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() { ... });

This is documented here for the Include filter

like image 82
JSimonsen Avatar answered Sep 10 '26 16:09

JSimonsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!