{"id":16367965,"url":"https://github.com/ephys/sequelize-cursor-pagination","last_synced_at":"2025-10-26T07:30:45.970Z","repository":{"id":57111783,"uuid":"374945187","full_name":"ephys/sequelize-cursor-pagination","owner":"ephys","description":"Implements cursor pagination in the Sequelize ORM","archived":false,"fork":false,"pushed_at":"2023-04-11T13:55:57.000Z","size":400,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"v6","last_synced_at":"2025-01-31T16:51:45.447Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ephys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-08T08:53:34.000Z","updated_at":"2024-05-21T02:09:09.000Z","dependencies_parsed_at":"2022-08-21T00:01:06.824Z","dependency_job_id":null,"html_url":"https://github.com/ephys/sequelize-cursor-pagination","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ephys%2Fsequelize-cursor-pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ephys%2Fsequelize-cursor-pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ephys%2Fsequelize-cursor-pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ephys%2Fsequelize-cursor-pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ephys","download_url":"https://codeload.github.com/ephys/sequelize-cursor-pagination/tar.gz/refs/heads/v6","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238284742,"owners_count":19446720,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T02:51:17.945Z","updated_at":"2025-10-26T07:30:40.661Z","avatar_url":"https://github.com/ephys.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sequelize-cursor-pagination\n\n[GraphQL-ready cursor pagination](https://graphql.org/learn/pagination/) for Sequelize.\n\nThis library provides a simple function, `sequelizeFindByCursor`, that you can use to paginate SQL queries using `after`, `before`, `first`, and `last` instead of `limit` \u0026 `offset`.\n\nIncludes efficient built-in support for `hasNextPage` \u0026 `hasPreviousPage`.\n\nThis library has been designed with the [GraphQL Cursor Connections Specification](https://relay.dev/graphql/connections.htm) in mind, but can likely be used for any cursor pagination (including REST).\n\n## Install\n\n[`npm i @ephys/sequelize-cursor-pagination`](https://www.npmjs.com/package/@ephys/sequelize-cursor-pagination)\n\nTypeScript typings are built-in.\n\n## Usage\n\nThe simplest usage is to select the first `x` elements from the table in a given sort order. \n\n```typescript\nconst results: FindByCursorResult = await sequelizeFindByCursor({\n  model: UserModel,\n  // you can also use 'last'\n  first: 10,\n  order: [['firstName', 'ASC'], ['lastName', 'ASC']],\n});\n```\n\nThis will return an object matching the following shape: \n\n```typescript\ntype FindByCursorResult = {\n  nodes: UserModel[],\n  \n  // these functions will sometimes return a Promise based on\n  // whether or not the value can be determined without making a new Query.\n  // In the above example, hasNextPage() will not return a promise because it already knows\n  // whether or not there is more data to be selected. It does this by selecting one more item than needed.\n  hasNextPage: () =\u003e MaybePromise\u003cboolean\u003e,\n  hasPreviousPage: () =\u003e MaybePromise\u003cboolean\u003e,\n} \n```\n\n### Cursor\n\nIn order to select the next page of your pagination, you need to pass a cursor to `sequelizeFindByCursor`.\n\nThese cursors are stateless and must be an object which includes the primary key + every value used in the sort order.\n\nIn the following example, the sort order uses `firstName` and `lastName` and the table has `id` as the sole primary key. Therefore the \ncursor will be an object with the shape `{ firstName: string, lastName: string, id: number }`.\n\nIt is up to you to build the cursor and to determine how the cursor will be stored for the next query.  \nYou could:\n- Serialize it and send it with the query response (beware of data leaks).\n- Store it somewhere and send the cursor ID (making it a stateful cursor).\n- If your database data is immutable, you could simply send a unique field of the entity as the cursor, \n  and rebuild the cursor from the entity before calling `sequelizeFindByCursor`. \\\n  If your data is not immutable this may cause problems with your pagination. \n  (If the last user of a page changes their name from Bertrand to Zoe, your user will end up at the end of your list)\n\n```typescript\nconst results: FindByCursorResult = await sequelizeFindByCursor({\n  model: UserModel,\n  first: 10,\n  // you can also use 'before' (you would typically use 'before' with 'last')\n  after: {\n    id: 6,\n    firstName: 'Bernard',\n    lastName: '',\n  },\n  order: [['firstName', 'ASC'], ['lastName', 'ASC']],\n});\n```\n\n### Options\n\n`sequelizeFindByCursor` supports a series of standard sequelize options such as:\n\n- `transaction`\n- `logging`\n- `where`\n- `attributes`\n\nCheck the typescript typings for more.\n\n### Customising the query\n\nIf the available options are not enough, you can use the escape hatch to build the query yourself. \nIt should be used as a last resort.\n\n```typescript\nconst results: FindByCursorResult = await sequelizeFindByCursor({\n  model: UserModel,\n  first: 10,\n  order: [['firstName', 'ASC'], ['lastName', 'ASC']],\n  findAll: query =\u003e {\n    // customise `query` before passing it to findAll.\n    // or use sequelize.query() to run a hand-written sql query.\n    return UserModel.findAll(query);\n  },\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fephys%2Fsequelize-cursor-pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fephys%2Fsequelize-cursor-pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fephys%2Fsequelize-cursor-pagination/lists"}