3.1.2 • Published 19 days ago

knex-paginate v3.1.2

Weekly downloads
4,208
License
ISC
Repository
github
Last release
19 days ago

Knex-paginate

npm CI

Extension of Knex's query builder with paginate method that will help with your pagination tasks.

How to set up

To use this lib, first you will have to install it:

npm i knex-paginate --save
// or
yarn add knex-paginate

Then, add the following lines to your Knex set up:

const knex = require('knex')(config);

const { attachPaginate } = require('knex-paginate');
attachPaginate();

Function definition

.paginate(params: IPaginateParams): Knex.QueryBuilder<any, IWithPagination<TResult>>;

interface IPaginateParams {
  perPage: number,
  currentPage: number,
  isFromStart?: boolean,
  isLengthAware?: boolean,
}

interface IWithPagination<T = any> {
  data: T;
  pagination: IPagination;
}

interface IPagination {
  total?: number;
  lastPage?: number;
  currentPage: number;
  perPage: number;
  from: number;
  to: number;
}

How to use

Example

const result = await knex('persons')
   .paginate({ perPage: 10, currentPage: 2 });
// result.data - will hold persons data
// result.pagination - will hold pagination object

pagination object

KeyValue
perPageItems per page.
currentPageCurrent page number.
fromCounting ID of the first item of the current page.
toCounting ID of the last item of the current page.

Returned if isLengthAware == true or currentPage == 1 or isFromStart == true:

KeyValue
totalTotal items that the full query contains.
lastPageLast page number.

This lib got inspiration from knex-paginator.

3.1.2

19 days ago

3.1.1

1 year ago

3.1.0

1 year ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

3 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.2.4

3 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

5 years ago

1.0.0

5 years ago