koa-pagination-v2 is a middleware designed for Koa applications to simplify handling pagination logic based on `page` and `limit` query parameters from the request. Currently at version 1.3.2, this package was last published approximately four years ago, indicating it is in a maintenance state rather than active development, with no recent releases or active contribution. Unlike its predecessor or other Koa pagination libraries that might focus on HTTP Range headers (e.g., the original `koa-pagination`), this version specifically processes `page` and `limit` from the URL query string. It exposes computed `limit` and `offset` values, along with a `pageable` helper function, via `ctx.state.paginate`. These values are directly usable with common ORMs (like Sequelize, as shown in the examples) to fetch a subset of data, and the `pageable` function helps construct comprehensive pagination metadata for API responses, including total count, page count, and navigation links. Its key differentiator is its straightforward, query-parameter-based approach, making it easy to integrate for simple REST API pagination.
npm install koa-pagination-v2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to apply `koa-pagination-v2` middleware to a Koa application, fetch paginated data using mock data, and return a response with pagination metadata. It shows accessing `limit`, `offset`, and the `pageable` helper from `ctx.state.paginate`.
Ensure you understand whether your API expects query parameters (`page`, `limit`) or HTTP `Range` headers, and choose the appropriate middleware. If migrating, update client-side pagination logic accordingly.
Evaluate alternatives if active maintenance and modern feature support are critical for your project. If using, ensure thorough testing with your specific Koa and Node.js versions.
Always ensure that your data fetching logic accurately determines the total count of items that match the query *before* pagination is applied, and pass this `total` count to `ctx.state.paginate.pageable()`.
Carefully choose `defaultLimit` and `maximumLimit` values to align with your API's design and expected usage. Clearly document these limits for API consumers so they understand the effective pagination behavior.
Ensure `app.use(pagination({ /* config */ }));` is called *before* any route handlers that depend on pagination data.Verify that your database query correctly returns the *total* number of records matching any filters, ignoring the `limit` and `offset` for the actual data retrieval, and pass this accurate total to the `pageable` function.
Check the `maximumLimit` option provided when initializing the `koa-pagination-v2` middleware. The middleware will cap the `limit` at this maximum, even if a higher value is requested by the client.
No dependency data recorded yet.