REST 上的 2 级(相关模型)范围 - strongloop api
Level 2 (related model) scope over REST - strongloop api
我在文档中发现 scopes 使您能够指定常用查询,您可以将这些查询引用为模型上的方法调用。下面我有一个 categories
模型。我正在尝试创建适用于与模型 games
的关系的范围。不幸的是,下面什么都不做。我怎样才能获得适用于关系的范围,如下所示?
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": ""
}
}
我希望能够通过端点获取数据:/Categories/{id}/games/mature
Table 架构:
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
是环回中的新概念。
目前它不支持相关方法,即您不能从相关模型访问它,即 category
(在您的情况下),但只能从定义了 scope
的模型访问,即 game
.
因此您现在可以使用远程方法实现您想要的。
通过使用 Remote Methods
。 Loopback Remote Methods
common/models/category.js add the following lines.
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'
}
}
);
};
Now in your common/models/category.json
Add this to your ACL property.
.....
.....
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"property": "mature"
}
]
现在你可以通过get method
获得你所有成熟类型的游戏
http://0.0.0.0:3000/api/categories/:id/games/mature
或者您现在也可以尝试 loopback-explorer
中的 API。
我在文档中发现 scopes 使您能够指定常用查询,您可以将这些查询引用为模型上的方法调用。下面我有一个 categories
模型。我正在尝试创建适用于与模型 games
的关系的范围。不幸的是,下面什么都不做。我怎样才能获得适用于关系的范围,如下所示?
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": ""
}
}
我希望能够通过端点获取数据:/Categories/{id}/games/mature
Table 架构:
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
是环回中的新概念。
目前它不支持相关方法,即您不能从相关模型访问它,即 category
(在您的情况下),但只能从定义了 scope
的模型访问,即 game
.
因此您现在可以使用远程方法实现您想要的。
通过使用 Remote Methods
。 Loopback Remote Methods
common/models/category.js add the following lines.
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'
}
}
);
};
Now in your common/models/category.json Add this to your ACL property.
.....
.....
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"property": "mature"
}
]
现在你可以通过get method
获得你所有成熟类型的游戏
http://0.0.0.0:3000/api/categories/:id/games/mature
或者您现在也可以尝试 loopback-explorer
中的 API。