This Bookshelf.js plugin provides a Laravel like simple pagination to your models.
Install the package via npm
:
$ npm install --save bookshelf-simplepaginate
var bookshelf = require('bookshelf')(knex);
bookshelf.plugin(require('bookshelf-simplepaginate'));
Car.query(function (qb) {
qb.innerJoin('manufacturers', 'cars.manufacturer_id', 'manufacturers.id');
qb.groupBy('cars.id');
qb.where('manufacturers.country', '=', 'Sweden');
}).simplePaginate({
limit: 100, // Defaults to 10 if not specified
page: 3, // Defaults to 1 if not specified
withRelated: ['engine'] // Passed to Model#fetchAll
}).then(function (results) {
console.log(results);
});
Output of results:
{
"data": [],
"meta": {
"pagination": {
"count_total": 1000,
"page_total": 10,
"count_per_page": 100,
"per_page": 100,
"current_page": 1,
"links": {
"previous": null,
"next": 1
}
}
}
}