Sequelize Simple Pagination
Add a method on a sequelize model for pagination queries
The project is inspired by sequelize-cursor-pagination
Quick start
Define a sequelize model and add a pagination method:
// Define a modelconst withPagination = ; const Counter = sequelize; 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 modelCounter;
Call the paginate
(default method name) method:
Counter
The paginate
method returns a promise with resolve data of SequelizePaginationResult
type.
Use examples
Predefine pagination configuration
Counter;
example/PredefinePagination.js
Custom pagination
Create a pagination with orderBy, order options
{ const orderBy order ...others = options; const result = await Counter; return orderBy order ...result;}
example/CustomPagination-OrderBy.js
Pagination hook
Alter the resolve data after pagination completion by the hook afterPaginationFunction()
const withPaginationHook = ; Counter; Counter
API
Module: sequelize-simple-pagination
withPagination(options) - for adding pagination method
withPagination()
decorates sequelize models by a pagination function (default: paginate()).
options
is an 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
isfalse
. Page index starts from 1 ifoneBaseIndex
istrue
. Default:false
- pageSize: Default: 1
- where: the query applies to findAll and passes value directly to where
- orders: the query applies to findAll and adds a primary key to order
- attributes: the query applies to findAll and passes value directly to attributes
- include: the query applies to findAll and passes value directly to include
Module: sequelize-simple-pagination/util
withPaginationHook({paginateMethod, afterPaginationFunction}) - hook functions
- paginateMethod: pagination method. Default: paginate
- afterPaginationFunction: Alter the resolve data after pagination completion
Pagination query
options.methodName
is paginate
)
paginate(options) - execute a pagination query (suppose options
is an 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 applies to findAll and passes value directly to where
- orders: the query applies to findAll and adds a primary key to order
- attributes: the query applies to findAll and passes value directly to attributes
- include: the query applies to findAll and passes value directly to include
return a promise with resolve data of SequelizePaginationResult
type.
Type
SequelizePaginationResult - pagination resolve data
SequelizePaginationResult
is an 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 offindAll
- orders the
orders
parameter offindAll
- attributes the
attributes
parameter offindAll
- include the
include
parameter offindAll
Run tests
npm run test