本文介绍了REST的第2级(相关模型)范围-Strongloop API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文档中发现范围使您可以指定常用查询,这些查询可以作为模型上的方法调用进行引用.下面我有一个categories模型.我正在尝试创建适用于与模型games的关系的范围.不幸的是,以下内容无济于事.如何获得适用于关系的范围,如下所示?

I found in the documentation that scopes enable you to specify commonly-used queries that you can reference as method calls on a model. Below i have a categories model. I am trying to create scopes that applies to the relation with model games. Unfortunately the below does nothing. How can I get scopes to apply to relation as shown below?

GET /Categories/{id}/games-这将获得所有游戏

common/models/category.json

"relations": {
    "categories": {
      "type": "hasMany",
      "model": "game",
      "foreignKey": ""
    }
},

/common/models/game.json

 "scopes": {
    "mature": {"where": {"mature": true}}
  },
  "validations": [],
  "relations": {
    "category": {
      "type": "belongsTo",
      "model": "category",
      "foreignKey": ""
    }
  }

我希望能够通过Endpoing获取数据:/Categories/{id}/games/mature

I want to be able to get the data through endpoing: /Categories/{id}/games/mature

表架构:

catgories

category_name       category_id 
-------------       -----------
fighting            1001
racing              1002
sports              1003

games

game_id         game_name           category_id     mature
-----------     ------------        -----------     --------------
13KXZ74XL8M     Tekken              10001           true
138XZ5LPJgM     Forza               10002           false

推荐答案

回送api基于swagger,而scopes是回送中的新概念.

Loopback api is based on swagger and scopes is a new concept in loopback.

当前它不支持相关方法,即,您不能从相关模型(例如category)访问(仅在您的情况下),而只能从定义了scope的模型(即game)访问.

Currently it doesn't have support for related methods i.e. you cannot access it from a related model i.e category (in your case) but only from model where the scope is defined i.e. game.

因此,您现在可以使用远程方法实现所需的目标.

Thus you can achieve what you want right now using remote methods.

使用Remote Methods . 环回远程方法

module.exports = function(Category) {

    Category.mature = function(id, filter, callback) {
        var app = this.app;
        var Game = app.models.Game;
        if(filter === undefined){
             filter = {};
        }

        filter.where  = filter.where || {};

        filter.where.categoryId =  id;
        filter.where.mature = true;

        Game.find(filter, function(err, gameArr) {
            if (err) return callback(err);
            console.log(gameArr);
            callback(null, gameArr);
        });
    }

    Category.remoteMethod(
        'mature', {
            accepts: [{
                arg: 'id',
                type: 'number',
                required: true
            },
            {
                arg: 'filter',
                type: 'object',
                required: false
            }
            ],
            // mixing ':id' into the rest url allows $owner to be determined and used for access control
            http: {
                path: '/:id/games/mature',
                verb: 'get'
            },
            returns: {
                arg: 'games',
                type: 'array'
            }
        }
    );

};
.....
.....
"acls": [
  {
     "principalType": "ROLE",
     "principalId": "$everyone",
     "permission": "ALLOW",
     "property": "mature"
  }
] 

现在您可以通过get method获得所有成熟类型的游戏 http://0.0.0.0:3000/api/categories/:id/games/mature

Now you can get all your game of mature type by get method http://0.0.0.0:3000/api/categories/:id/games/mature

或者您现在也可以尝试从loopback-explorer开始使用API​​.

Or you can also try the API from loopback-explorer now.

这篇关于REST的第2级(相关模型)范围-Strongloop API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:39