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

本文是小编为大家收集整理的关于sails.js蓝图按关系查询的处理方法,想解了sails.js蓝图按关系查询的问题怎么解决?sails.js蓝图按关系查询问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我正在运行带有 postgresql 支持的sails 0.10.5,我想问一下是否有任何方法可以进行查询以按关系过滤结果.例如:

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

或者

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

或者

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

模型 Document 与 User 有一个 belongsTo 关系,其中有一个名为 role 的属性(字符串 f.e.).

我无法进行这样的查询,我错过了什么吗?

谢谢!

推荐答案

我认为目前 SailsJS 0.10.5 不可能.实际上我也想做同样的事情,所以我决定为此做一个快速破解.

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

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 可以处理额外的关联过滤器,如下所示:

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

我希望它会有所帮助.顺便说一句,我明天会找时间向 SailsJS 核心发送这个改进的拉取请求.

P/S:SailsJS 核心制作精良.可能核心提交者太忙了,无法处理所有功能请求.让我们做出贡献!

本文地址:https://www.itbaoku.cn/post/1764044.html