sails.js蓝图按关系查询 [英] sails.js Blueprint query by relations

查看:253
本文介绍了sails.js蓝图按关系查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PostgreSQL支持下运行帆 0.10.5 ,我想问一下是否有任何方法可以进行查询以按关系过滤结果。例如:

I'm running sails 0.10.5 with postgresql support and I want to ask if there is any way to do a query to filter results by relations. For example:

http://api/documents?user.role=Admin

http://api/documents?where={"user.role": "Admin"}

http://api/documents?where={"user.role": {"like": "%Admin%"}}

其中模型文档具有与User的属属关系,该属性具有称为角色的属性(字符串fe)。

Where a model Document has a belongsTo relation to User which has an attribute called role (string f.e.).

我无法执行此类查询,我遗漏了一些东西吗?

I wasn't able to do such query, Am I missing something?

谢谢!

推荐答案

我认为暂时无法使用SailsJS 0.10.5。其实我想做同样的事情,所以我决定为此目的实施一个快速hack。

I don't think that it's possible with SailsJS 0.10.5 for now. Actually I would like to do the same thing, so I decided to do implement a quick hack for this purpose.

打开文件 sails / lib / hooks / blueprints / actionUtil.js ,编辑方法 populateEach 如下:

Open file sails/lib/hooks/blueprints/actionUtil.js, edit method populateEach like below:

populateEach: function ( query, req ) {
    var DEFAULT_POPULATE_LIMIT = sails.config.blueprints.defaultLimit || 30;
    var _options = req.options;
    var aliasFilter = req.param('populate');
    var shouldPopulate = _options.populate;

    // Convert the string representation of the filter list to an Array. We
    // need this to provide flexibility in the request param. This way both
    // list string representations are supported:
    //   /model?populate=alias1,alias2,alias3
    //   /model?populate=[alias1,alias2,alias3]
    if (typeof aliasFilter === 'string') {
        aliasFilter = aliasFilter.replace(/\[|\]/g, '');
        aliasFilter = (aliasFilter) ? aliasFilter.split(',') : [];
    }

    return _(_options.associations).reduce(function populateEachAssociation (query, association) {        
        // If an alias filter was provided, override the blueprint config.
        if (aliasFilter) {
            shouldPopulate = _.contains(aliasFilter, association.alias);
        }

        // Only populate associations if a population filter has been supplied
        // with the request or if `populate` is set within the blueprint config.
        // Population filters will override any value stored in the config.
        //
        // Additionally, allow an object to be specified, where the key is the
        // name of the association attribute, and value is true/false
        // (true to populate, false to not)
        if (shouldPopulate) {
            // IMPORTANT NOTE: This is my trick. We should take advanced options from request parameter to make requests even more flexible
            var populationOptions = req.param('populate_' + association.alias);

            if (!populationOptions) {
                var populationLimit = _options['populate_' + association.alias+'_limit'] ||
                                      _options.populate_limit ||
                                      _options.limit ||
                                      DEFAULT_POPULATE_LIMIT;
                populationOptions = {limit: populationLimit};
            }

            return query.populate(association.alias, populationOptions);
        }
        else { 
            return query;
        }
    }, query);
},

是的!现在,您的API可以处理如下所示的其他关联过滤器:

Yay! Now your API can handle additional association filters like below:

# POST /api/documents
{
    "where" : {
        // Normal conditions
    }
    "populate_user": {
        // Advanced condition for association 'admin'
        "where" : {
            "role" : {
                 "like": "%Admin%"
            }
        },
        "limit" : 4    
     }
}

我希望对您有所帮助。顺便说一下,我明天会花时间将这一改进的请求请求发送到SailsJS内核。

I hope that it helps. By the way I will find time to send a pull request of this improvement to SailsJS core tomorrow.

P / S:SailsJS内核非常出色。核心提交者可能太忙而无法处理所有功能请求。让我们贡献力量吧!

P/S: SailsJS core is quite well made. Probably core committers are just too busy to handle all feature requests. Let's contribute!

这篇关于sails.js蓝图按关系查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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