You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
12 lines
434 B
JavaScript
12 lines
434 B
JavaScript
const { ValidationError } = require('../error');
|
|
|
|
module.exports = async function paginate(cursor, page, pageSize) {
|
|
if (page <= 0) throw new ValidationError('page');
|
|
const [count, pageDocs] = await Promise.all([
|
|
cursor.count(),
|
|
cursor.skip((page - 1) * pageSize).limit(pageSize).toArray(),
|
|
]);
|
|
const numPages = Math.floor((count + pageSize - 1) / pageSize);
|
|
return [pageDocs, numPages, count];
|
|
};
|