Cursor pagination utility for sequelize.
npm install @thomas-smyth/sequelize-cursor-paginationsequelize-cursor-pagination is a Sequelize modal decorator that implements two kinds of pagination:
[[field1, direction1], [field2, direction2], ...].
before or after cursor in order for it to decide whether you are requesting the previous or next page. Although this is similar to the Relay GraphQL Cursor Connections Specification, the package is not a full implementation of the specification and therefore the two cursor input options add unnecessary complexity to the package as the caller has to specify both the cursor and the direction even though the cursor will be unique to the direction. This version simplifies this by embedding the direction in each cursor, so the caller only needs to input the appropriate cursor for the previous/next page to be returned. In addition to this, it provides an implementation that fully meets the Relay GraphQL Cursor Connections Specification for use in GraphQL APIs.
yarn add @thomas-smyth/sequelize-cursor-pagination
`
Usage
#### Define a Sequelize Model
##### Simple Pagination
`js
const { withSimplePagination } = require('@thomas-smyth/sequelize-cursor-pagination');
const Counter = sequelize.define('counter', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
value: Sequelize.INTEGER,
});
const options = {
methodName: 'paginate',
primaryKeyField: 'id',
};
withSimplePagination(options)(Counter);
`
##### Relay Pagination
`js
const { withRelayPagination } = require('@thomas-smyth/sequelize-cursor-pagination');
const Counter = sequelize.define('counter', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
value: Sequelize.INTEGER,
});
const options = {
methodName: 'paginate',
primaryKeyField: 'id',
};
withRelayPagination(options)(Counter);
`
The withSimplePagination/withRelayPagination function has the following options:
* methodName - The name of the pagination method. The default value is paginate.
* primaryKeyField - The primary key field of the model which all queries will be ordered by last in order to ensure cursors are unique. The default value is id.
#### Call the Initial Page
##### Simple Pagination
`js
const page = await Counter.paginate({
where: { value: { $gt: 2 } },
limit: 10
});
`
The paginate method returns an object with the following properties:
* results - An array of Sequelize model instances.
* cursors - Object containing information related to cursors.
- cursors.hasPrev - Has previous value(s).
- cursors.hasNext - Has next value(s).
- cursors.prevCursor - The cursor for the previous page.
- cursors.nextCursor - The cursor for the next page.
##### Relay Pagination
`js
const page = await Counter.paginate({
where: { value: { $gt: 2 } },
first: 10
});
`
The paginate method returns an object with the following properties:
* edges - An array of edges.
- edges[].cursor - The cursor of the edge.
- edges[].node - The node of the edge.
* pageInfo - Object containing information related to cursors.
- pageInfo.hasPreviousPage - Has previous value(s).
- pageInfo.hasNextPage - Has next value(s).
- pageInfo.startCursor - The cursor for the first edge page.
- pageInfo.endCursor - The cursor for the last edge page.
For more information, please see the Relay GraphQL Cursor Connections Specification.
#### Call the Next Page
##### Simple Pagination
To call the next/previous page pass the appropriate prevCursor/nextCursor values to the cursor option. For example, to go to the next page:
`js
const pageOne = await Counter.paginate({
where: { value: { $gt: 2 } },
limit: 10
});
const pageTwo = await Counter.paginate({
where: { value: { $gt: 2 } },
limit: 10,
cursor: pageOne.cursors.nextCursor
});
`
##### Relay Specification
To call the next/previous page pass the appropriate endCursor/startCursor value to the appropriate after/before option, as well as the appropriate first/last option as a replacement to the limit option. For example, to go to the next page:
`js
const pageOne = await Counter.paginate({
where: { value: { $gt: 2 } },
limit: 10
});
const pageTwo = await Counter.paginate({
where: { value: { $gt: 2 } },
after: pageOne.pageInfo.endCursor,
first: 10
});
`
For more information, please see the Relay GraphQL Cursor Connections Specification.
The paginate method accepts a paginationField cursor that overrides the previously specified primary key. It should be , like a primary key, this field should be unique to ensure cursors are unique.
The paginate method _should_ also accept all the same arguments as Sequelizer's findAll finder, however, this has not been as extensively tested. Open to issues/PRs to address any issues found regarding this.
Run Tests
`
yarn run test
``