3
votes

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.

1

1 Answers

4
votes

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