How to include related entities in REST with loopback.io
Asked Answered
T

2

3

I'm using Strongloop's loopback tool to create a REST service. I'm wondering how to define what related entities to return when requesting a model.

I see in the docs that you can send a request like GET /api/members?filter[include]=posts and that will return the related post models, and I discovered that you can make a request like GET /api/members?filter[include]=posts&filter[include]=comments to get posts and comments, but is there a way to define either in code or the generated json file that you'd like a certain relation to always be returned when requesting a model?

Tobytobye answered 18/9, 2014 at 4:2 Comment(0)
L
1

The preset filter properties are referred as default scope. We have a pending pull request to support that. Please see https://github.com/strongloop/loopback-datasource-juggler/pull/296.

As a workaround before the feature is released, you can use beforeRemote hooks to update the filter object with your default scope. See http://docs.strongloop.com/display/LB/Defining+remote+hooks.

Lobel answered 23/9, 2014 at 23:41 Comment(4)
Thanks for the info. Very excited about this project.Tobytobye
The link is not valid anymore, but I am really curious about how to override filter in a beforeRemote, any hints?Glomerulus
Something like ctx.args.filter = {include: 'posts'}; in a beforeRemote does the trickGlomerulus
Today the preset filters are support. Add this to your model.json file: "scope": { "include": { "relation": "channel", "scope": { "fields": ["code", "description"] } } }. Don't forget to add a belongsTo relation to the other model. And to the other model add a hasMany relation to this model. Raymond, maybe you can update your answer to reflect this.Kiefer
D
3

You can use two different easy methods to get relationships to the account.

  1. Using Model definitions in the Model.json file.

    "validations": [],
    "relations": {
      "team": {
      "type": "belongsTo",
      "model": "Team",
      "foreignKey": ""
    },
    "user": {
      "type": "belongsTo",
      "model": "User",
      "foreignKey": ""
    }
    }
    

This will always bind one model with another models using direct relationships and you can retrieve them using following code lines.

app.models.TeamRole.findOne({
      where: {
        userId: user.id
      },
      include:[ {
        relation: 'team'
      },
{
        relation: 'user'
      } ]
    },function(err,team,user){
//retrieve relational data here
});
  1. You can use operational hooks concept to obtain such kinds of relations easily.

Cheers.

Deficit answered 30/9, 2017 at 19:49 Comment(0)
L
1

The preset filter properties are referred as default scope. We have a pending pull request to support that. Please see https://github.com/strongloop/loopback-datasource-juggler/pull/296.

As a workaround before the feature is released, you can use beforeRemote hooks to update the filter object with your default scope. See http://docs.strongloop.com/display/LB/Defining+remote+hooks.

Lobel answered 23/9, 2014 at 23:41 Comment(4)
Thanks for the info. Very excited about this project.Tobytobye
The link is not valid anymore, but I am really curious about how to override filter in a beforeRemote, any hints?Glomerulus
Something like ctx.args.filter = {include: 'posts'}; in a beforeRemote does the trickGlomerulus
Today the preset filters are support. Add this to your model.json file: "scope": { "include": { "relation": "channel", "scope": { "fields": ["code", "description"] } } }. Don't forget to add a belongsTo relation to the other model. And to the other model add a hasMany relation to this model. Raymond, maybe you can update your answer to reflect this.Kiefer

© 2022 - 2024 — McMap. All rights reserved.