node.js - sails.js 蓝图按关系查询

标签 node.js postgresql sails.js blueprint

我正在运行带有 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%"}}

其中模型文档与用户有 belongsTo 关系,用户具有称为角色(字符串 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 核心制作精良。可能核心提交者太忙了,无法处理所有功能请求。让我们做出贡献!

关于node.js - sails.js 蓝图按关系查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27275641/

相关文章:

javascript - 返回 JavaScript Promise 拒绝的已完成值

node.js - 如何在 node-oidc-provider 中重定向失败的登录尝试

r - 使用 R 在 postgres 中设置模式名称

node.js - 根据 MVC,哪个项目结构是正确的?

javascript - 限制 Mongoose 中查询人口后的结果数量

node.js - npm 删除无关包

c# - 在单引号中设置参数值的Postgres准备语句错误

java - CMD (Java) 中的 Postgres Restore 不再响应,而相同的命令在 CMD 中执行良好

node.js - 定时器删除记录+一个条件

javascript - 将 sails.js 变量发送到 Javascript 文件