Catalog npm
Back to packageRELEASE DETAILS

1.1.0

npm

@0xc14m1z/express-pagination

Configurable ExpressJS pagination middleware.

expressmiddlewarenodepagination
Published1 Feb 2019
Direct dependencies3
Transitive dependenciesNot calculated

README

Current package documentation

express-pagination

TravisCI Coveralls CodeClimate Codacy

Configurable ExpressJS pagination middleware.


The purpose of this package is to simplify handling of pagination for ExpressJS applications. It can be used as fast as possible as shown in the Basic Usage section, or it can be highly configured to perfectly reflect your application needs.

This middleware allows your application to read two parameters for pagination (the current page number and the number of desired results per page) and setup a configurable property in the request object that gets passed to your route handler.

installation

npm install --save @0xc14m1z/express-pagination

default behaviour

This middleware:

  • looks for a page parameter as the current page number;
  • looks for a perPage parameter as the desired number of results per page;
  • sets a pagination property on the request object;
    • sets a page property to the given current page number or fallback to page 1;
    • sets a perPage property to the given desired number of results per page or fallback to 50;
    • sets a from property to the number of results to skip;

All of these properties can be configured as shown in the Advanced Usage section.

basic usage

This package exports two convenient methods to be used as ExpressJS middlewares on route or application basis.

If you don't have special needs, you can just import a middleware and use it like:

const pagination = require('@0xc14m1z/express-pagination')

app.get('/first-route', pagination.add, function (req, res) {
  // your handler here
})

Now your route can be called as: /first-route?page=3 or /first-route?page=3&perPage=30.

If no perPage parameter is given, the value 50 is used for it.

With this setup you'll have pagination information in req.pagination as:

{
  page: 3,
  perPage: 30, // or 50, if this parameter isn't given
  from: 60
}

If you want to change the default number of desired results per page, you can use a slightly different middleware:

const pagination = require('@0xc14m1z/express-pagination')

app.get('/second-route', pagination.addWith(25), function (req, res) {
  // your handler here
})

advanced usage

The default export of this package is a function that takes a configuration in input and returns a middleware generator.

This middleware generator can than be used in your application:

// customPagination.js
const pagination = require('@0xc14m1z/express-pagination')

module.exports = pagination(/* custom configuration here */)
// controller.js
const addPagination = require('./customPagination')

// here the number results per page is taken from the configuration
app.get('/first-route', addPagination(), function (req, res) {
  // your handler here
})

// here the given number results per page overrides the configuration value
app.get('/second-route', addPagination(100), function (req, res) {
  // your handler here
})

The default configuration is:

{
  input: {
    page: 'page',
    perPage: 'perPage'
  },
  output: {
    property: 'pagination',
    page: 'page',
    perPage: 'perPage',
    from: 'from',
    defaultPerPage: 50
  }
}

Let's say that we have an application that receives paging information in a shorter format, for instance: /paged-route?p=4&pp=30

Where p is the current page number and pp is the desired number of results per page, we can set the configuration as:

{
  input: {
    page: 'p',
    perPage: 'pp'
  }
}

Let's say that, for instance, we are using Sequelize ORM to perform our database queries. It allows to narrow query results using the standard SQL keywords: limit and offset.

We may set the configuration to:

{
  input: {
    page: 'p',
    perPage: 'pp'
  },
  output: {
    property: 'queryLimits'
    perPage: 'limit',
    from: 'offset'
  }
}

And use it like:

// controller.js
const addPagination = require('./customPagination')

app.get('/users', addPagination(), function (req, res) {
  const { limit, offset } = req.queryLimits

  User.findAll({ limit, offset })
  // or even User.findAll({ ...req.queryLimits })
})

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 3

Native requirements
3 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
chai
Environment
{
  "local_name": "chai",
  "alias": false,
  "peer_metadata": null
}
^4.2.0development
nwb
Environment
{
  "local_name": "nwb",
  "alias": false,
  "peer_metadata": null
}
0.23.xdevelopment
sinon
Environment
{
  "local_name": "sinon",
  "alias": false,
  "peer_metadata": null
}
^7.2.3development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-o1Tp6kzotdI5H1avh8Al+GI8s9iMUagxiwhapbUuZP+1T019/B8ZLMcYqqxK0VyNoLQnFLGuHunegrVla/1V6Q==",
  "shasum": "fef46add53bcde82f8069e43026a5590162066ee",
  "tarball": "https://registry.npmjs.org/@0xc14m1z/express-pagination/-/express-pagination-1.1.0.tgz",
  "fileCount": 9,
  "unpackedSize": 6953,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcVNQrCRA9TVsSAnZWagAANhoP/2uNhpxcrp6tJufbXDhp\nSmzVsNA/mCn+ST/DbytdvqEHzq0NDr2hruRqrmtXNwZ09S7aXyWpM4dY/zrZ\nvWWiq8qHVyjzRbUhki+xQxEBKpV17Fccf/3QqA/vyEiWIGfygtq53EVTknEP\n3Nb9xEgG5dxeR52jbpB4Cx8HvPJZnaH8cFdOGFX/vJUkNVKDfBO5Sg42jeM7\nC9KiDSTJd/2RIJ74x2TZJH3BDFfjyjqi9veikWGlxkYJyihG31gcCz5QSDVj\nG97IW6jVG4Rd5FlbaEZDhsdyMYHEiLWwLsA600CYb9Q+zIhFDVEf2CBusJuO\nJVF8Bw76k7tiGE0ykJLerRXbtwNHG0Y70QDIZzAWIidfRfHC0tsy3dceKAO1\nIHqf1Jj8qmMHJZtgQp0Lk2GqJUqeUvMn64+cNKYgI4MRDUlN1XrCotzFU90t\niBK9FrkUmsArTGOWqBzG7Esy6aKjGEAkWQkZB0ZZ7xrjML7qjs2eTeSHwEmY\nBBNhJ6pD4b7x/1z231f+ODbZi4tOhqOuU9F6+k1TibyQ9QJklJT67TF3HGfg\n1mwxLmrNGx2+9qKt6KXkHIMwjIJQDwgQqNblE8mxogJ5NNoUVg2aekOL8RJX\n01uVqbkTN/YE4Ikcs6Uj1lLnOARnWvPHBuM0KHf8yj1SgS7gSy8Kwwkle0GV\nlVsb\r\n=+xBx\r\n-----END PGP SIGNATURE-----\r\n",
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEQCIDzyWMmrolPQD0zqfvVskVEjcGE0x53N6M1wfQVXseWMAiBpzu0k30ZE3x9VXGcbtEAkvYEGUTrPlyWaJEVsOoy67g=="
    }
  ]
}

Repository

PENDING
0xc14m1z/express-pagination

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 22:44 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:3024de428c156f59fcdf86b0161de84e3ca1273bf50ccb8a4f5c5b7065eb8e35

Other collected versions

1.1.73 Feb1.1.63 Feb1.1.53 Feb1.1.21 Feb1.1.11 Feb1.0.01 FebView all collected versions →