在Bookshelf模型上限制Knex查询以仅返回n个logging

我有以下代码,我正在使用splice函数只传递第一个10 / JSON对象到JADE模板。

app.get('/index', function(req, res) { new models.Condos() .query('orderBy', 'age', 'asc') .fetch() .then(function(names) { var name = names.splice(0,10); res.render('index', { names: name.toJSON() }); }); }); }; 

有什么办法可以限制查询本身只返回前10条logging,而不是拼接数组来做到这一点(使用偏移量和限制参数)?

你可以写一个knex查询来达到这个目的,它看起来像这样:

 app.get('/index', function(req, res) { knex.select('*') .from('condos') .limit(10) .then(function(names) { res.render(names); }); }); 

我所寻找的是更多的东西。

 app.get('/index', function(req, res) { new models.Condos() .query('orderBy', 'age', 'asc') .query('limit','10') .fetch() .then(function(names) { var name = names.splice(0,10); res.render('index', { names: name.toJSON() }); }); }); }; 

您可以使用Bookshelf的分页插件 。

 models.Condos.fetchPage( {page:1, pageSize:10} ) 

注意:对于页面大小为10的第一页,可以省略参数并且只是

 models.Condos.fetchPage()