Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sequelize-pagination

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sequelize-pagination

Add a method on a sequelize model for pagination queries

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
208
increased by3.48%
Maintainers
1
Weekly downloads
 
Created
Source

Sequelize Pagination

Add a method on a sequelize model for pagination queries

The project is inspired by sequelize-simple-pagination

Quick start

Define a sequelize model and add a pagination method:

// Define a model
const withPagination = require('sequelize-pagination');

const Counter = sequelize.define('counter', {
  id: { type:  Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
  value: Sequelize.INTEGER,
});

const options = {
  methodName: 'paginate', // the name of the pagination method
  primaryKey: 'id',       // the primary key field of the model
};

// Add a pagination method for the model
withPagination(options)(Counter);

Call the paginate (default method name) method:

Counter.paginate({
  pageIndex: 0,
  pageSize: 10,
})
.then(pagination => {
    console.log(pagination.entities)
})

The paginate method returns a promise with resolve data of SequelizePaginationResult type.

Use examples

Predefine pagination

withPagination({
    pageSize: 3,                            // Fix page size
})(Counter);

example/PredefinePagination.js

Custom pagination

Create a pagination with orderBy, order options

async function paginateCounter(options) {
    const {orderBy, order, ...others} = options;
    const result = await Counter.paginate({
        orders: [[orderBy, order]],
        ...others,
    });
    return {orderBy, order, ...result};
}

example/CustomPagination-OrderBy.js

API

withPagination(options) - for adding pagination method

withPagination() is a function generator for remember pagination configuration.

options is a object with following properties:

  • methodName: the name of the pagination method. Default: paginate
  • primaryKey: the primary key field of the model. Default: id
  • oneBaseIndex: page index base. Pag index starts from 0 if oneBaseIndex is false. Page index starts from 1 if oneBaseIndex is true. Default: false
  • pageSize: Default: 1
  • where: the query applied to findAll and pass value directly to where
  • array: the query applied to findAll and add a primary key to order
  • attributes: the query applied to findAll and pass value directly to attributes
  • include: the query applied to findAll and pass value directly to include

paginate(options) - execute a pagination query (suppose options.methodName is paginate)

options is a object with following properties:

  • primaryDesc: primary key desc order. Default: false
  • pageSize: Default: 1
  • pageIndex: Pag index starts from 0 if oneBaseIndex is false. Page index starts from 1 if oneBaseIndex istrue.
  • where: the query applied to findAll and pass value directly to where
  • array: the query applied to findAll and add a primary key to order
  • attributes: the query applied to findAll and pass value directly to attributes
  • include: the query applied to findAll and pass value directly to include

return a promise with resolve data of SequelizePaginationResult type.

SequelizePaginationResult - pagination resolve data

SequelizePaginationResult is a object type with following properties:

  • entities the results of the query
  • pageIndex page index
  • pageCount page count(total page amount)
  • pageSize page size for one page
  • count all entities for a model
  • where the where parameter of findAll
  • orders the orders parameter of findAll
  • attributes the attributes parameter of findAll
  • include the include parameter of findAll

Run tests

npm run test

Keywords

FAQs

Package last updated on 08 May 2018

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc