{"id":24344789,"url":"https://github.com/tvainika/objection-keyset-pagination","last_synced_at":"2025-04-09T17:41:00.437Z","repository":{"id":57312905,"uuid":"126373344","full_name":"tvainika/objection-keyset-pagination","owner":"tvainika","description":"Keyset pagination (cursor based pagination) plugin for Objection.js","archived":false,"fork":false,"pushed_at":"2021-01-05T07:43:15.000Z","size":30,"stargazers_count":20,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T10:49:49.286Z","etag":null,"topics":["cursor","objection","objectionjs","offset","pagination","paging"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tvainika.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-22T17:40:12.000Z","updated_at":"2025-01-10T19:59:07.000Z","dependencies_parsed_at":"2022-09-20T23:21:29.058Z","dependency_job_id":null,"html_url":"https://github.com/tvainika/objection-keyset-pagination","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvainika%2Fobjection-keyset-pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvainika%2Fobjection-keyset-pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvainika%2Fobjection-keyset-pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvainika%2Fobjection-keyset-pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tvainika","download_url":"https://codeload.github.com/tvainika/objection-keyset-pagination/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248079156,"owners_count":21044250,"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":["cursor","objection","objectionjs","offset","pagination","paging"],"created_at":"2025-01-18T09:56:06.260Z","updated_at":"2025-04-09T17:41:00.402Z","avatar_url":"https://github.com/tvainika.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/tvainika/objection-keyset-pagination.svg?branch=master)](https://travis-ci.org/tvainika/objection-keyset-pagination)\n\n# objection-keyset-pagination\n\nObjection-keyset-pagination is a plugin to [Objection.js](https://vincit.github.io/objection.js) ORM to implement keyset based pagination, also known as cursor pagination.\n\nKeyset pagination requires having strict ordering of records. On the user interface side, keyset pagination goes well with infinite scroll elements. Keyset pagination provides stable results. Next batch of records always continues from the last record of previous batch even if there would be insertion or deletions between queries. Using dummy numbered pages such as Objection.js' own `.page()` or `OFFSET` in SQL, then insertion or deletion causes some records omitted or duplicated.\n\n\u003ca href=\"https://use-the-index-luke.com/no-offset\"\u003e\n   \u003cimg src=\"https://use-the-index-luke.com/static/no-offset-banner-468x60.NsC5gHrT.png\" width=\"468\" height=\"60\"\n        alt=\"Do not use OFFSET for pagination\"\n   /\u003e\n\u003c/a\u003e\n\n## Installation\nInstall:\n\n```bash\nnpm install objection-keyset-pagination\n```\n\nRegister the plugin:\n\n```js\nconst Model = require('objection').Model;\nconst keysetPagination = require('objection-keyset-pagination')();\n\nclass Person extends keysetPagination(Model) {\n  static get tableName() {\n    return 'Person';\n  }\n}\n```\n\n## Options\n\n**limit:** The number of rows per request if not set. The limit can be also set per query with `.limit()`. Default value is 10.\n\n**countTotal:** Query total number of rows.  Default value is false.  Second query will be performed to get the total count.\n\nThe options can be provided by optional second argument when registering the plugin:\n\n```js\nconst keysetPagination = require('objection-keyset-pagination')({\n  limit: 20});\n```\n\n## Usage\n\nQuery with `.keysetPage()` to fetch first batch of rows and the keyset. Then later fetch next batch of rows starting from the keyset. The keyset is a plain old JS object, which can be easily passed around in JSON serialization.\n\n```js\nconst result1 = await Person\n  .query()\n  .where('age', '\u003e', 20)\n  .limit(5)\n  .orderBy('id')\n  .keysetPage();\n```\n\nThe models are returned in `result.results` just like with `.page()`.\n\nThe keyset index columns are returned in `result.keyset`. Pass this back in the next query to fetch next batch of rows.\n\nIf the query doesn't have `orderBy` clause, Model's `idColumn` will be used in ascending order.\n\nIf ***countTotal*** was set to true, then the return value also has `result.total` which contains **total** count of rows.\n\nThe next batch of rows are fetched by passing `result.keyset` as parameter to `.keysetPage()`.\n\n```js\nconst result2 = await Person\n  .query()\n  .where('age', '\u003e', 20)\n  .limit(5)\n  .orderBy('id')\n  .keysetPage(result1.keyset);\n```\n\nFetching backwards can be achieved using `.previousKeysetPage(result.keyset)`. The returned models here are in reverse order.\n\n```js\nconst result3 = await Person\n  .query()\n  .where('age', '\u003e', 20)\n  .limit(5)\n  .orderBy('id')\n  .previousKeysetPage(result2.keyset);\n\nexpect(result3.results).to.eql(result1.reverse());\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftvainika%2Fobjection-keyset-pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftvainika%2Fobjection-keyset-pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftvainika%2Fobjection-keyset-pagination/lists"}