node-rigorous
Version:
Rigorous Framework
38 lines (30 loc) • 910 B
JavaScript
module.exports = function createPaginationMethod() {
return function (query = {}, offsetParam = 0, limitParam = 500, options) {
let offset;
let limit;
try {
offset = parseInt(offsetParam, 10);
} catch (err) {
offset = 0;
}
try {
limit = parseInt(limitParam, 10);
} catch (err) {
limit = 500;
}
const Model = this;
return new Promise((resolve, reject) => {
Model.paginate(query, Object.assign({
offset,
limit,
}, options), (err, result) => {
// result.docs
// result.total
// result.limit - 10
// result.offset - 20
if (err) { return reject(err); }
return resolve(result);
});
});
};
};