使用 LoopBack JS 的多级包含过滤器

Multi-level include filter with LoopBack JS

我的问题是我无法弄清楚如何使用 LoopBack 后端在一个请求中获取多级关系结构。我有 3 个模型:ContinentCountryCounty。我想做的是获取一个大陆,并接收所有国家,以及其中的所有县。

他们之间的关系:

所以 REST api 调用 /api/Continent/1 returns

{
   "id": 1
   "name":"Europe"
}

现在,我想获取 Continent 的所有国家和县,所以我查询 /api/Continent/1?filters[include]=country

仍然,我不了解县。

我应该进行什么样的查询才能获得包含两个关系级别的列表? 像这样:

{
  "id": 1,
  "name": "Europe",
  "country": [
    id: 1,
    name:"United Kingdom",
    county:[
      {id:1,name:"Avon"},
      {id:2,name:"Bedfordshire"},
      ...
    ],
    ...
  ]
}

感谢您的帮助!

您好,希望现在回答还为时不晚。在彻底翻阅他们关于这个问题的文档后,我最终编写了一个远程方法来实现深层次的多重包含。目前还不清楚如何在 REST api 级别进行处理。

Continent.listContinents = function(limit,skip,order,cb) {
Continent.find({
  limit:limit,
  skip:skip,
  order:order,
  include:[{country:'county'}],
}, cb);
};

Continent.remoteMethod('listContinents', {
description:"List continents. Include the related country and county information",
returns: {arg: 'continents', type: 'array'},
accepts: [{arg: 'limit', type: 'number', http: { source: 'query'  }},
          {arg: 'skip', type: 'number', http: { source: 'query'  }},
          {arg: 'order', type: 'string', http: { source: 'query'  }}],
          http: {path:'/list', verb: 'get'}
});

我添加了一些额外的查询字符串参数limit、order 和 skip 以启用分页和排序..但不是必须的:)

此外,这还假设您已经在 Continent 和 Country 之间定义了关系类型,然后在 Country 和 County 之间定义了关系类型。

语法是:

/api/Continent/1?filter={"include": {"country": "countys"}}