我正在做一个学习Node.js + express + Bookshelf.js的个人项目。我在哪里构建查询?特别是,我如何在下面的代码中简单地设置一个“ORDER BY”或“WHERE”?
var Accounts = require('../collections/accounts').collection;
new Accounts().fetch({
withRelated: ['folders']
}).then(function(collection) {
// process results
});我想学习Bookshelf.js,因为它似乎提供了我在Laravel's Elequent中使用过的特性,比如多态关系和子表达式。但是,我发现文档不是很深入,几乎不可能找到示例。
提前感谢您的帮助。
罗宾
发布于 2014-02-27 20:47:33
啊,刚刚找到了我问题的答案。
正如bookshelf.js网站所说,它使用knex.js查询构建器。因此,为了对我的集合进行排序,我所做的是:
var Accounts = require('../collections/accounts').collection
new Accounts().query(function(qb){
qb.orderBy('name','DESC');
}).fetch({
}).then(function(collection){
// process results
});..。这很好用!
发布于 2015-03-20 08:04:08
我知道这是一个古老的帖子,但这是我对它的看法。BookshelfJS很神奇,但它缺少一些简单的功能。因此,我创建了自己的基本模型,名为Closet。
对于orderBy,Closet是这样的:
var Closet = DB.Model.extend({
/**
* Orders the query by column in order
* @param column
* @param order
*/
orderBy: function (column, order) {
return this.query(function (qb) {
qb.orderBy(column, order);
});
}
});我的其他模型使用的是Closet而不是Bookshelf.Model。然后,您可以直接使用orderBy
new Accounts()
.orderBy('name', 'DESC')
.fetch()
.then(function(collection){
// process results
});发布于 2015-01-26 10:10:00
您也可以简单地这样做
var Accounts = require('../collections/accounts').collection;
new Accounts().query('orderBy', 'columnname', 'asc').fetch({
withRelated: ['folders']
}).then(function(collection) {
// process results
});而不需要到达查询构建器。
https://stackoverflow.com/questions/22068144
复制相似问题