问题
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 - This gets all 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": ""
}
}
I want to be able to get the data through endpoing: /Categories/{id}/games/mature
Table schema:
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
回答1:
Loopback api is based on swagger and scopes is a new concept in loopback.
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.
By Using 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"
}
]
Now you can get all your game of mature type by get method
http://0.0.0.0:3000/api/categories/:id/games/mature
Or you can also try the API from loopback-explorer now.
来源:https://stackoverflow.com/questions/34101531/level-2-related-model-scope-over-rest-strongloop-api