sequelize-pagination
is a Sequelize plugin to make the sequelize model paginatable.
This plugin use 'number' and 'size' parameters to paginate the sql query.
number
is a integer and begins from 1 (not 0).size
is a integer and should be larger than 0.
Just pass Sequelize model class to make the model class paginatable.
'use strict';
const Pagination = require('../sequelize-paginate/index.js');
// configure if want to change global default.
// Pagination.configure({
// size: 10
// });
// Extend model class.
const ModelClass = require('./path/to/model_class.js');
// This add these functionality.
// - pagination scope
// - nextPage as Class method.
Pagination.paginatable(ModelClass);
// Model level config supported.
Pagination.paginatable(ModelClass, {size: 40});
Use paginate
scope to fetch the resources.
const params = {
size: 20,
number: 2
};
// Use `paginate` scope to handle "number"/"size" parameter.
ModelClass.scope({method: ['paginate', params]}).findAll({order: 'id'})
=> {Promise<Array<ModelClass.Instance>>}
// Use `nextPage` class method to create next page parameter.
ModelClass.nextPage(params);
=> {size: 20, number: 3}
// When Pass total count to `nextPage`, return next page info if exists.
// NOTE: `nextPage` return `undefined` if next page does not exist.
ModelClass.nextPage(params, totalCount);
=> {size: 20, number: 3}
// Use `hasNextPage` method to check whether next page exists, or not.
ModelClass.hasNextPage(params, totalCount);
=> true
// Use `pagination` class method to get pagination config.
ModelClass.pagination
=> {size: 10}
$ cd path/to/this/repo/root
$ yarn install
export TEST_DB=postgres://postgres:pass@127.0.0.1:5432/testdb
$ docker-compose up
Use tasks in package.json.
$ yarn run ci