npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thomas-smyth/sequelize-cursor-pagination

v2.0.0

Published

Cursor pagination utility for sequelize.

Downloads

3,127

Readme

sequelize-cursor-pagination

GitHub release GitHub contributors GitHub release

GitHub issues GitHub pull requests GitHub issues

About

sequelize-cursor-pagination is a Sequelize modal decorator that implements two kinds of pagination:

This package was created to solve some minor annoyances in and simplify Kaltsoon's sequelize-cursor-pagination, however, has expanded to support the Relay GraphQL Cursor Connections Specification in order for it to be suitable for GraphQL APIs.

Why use this package over others?

There is a small number of packages out there that provide cursor based pagination queries for Sequelize. The most prominent of these is Kaltsoon's sequelize-cursor-pagination, which this package uses as a base with the intent to improve upon it.

  • Multiple Order Queries - Kaltsoon's version does not support multiple order queries very well. This version allows you to input orders of any length like normal Sequelize finders, i.e. [[field1, direction1], [field2, direction2], ...].
  • Common Value Support - Kaltsoon's version does not support common values very well. This version provides better support for common values by ensuring that ordering and comparisons of fields are robust. Furthermore, it ensures that all queries are ordered by primary key at some stage and that cursors contain the primary key leading to unambiguous starting points for pages.
  • Single Cursor Input/Relay GraphQL Cursor Connections Specification - Kaltsoon's version requires you to specify whether an inputted cursor is either a 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.

Install

yarn add @thomas-smyth/sequelize-cursor-pagination

Usage

Define a Sequelize Model

Simple Pagination
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
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
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
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:

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:

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