Search code examples
loopbackjsstrongloop

Multi-level include filter with LoopBack JS


My problem is that I can't figure out how to get multilevel relations structures in one request with LoopBack backend. I have 3 models: Continent, Country, County. What I would like to do is to GET a continent, and recieve all the countries, and all the counties within.

The relationship between them:

  • Continent hasMany Country, and Country belongsTo Continent
  • Country hasMany County, and County belongsTo Country

So the REST api call to /api/Continent/1 returns

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

Now, I want to get all the countries and counties with the Continent, so I do a query to /api/Continent/1?filters[include]=country

Still, I don't get the countys.

What kind of query should I make to get a list which includes both relation levels? Like this:

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

Thanks for your help!


Solution

  • The syntax is:

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