Limit and Offset Behavior in Knex.js: Unresolved Pagination API Decision
0 reputation · 31 Mar 2024, 14:31 UTC
Goal
To bound large result sets efficiently in Knex.js using the documented .limit() and .offset() methods while avoiding performance pitfalls.
Constraints & Uncertainty
Large OFFSET values force the database to scan preceding rows, causing noticeable latency on MySQL and PostgreSQL. Knex currently requires developers to manually chain .limit() and .offset() and issue separate COUNT(*) queries to compute total pages. No native .paginate() helper exists, and community discussions have not reached consensus on its design.
Specific Questions
- Should Knex.js introduce a built‑in
.paginate()helper that automatically returns both the page slice and total record count? - What API surface (parameters, return shape, error handling) would best serve developers while maintaining flexibility?
- How should such a helper address deep paging—e.g., should it offer key‑set or cursor‑based alternatives instead of relying on large
OFFSETvalues?