从具有HasManyThrough关系的模型查询-Strongloop API [英] Querying from models with HasManyThrough relations - strongloop api

查看:51
本文介绍了从具有HasManyThrough关系的模型查询-Strongloop API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对之前问题一个>.当前,该api可以从共享关系的categorygame模型中查询.例如,通过此端点/Categories/1001/games/mature,我可以列出fighting类别的所有游戏,这些游戏的mature设置为true.但是,我从数据库表game_info中加入了第三个模型gameInfo.由于我想从这三个表中获取信息,因此我从数据库表games_categories_bridge中添加了一个名为gamesCategoriesBridge的直通模型.我按照指南设置了 HasManyThrough关系.问题是最终结果中没有显示诸如descriptionpublishedDate之类的其他信息.如何正确设置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 
}
.....
]

推荐答案

首先在gamegame_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);
        });
    });
}

这篇关于从具有HasManyThrough关系的模型查询-Strongloop API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆