1.3.2 • Published 3 years ago
koa-pagination-v2 v1.3.2
koa-pagination-v2
Pagination middleware for Koa.
Installation
Install the package via yarn:
> yarn add koa-pagination-v2or via npm:
> npm install koa-pagination-v2 --saveConfiguration
The middleware can be configured with the following parameters:
defaultLimit: Default number of items allowed per page (10by default).maximumLimit: Maximum number of items allowed per page (100by default).
You can change the defaults by doing:
pagination({ defaultLimit: 20, maximumLimit: 50 });Usage
const app = new (require('koa'))();
const pagination = require('koa-pagination-v2');
const { User } = require('../models');
app.use(pagination({ defaultLimit: 20, maximumLimit: 50 }));
app.get('/', async ctx => {
const { limit, offset, pageable } = ctx.state.paginate;
const { rows: users, count: total } = await User.findAndCountAll({
offset,
limit
});
return ctx.ok({ users, _meta: pageable(total) });
});
app.listen(3000);Request example
> http://localhost:3000/v1/users?page=1&limit=10Response example
{
"statusName": "OK",
"statusCode": 200,
"data": [
{
"id": "b79398ef-acf2-4e67-af1b-1482b79886b1",
"firstName": "Artash",
"lastName": "Mardoyan",
"updatedAt": "2022-07-11T08:24:38.337Z",
"createdAt": "2022-07-11T08:17:05.356Z"
},
{
"id": "b79398ef-acf2-4e67-af1b-1482b79886b2",
"firstName": "Gagik",
"lastName": "Alikhanyan",
"updatedAt": "2022-07-11T08:24:38.337Z",
"createdAt": "2022-07-11T08:17:05.356Z"
},
{
"id": "b79398ef-acf2-4e67-af1b-1482b79886b3",
"firstName": "Rafik",
"lastName": "Abgaryan",
"updatedAt": "2022-07-11T08:24:38.337Z",
"createdAt": "2022-07-11T08:17:05.356Z"
},
{
"id": "b79398ef-acf2-4e67-af1b-1482b79886b3",
"firstName": "Tsolak",
"lastName": "Harutyunyan",
"updatedAt": "2022-07-11T08:24:38.337Z",
"createdAt": "2022-07-11T08:17:05.356Z"
}
],
"_meta": {
"offset": 0,
"limit": 10,
"total": 4,
"pageCount": 4,
"currentPage": 1,
"hasPrev": false,
"hasNext": false
}
}License
1.2.0
3 years ago
1.3.2
3 years ago
1.3.1
3 years ago
1.3.0
3 years ago
1.2.1
3 years ago
1.1.0
4 years ago
1.0.9
5 years ago
1.0.8
5 years ago
1.0.7
5 years ago
1.0.6
5 years ago
1.0.5
5 years ago
1.0.4
5 years ago
1.0.2
5 years ago
1.0.1
5 years ago
0.0.5
5 years ago
1.0.3
5 years ago
0.0.3
5 years ago
0.0.4
5 years ago
0.0.2
5 years ago
0.0.1
5 years ago
1.0.0
5 years ago