【发布时间】:2015-12-12 07:18:54
【问题描述】:
这是对之前question 的后续跟进。目前,该api可以从共享关系的category和game模型中查询。例如,通过此端点/Categories/1001/games/mature,我可以列出所有fighting 类别的游戏,其中mature 设置为true。但是,我已经包含了来自 db 表 game_info 的第三个模型 gameInfo。因为,我想从这三个表中获取信息,所以我从 db 表 games_categories_bridge 中包含了一个名为 gamesCategoriesBridge 的直通模型。我按照指南设置HasManyThrough relations。问题是description 和publishedDate 等附加信息不会显示在最终结果中。如何正确设置remoteMethod 来完成以下任务?
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_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 响应的所需格式:
games [
{
gameName: 'Tekken',
gameInfo :
[
{
description : 'Published by Namco.',
published_date : '1994'
}
],
categorName: 'fighting',
categoryId: 1001,
mature: true
}
.....
]
【问题讨论】:
-
您想要的 API 响应格式具有
game_info属性,但它显示了与category相关的信息,为什么会这样。另外,我认为您需要重新设计数据库的架构。因为使用这个模式你可以实现你想要的,但事情会变得比平常困难。 -
@RobinsGupta,对不起,错字。更正了所需的 API 响应。
-
games->category model之间是什么关系?每个game属于一个category还是每个game可以有多个category? -
@RobinsGupta 每个游戏可以有很多类别
标签: node.js loopbackjs strongloop