Thin MongoDB ODM built for Standard Schema
mongodb
zod
deno
1import type { Collection, Document, Filter, WithId } from "mongodb";
2import type { Infer, Schema } from "../types.ts";
3
4/**
5 * Pagination operations for the Model class
6 *
7 * This module contains pagination-related functionality for finding documents
8 * with skip, limit, and sort options.
9 */
10
11/**
12 * Find documents with pagination support
13 *
14 * @param collection - MongoDB collection
15 * @param query - MongoDB query filter
16 * @param options - Pagination options (skip, limit, sort)
17 * @returns Array of matching documents
18 *
19 * @example
20 * ```ts
21 * const users = await findPaginated(collection,
22 * { age: { $gte: 18 } },
23 * { skip: 0, limit: 10, sort: { createdAt: -1 } }
24 * );
25 * ```
26 */
27export async function findPaginated<T extends Schema>(
28 collection: Collection<Infer<T>>,
29 query: Filter<Infer<T>>,
30 options: { skip?: number; limit?: number; sort?: Document } = {},
31): Promise<(WithId<Infer<T>>)[]> {
32 return await collection
33 .find(query)
34 .skip(options.skip ?? 0)
35 .limit(options.limit ?? 10)
36 .sort(options.sort ?? {})
37 .toArray();
38}