lookback API where 具有多个条件的过滤器

lookback API where filter with multiple conditions

当使用环回 API 时,'AND' 运算符在具有多个条件的 'where' 过滤器中是否多余? 例如,我测试了以下两个查询,它们 return 相同的结果:

<model>.find({ where: { <condition1>, <condition2> } });
<model>.find({ where: { and: [<condition1>, <condtion2>] } });

更具体地说,假设这是 table 内容:

name   value
----   -----
a      1
b      2

当我使用两个不同的 'where' 过滤器执行 'find()' 时,在这两种情况下我都获得了第一条记录:

{ where: { name: 'a', value: 1 } }
{ where: { and: [ { name: 'a'}, { value: 1 } ] } }

我已经通读了API文档,但没有找到在有多个条件时使用什么逻辑运算符。 如果 'AND' 如我的测试所示是多余的,我宁愿不使用它。但我只是想确定这在一般情况下是否正确,或者它是否恰好适用于我正在使用的 postgreSQL。

这是一个有效的查询,只能用 and 语句完成。

  {
    "where": {
      "or": [
        {"and": [{"classification": "adn"}, {"series": "2"}]},
        {"series": "3"}
      ]
    }
  }

编辑:https://github.com/strongloop/loopback-filters/blob/master/index.js

function matchesFilter(obj, filter) {
  var where = filter.where;
  var pass = true;
  var keys = Object.keys(where);
  keys.forEach(function(key) {
    if (key === 'and' || key === 'or') {
      if (Array.isArray(where[key])) {
        if (key === 'and') {
          pass = where[key].every(function(cond) {
            return applyFilter({where: cond})(obj);
          });
          return pass;
        }
        if (key === 'or') {
          pass = where[key].some(function(cond) {
            return applyFilter({where: cond})(obj);
          });
          return pass;
        }
      }
    }
    if (!test(where[key], getValue(obj, key))) {
      pass = false;
    }
  });
  return pass;
}

它遍历查找失败的 where 对象的键,因此在您的情况下它就像一个隐式 and 语句。

编辑 2:https://github.com/strongloop/loopback-datasource-juggler/blob/cc60ef8202092ae4ed564fc7bd5aac0dd4119e57/test/relations.test.js

环回数据源 juggler 包含使用隐式 and 格式的测试

{PictureLink.findOne({where: {pictureId: anotherPicture.id, imageableType: 'Article'}},

{pictureId: anotherPicture.id, imageableId: article.id, imageableType: 'Article',}

But I just want to make sure if this is true in general, or if it just happens to work with postgreSQL which I'm using.

大体上是这样吗?编号

这似乎是针对 SQLConnector 中的 PostgreSQL 和 MySQL(可能还有其他 SQL 数据库)处理的。因此,不使用 SQLConnector(例如 MongoDB)的连接器可能不支持此功能。但是,考虑到我在网上看到的许多示例,我认为可以肯定地假设其他连接器也以这种方式实现了它。