Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
withPagination
✓ import { withPagination } from 'sql-cursor-pagination'
✗ const { withPagination } = require('sql-cursor-pagination')
ESM-only since v4; Node >=18 required. CommonJS require() will fail.
Order
✓ import { Order } from 'sql-cursor-pagination'
TypeScript type (enum-like union) for sort order values: 'ASC' | 'DESC'. No runtime export for CJS.
buildCursorSecret
✓ import { buildCursorSecret } from 'sql-cursor-pagination'
✗ import { buildCursorSecret } from 'sql-cursor-pagination/buildCursorSecret'
Direct named export; no subpath import. Function returns a Buffer from a string or Buffer secret.
Shows complete setup with Knex and TypeScript: table creation, paginated query with cursor support.
import knex from 'knex';
import { withPagination, Order, buildCursorSecret } from 'sql-cursor-pagination';
const db = knex({
client: 'sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
await db.schema.createTable('users', (table) => {
table.integer('id').notNullable();
table.integer('created_at').notNullable();
table.string('first_name').notNullable();
table.string('last_name').notNullable();
table.string('email').notNullable();
table.boolean('admin').notNullable();
});
async function fetchUsers(userInput: {
order: Order;
admins: boolean;
first?: number;
last?: number;
before?: string;
after?: string;
}) {
const query = db('users').where('admin', userInput.admins);
const { edges, pageInfo } = await withPagination({
query: {
first: userInput.first,
last: userInput.last,
before: userInput.before,
after: userInput.after,
},
setup: {
sortFields: [
{ field: 'first_name', order: userInput.order },
{ field: 'last_name', order: userInput.order },
{ field: 'id', order: userInput.order },
],
cursorSecret: buildCursorSecret('somethingSecret'),
queryName: 'users',
runQuery: async ({ limit, whereFragmentBuilder, orderByFragmentBuilder }) => {
const whereFragment = whereFragmentBuilder.withArrayBindings();
const orderByFragment = orderByFragmentBuilder.withArrayBindings();
const rows = await query
.limit(limit)
.whereRaw(whereFragment.sql, whereFragment.bindings)
.orderByRaw(orderByFragment.sql, orderByFragment.bindings);
return rows;
},
},
});
return { edges: edges, pageInfo: pageInfo };
}
Errors
Common errors & fixes
Error: 'withPagination' is not exported from 'sql-cursor-pagination' (or similar import error)
Using CommonJS require() with ESM-only package (v4+).
fixSwitch to import statement or use dynamic import: const { withPagination } = await import('sql-cursor-pagination'); could not determine unique field in sort fields
sortFields array does not include a unique key (like primary key) as the last field.
fixAdd a unique field, e.g., { field: 'id', order: 'ASC' } as the last element in sortFields. Cannot read properties of undefined (reading 'sql')
Misuse of whereFragmentBuilder or orderByFragmentBuilder without calling withArrayBindings() or withPositionalBindings().
fixCall whereFragmentBuilder.withArrayBindings() (or the positional variant) to get the fragment object.
Audit
Dependencies
No dependency data recorded yet.