How to include related entities in REST with loopback.io

依然范特西╮ 提交于 2019-12-06 02:30:39

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!