3

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

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(字符串 fe)的属性。

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

谢谢!

4

2 回答 2

1

我认为目前 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 核心制作精良。可能核心提交者太忙了,无法处理所有功能请求。让我们做出贡献!

于 2014-12-18T16:00:54.983 回答
0

似乎“人口”在sails v0.12中运行良好。我测试了它的第一级 - 关联并且工作正常。但是仍然没有找到一种可用且可靠的方法来仅获得满足深度关联中某些标准的主导模型记录->这是原始问题,甚至是在第 2 或第 3 级深度。编写自定义控制器是目前唯一的方法,或者用更多逻辑来检修客户端来处理这种“反向”搜索,这就是我现在的做法。

首先,我找到满足所需标准的关联模型,然后从这个集合中取出“不同”的主要记录 id。它通常意味着对服务器/级别至少有一个或两个以上的请求,因为最终查询具有主导模型 ID 的“OR”数组。

此外,向某个 url 发出 POST 请求以实际获取数据并不是一个好主意,因为它违反了 REST 原则的核心:https ://spring.io/understanding/REST

于 2016-05-21T20:06:31.267 回答