Pagination-api
You only need to pass the data and the total query
Install
npm i pagination-apis
import
; // orconst buildPaginator = ;
build paginator
All parameters are optional.
PaginationParams
- If
page
is empty or invalid by default page is 1. - If
limit
is empty or invalid by default page is 50. Thelimit
is always less thanmaximumLimit
. maximumLimit
by default is 50. always try to pass a safe value and avoid making a big query.url
If pass url it shows the next and previous properties.
; const limit page = ; console // 50console // 1 // example 2const limit page = ; console // 30console // 2 // example url without paramsconst paginate = ; console;/*{ totalPages: 2, total: 100, data: [], next: '/api/example?limit=50&page=2', previous: '/api/example?limit=50&page=1',}*/ // example url with paramsconst paginate = ; console;/*{ totalPages: 2, total: 100, data: [], next: '/api/example?id=30&limit=50&page=2', previous: '/api/example?id=30&limit=50&page=1',}*/ const paginate = ; console;/*{ totalPages: 2, total: 100, data: [], next: '/api/example?id=30&q=hola&limit=50&page=2', previous: '/api/example?id=30&q=hola&limit=50&page=1',}*/
Example with typeorm
; const skip limit paginate = ; // query builderconst data total = await ; // or findconst data total = await userRepository; return ;
Example with sequelize
; ; return paginaterows, count;
Example output
{
totalPages: 4,
total: 40,
data: [
{ id: 1, name: 'example' },
{ id: 1, name: 'example' },
{ id: 1, name: 'example' },
{ id: 1, name: 'example' },
{ id: 1, name: 'example' },
{ id: 1, name: 'example' },
{ id: 1, name: 'example' },
{ id: 1, name: 'example' },
{ id: 1, name: 'example' },
{ id: 1, name: 'example' },
],
next: '/api/example?limit=10&page=3',
previous: '/api/example?limit=10&page=1',
}
Example with sequelize and typescript in base service
;