Socket
Socket
Sign inDemoInstall

knex-paginator

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    knex-paginator

Simple paginator for Knex. It adds the .paginate() function to knex's query builder.


Version published
Weekly downloads
316
increased by17.04%
Maintainers
1
Install size
3.00 MB
Created
Weekly downloads
 

Readme

Source

Knex-Paginator

Warning: do not use in production. This is not being actively maintained.

Simple paginator for Knex. It adds the .paginate() function to knex's query builder.

How to set up

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

npm i knex-paginator --save

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

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

const setupPaginator = require('knex-paginator');
setupPaginator(knex);

Function definition

paginate(perPage = 10, page = 1, isLengthAware = false)
ArgumentDescription
perPage (integer, defaults to 10)Items to show per page.
page (integer, defaults to 1)Current page.
isLengthAware (boolean. Defaults to false)Whether the paginator is aware of its length or not.

Note: If isLengthAware is set to true, the performance will be worst, as it will have to perform an extra query to get the length.

How to use

Example with callback

knex('products')
    .where('price', '<', 20)
    .paginate(15, 1, true)
    .then(paginator => {
        console.log(paginator.current_page);
        console.log(paginator.data);
    });

Example with async/await

const paginator = await knex('products')
    .where('price', '<', 20)
    .paginate(15, 1, true);

console.log(paginator.current_page);
console.log(paginator.data);

The paginator object

The function returns an object that contains the following data:

Always returned:

KeyValue
per_pageItems per page.
current_pageCurrent page number.
fromID of the first item of the current page.
toID of the last item of the current page.
dataThe actual data of the current page.

Returned if isLengthAware == true:

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

Keywords

FAQs

Last updated on 06 Jul 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc