mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 01:38:19 +08:00
17 lines
599 B
TypeScript
17 lines
599 B
TypeScript
import { Cursor } from 'mongodb';
|
|
import { ValidationError } from '../error';
|
|
|
|
export default async function paginate(
|
|
cursor: Cursor, page: number, pageSize: number,
|
|
): Promise<[any[]/* docs */, number/* numPages */, number/* count */]> {
|
|
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];
|
|
}
|
|
|
|
global.Hydro.lib.paginate = paginate;
|