从具有 HasManyThrough 关系的模型中查询 - strongloop api

Querying from models with HasManyThrough relations - strongloop api

这是对之前 . 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 的跟进。问题是 descriptionpublishedDate 等附加信息没有显示在最终结果中。我怎样才能正确设置 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'
        }
    }
);

};

Table schema:

类别

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 响应所需的格式:

games [ 
{
gameName: 'Tekken', 
gameInfo : 
[
    { 
        description : 'Published by Namco.',
        published_date : '1994'
    }
],
        categorName: 'fighting', 
        categoryId: 1001, 
        mature: true 
}
.....
]

首先在 gamegame_info 模型之间创建一个 hasMany 关系

//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);
        });
    });
}