Loopback.io 多个包含模型查询条件

Loopback.io Multiple Include Model Query Conditions

对于单个模型查询,我们可以对查询中定义的条件应用条件操作,其中 and/or 运算符应用于一系列条件:

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

有没有一种巧妙的方法可以为 include 中包含的模型应用条件操作?

举个例子:

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

是否可以对上面的 条件 1、条件 2 应用 and/or 操作,即 return 所有模型,使其符合范围中定义的条件包含的模型。

我相信您可以按照此代码示例执行此操作:

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() { ... });

这已记录在案 here 用于包含过滤器