从具有HasManyThrough关系的模型查询-Strongloop API
这是对之前问题href ="href =" https://stackoverflow.com/questions/34101531/level-2-related-model-scope-over-rest-strongloop-api>一个>.当前,该api可以从共享关系的category
和game
模型中查询.例如,通过此端点/Categories/1001/games/mature
,我可以列出fighting
类别的所有游戏,这些游戏的mature
设置为true
.但是,我从数据库表game_info
中加入了第三个模型gameInfo
.由于我想从这三个表中获取信息,因此我从数据库表games_categories_bridge
中添加了一个名为gamesCategoriesBridge
的直通模型.我按照指南设置了 HasManyThrough关系.问题是最终结果中没有显示诸如description
和publishedDate
之类的其他信息.如何正确设置remoteMethod
来完成以下任务?
This is a follow up to a previous question. Currently, the api can query from the category
and game
model which share a relation. For example, through this endpoint /Categories/1001/games/mature
I can list all games of fighting
category that have mature
set to true
. However, I have included a third model gameInfo
from db table game_info
. Since, I want to fetch the information from those three tables, i have included a through model named gamesCategoriesBridge
from db table games_categories_bridge
. I followed the guidelines to set HasManyThrough relations. The issue is that the additional information such as description
and publishedDate
doesnt show in the final result. How could I properly set the remoteMethod
to accomplish the below?
common/models/category.js
common/models/category.js
module.exports = function(Category) {
Category.mature = function(id, callback) {
var app = this.app;
var Game = app.models.Game;
Game.find({
"where": {
categoryId: id,
mature: true
}
}, function(err, gameArr) {
if (err) return callback(err);
console.log(gameArr);
callback(null, gameArr);
});
}
Category.remoteMethod(
'mature', {
accepts: [{
arg: 'id',
type: 'number',
required: true
}],
// 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'
}
}
);
};
表架构:
类别
category_name category_id
------------- -----------
fighting 1001
racing 1002
sports 1003
游戏
game_id game_name category_id mature
----------- ------------ ----------- --------------
13KXZ74XL8M Tekken 10001 true
138XZ5LPJgM Forza 10002 false
game_info
game_id description published_date
----------- ----------- --------------
13KXZ74XL8M Published by Namco. 1994
138XZ5LPJgM Published by Microsoft Studios. 2005
games_categories_bridge
game_id category_id
----------- -----------
13KXZ74XL8M 10001
138XZ5LPJgM 10002
端点:/categories/{id}/games/mature
API响应所需的格式:
Endpoint: /categories/{id}/games/mature
Desired Format for API Response:
games [
{
gameName: 'Tekken',
gameInfo :
[
{
description : 'Published by Namco.',
published_date : '1994'
}
],
categorName: 'fighting',
categoryId: 1001,
mature: true
}
.....
]
首先在game
和game_info
模型之间创建hasMany
关系
First create a hasMany
relation between game
and game_info
model
//Now inside remote_method.
Category.mature = function(id, callback) {
var app = this.app;
var Game = app.models.game;
Category.findById(id, {}, function(err, category) {
if (err) return callback(err);
//Now call the Game find method
Game.find({
"where": {
categoryId: id,
mature: true
},
include:'game_info'
}, function(err, gameArr) {
if (err) return callback(err);
gameArr.forEach(function(gameObj, index){
gameObj.categoryName = category.category_name;
});
callback(null, gameArr);
});
});
}