Skip to content

createPaginator

function createPaginator<T, TCursor>(options): PaginatorSync<T>;

Defined in: pagination.ts:236

Create a source-agnostic paginator. You provide fetchers for the capabilities your source supports; the paginator normalizes inputs, over-fetches one row to compute hasNextPage without a count query, handles backward (scroll-up) pagination, and emits a uniform Paginated<T> envelope.

Sync/async adaptive: the paginator is as synchronous as what you pass. All-sync fetchers + codec → paginate returns a plain Paginated<T>; any async piece → it returns a Promise. await works on both.

Type Parameter Default type
T -
TCursor extends Record<string, unknown> Record<string, unknown>
Parameter Type
options CreatePaginatorOptionsSync<T, TCursor>

PaginatorSync<T>

Drizzle + drizzle-cursor

const cursorGen = generateCursor({ primaryCursor: { key: "id", schema: users.id } });
const paginator = createPaginator({
fetchOffset: async ({ limit, offset }) => ({
items: await db.select().from(users).orderBy(asc(users.id)).limit(limit).offset(offset),
}),
fetchCursor: async ({ limit, cursor, direction }) => ({
items: await db.select().from(users)
.where(cursorGen.where(cursor))
.orderBy(...cursorGen.orderBy)
.limit(limit),
}),
cursor: { fromItem: (u) => ({ id: u.id }) },
maxLimit: 100,
});
await paginator.paginate({ type: "page", page: 2, pageSize: 20 });
await paginator.paginate({ type: "cursor", limit: 20, cursor: token, direction: "backward" });

In-memory array — fully synchronous

const paginator = createPaginator({
fetchOffset: ({ limit, offset }) => ({ items: rows.slice(offset, offset + limit) }),
});
const { items, pageInfo } = paginator.paginate({ type: "page", page: 1, pageSize: 10 }); // no await

Iterating everything (pages and items walk lazily)

for await (const page of paginator.pages({ type: "cursor", limit: 100 })) {
await exportBatch(page.items); // one fetch per loop turn
}
for await (const user of paginator.items({ type: "offset", limit: 50, offset: 0 })) {
// flattened across pages; composes with the iterable utils:
}
const all = await collect(paginator.items({ type: "cursor", limit: 100 }));
function createPaginator<T, TCursor>(options): Paginator<T>;

Defined in: pagination.ts:240

Create a source-agnostic paginator. You provide fetchers for the capabilities your source supports; the paginator normalizes inputs, over-fetches one row to compute hasNextPage without a count query, handles backward (scroll-up) pagination, and emits a uniform Paginated<T> envelope.

Sync/async adaptive: the paginator is as synchronous as what you pass. All-sync fetchers + codec → paginate returns a plain Paginated<T>; any async piece → it returns a Promise. await works on both.

Type Parameter Default type
T -
TCursor extends Record<string, unknown> Record<string, unknown>
Parameter Type
options CreatePaginatorOptions<T, TCursor>

Paginator<T>

Drizzle + drizzle-cursor

const cursorGen = generateCursor({ primaryCursor: { key: "id", schema: users.id } });
const paginator = createPaginator({
fetchOffset: async ({ limit, offset }) => ({
items: await db.select().from(users).orderBy(asc(users.id)).limit(limit).offset(offset),
}),
fetchCursor: async ({ limit, cursor, direction }) => ({
items: await db.select().from(users)
.where(cursorGen.where(cursor))
.orderBy(...cursorGen.orderBy)
.limit(limit),
}),
cursor: { fromItem: (u) => ({ id: u.id }) },
maxLimit: 100,
});
await paginator.paginate({ type: "page", page: 2, pageSize: 20 });
await paginator.paginate({ type: "cursor", limit: 20, cursor: token, direction: "backward" });

In-memory array — fully synchronous

const paginator = createPaginator({
fetchOffset: ({ limit, offset }) => ({ items: rows.slice(offset, offset + limit) }),
});
const { items, pageInfo } = paginator.paginate({ type: "page", page: 1, pageSize: 10 }); // no await

Iterating everything (pages and items walk lazily)

for await (const page of paginator.pages({ type: "cursor", limit: 100 })) {
await exportBatch(page.items); // one fetch per loop turn
}
for await (const user of paginator.items({ type: "offset", limit: 50, offset: 0 })) {
// flattened across pages; composes with the iterable utils:
}
const all = await collect(paginator.items({ type: "cursor", limit: 100 }));
function createPaginator<T, TCursor>(options): PaginatorMaybeAsync<T>;

Defined in: pagination.ts:244

Create a source-agnostic paginator. You provide fetchers for the capabilities your source supports; the paginator normalizes inputs, over-fetches one row to compute hasNextPage without a count query, handles backward (scroll-up) pagination, and emits a uniform Paginated<T> envelope.

Sync/async adaptive: the paginator is as synchronous as what you pass. All-sync fetchers + codec → paginate returns a plain Paginated<T>; any async piece → it returns a Promise. await works on both.

Type Parameter Default type
T -
TCursor extends Record<string, unknown> Record<string, unknown>
Parameter Type
options CreatePaginatorOptionsMaybeAsync<T, TCursor>

PaginatorMaybeAsync<T>

Drizzle + drizzle-cursor

const cursorGen = generateCursor({ primaryCursor: { key: "id", schema: users.id } });
const paginator = createPaginator({
fetchOffset: async ({ limit, offset }) => ({
items: await db.select().from(users).orderBy(asc(users.id)).limit(limit).offset(offset),
}),
fetchCursor: async ({ limit, cursor, direction }) => ({
items: await db.select().from(users)
.where(cursorGen.where(cursor))
.orderBy(...cursorGen.orderBy)
.limit(limit),
}),
cursor: { fromItem: (u) => ({ id: u.id }) },
maxLimit: 100,
});
await paginator.paginate({ type: "page", page: 2, pageSize: 20 });
await paginator.paginate({ type: "cursor", limit: 20, cursor: token, direction: "backward" });

In-memory array — fully synchronous

const paginator = createPaginator({
fetchOffset: ({ limit, offset }) => ({ items: rows.slice(offset, offset + limit) }),
});
const { items, pageInfo } = paginator.paginate({ type: "page", page: 1, pageSize: 10 }); // no await

Iterating everything (pages and items walk lazily)

for await (const page of paginator.pages({ type: "cursor", limit: 100 })) {
await exportBatch(page.items); // one fetch per loop turn
}
for await (const user of paginator.items({ type: "offset", limit: 50, offset: 0 })) {
// flattened across pages; composes with the iterable utils:
}
const all = await collect(paginator.items({ type: "cursor", limit: 100 }));