5

I have User model over relational DB.

Each User can hasMany "users" where "chiefId" is FK.

"relations": {
    "users": {
      "type": "hasMany",
      "model": "User",
      "foreignKey": "chiefId"
    },
}

I can query related users for each chief-user like this:

GET /users?filter={"include":"users"}

But it returns full user objects.

  • How should I query only "name" properties of related users?
  • Also is it possible to count related instances in one request to server?
IvanZh
  • 2,265
  • 1
  • 18
  • 26
  • not supported it seems, see http://stackoverflow.com/questions/25903985/how-to-include-related-entities-in-rest-with-loopback-io – superkhau Nov 20 '14 at 01:10

3 Answers3

7

A late reply but I just ran into this question now. It is possible:

filter: {
 include:{
  relation: "users",
  scope: {
   fields:["name"]
  }
 }
}
Hidde Stokvis
  • 442
  • 3
  • 10
2

As far as I understood this question is about adding a nested filter on an include level, which seems to be not yet supported: https://groups.google.com/forum/#!msg/loopbackjs/T6onsYMJFOI/V4ILc3Obf3MJ

May be it's not the best way to approach this problem, but what you can do is a manual response transformation in .afterRemote('find', ...) hook.

Alex V
  • 1,155
  • 8
  • 10
0
/users?filter[fields][0]=name

See https://github.com/strongloop/loopback-example-relations-basic for more info.

ndsmyter
  • 6,535
  • 3
  • 22
  • 37
superkhau
  • 2,781
  • 18
  • 9
  • This doesn't work for included objects. And I need only "name" for **related** (or embedded) user objects! – IvanZh Nov 19 '14 at 07:32
  • do any of use cases in the example repo meet your use case? maybe this one /api/customers?filter[include][reviews]=author&filter[where][age]=21 – superkhau Nov 19 '14 at 18:36
  • I saw the use cases, but they don't limit fields returned from relation. As Alex Voitau said, this isn't supported yet. Nevertheless thanks for response! – IvanZh Nov 20 '14 at 06:57