bookshelfjs中的多个orderBy()列

时间:2016-11-14 11:19:46

标签: javascript node.js express bookshelf.js knex.js

如何在bookshelfJs中实现多个orderBy

我可以在模型中添加尽可能多的orderBy,但在API中可以有任何排序选项 像example.com/users?sort=-name,status一样,它不需要硬编码。

以下答案对于要求似乎是合法的

Knex.js multiple orderBy() columns

如何在Bookshelf中实现多个orderBy?

模型/ Users.js

var Bookshelf = require('../../dbConfig').bookshelf;

var User = Bookshelf.Model.extend({
    tableName: 'user_table'
});
var Users = Bookshelf.Collection.extend({
    model: User
});

module.exports = {
    User: User,
    Users: Users
};

services.js

var Model = require('./../models/Users');
var express = require('express');

var listAllContentProviders = function (query_params, callback) {
Model.Users
        .forge()
        .orderBy("name")
        .orderBy("-status")
        .fetch()
        .then(function (collection) {
            return callback(null, collection);
        })
        .catch(function (err) {
            return callback(err, null);
        });  
};

2 个答案:

答案 0 :(得分:1)

querystring获取url

   var queryData = url.parse(request.url, true).query;
    var sortArray = [];
    if (queryData.sort) {
        sortArray.push({field: queryData.sort , 'direction': 'asc'};
   }

现在sortArray包含所有排序字段,

现在使用您添加的答案(Knex.js multiple orderBy() columns)来完成您的要求

knex
  .select()
  .table('products')
  .modify(function(queryBuilder) {
    _.each(sortArray, function(sort) {
      queryBuilder.orderBy(sort.field, sort.direction);
    });
  })

未经测试:

 Model.Users
    .forge()
    .modify(function(queryBuilder) {
         _.each(sortArray, function(sort) {
            queryBuilder.orderBy(sort.field, sort.direction);
         })
    })
    .fetch()
    .then(function (collection) {
        return callback(null, collection);
    })
    .catch(function (err) {
        return callback(err, null);
    });  
  

新修改:

尝试这样的事情,

    queryBuilder = Model.Users
    .forge()
    .fetch();
    sortArray.forEach( function(sort) {
        queryBuilder.orderBy(sort.field, sort.direction);
    });
    queryBuilder.then(function (collection) {
        return callback(null, collection);
    })
    .catch(function (err) {
        return callback(err, null);
    });  

如果您在解决承诺方面遇到任何问题,请将其修改为

    queryBuilder = Model.Users
    .forge();
    queryBuilder.then(function(){
        sortArray.forEach( function(sort) {
            queryBuilder.orderBy(sort.field, sort.direction);
        })
    }).fetch().then(function (collection) {
        return callback(null, collection);
    })
    .catch(function (err) {
        return callback(err, null);
    });  

答案 1 :(得分:0)

var sortArray = [];
var expectedSortFields = ["id", "status"]; // where we want the sorting remaining parameter will be ignored.

for (var x in query_params) {
        if (x === 'sort') {
            var sortFields = query_params[x].split(',');
            for (var y in sortFields) {
                if (expectedSortFields.includes(sortFields[y])) {
                    sortArray[y] = {
                        'field': sortFields[y],
                        'direction': 'desc' //just made it desc for test, this can also be checked basedon the sign given in API '-' for desc and by default asc
                    }
                }
            }
        }

    }

Model. Users
        .forge()
        .query(function (qb) {
            for (var i in sortArray) {
                qb.orderBy(sortArray[i].field, sortArray[i].direction);
            }
        })
        .then(function (collection) {
            return callback(null, collection);
        })
        .catch(function (err) {
            return callback(err, null);
        });