Constraint and offset with Bookshelf.js
I am using the below code to get all results from a table:
Search.forge()
.fetchAll()
.then(function (collection) {
res.json({error: false, data: collection.toJSON()});
})
But I would need pagination and thought I could do it with constraint and offset, but so far I haven't found anything to provide me with options for constraint and offset.
Is this possible or do I need to build my query with Knex?
+3
source to share
3 answers
The Knex query builder should help, you must first call the collection.query()
bookshelf to click on the query chain builder
Models.forge()
.query(function(qb) {
//qb is knex query builder, use knex function here
qb.offset(0).limit(10);
})
.fetchAll().then(function(result) {
res.json(result.toJSON());
})
so the bucket builder function -> http://knexjs.org/#Builder is now available for free
+7
source to share