{"id":20767034,"url":"https://github.com/binded/bookshelf-cursor-pagination","last_synced_at":"2025-05-11T08:34:06.454Z","repository":{"id":97906145,"uuid":"93461492","full_name":"binded/bookshelf-cursor-pagination","owner":"binded","description":"Bookshelf plugin that implements cursor based pagination","archived":true,"fork":false,"pushed_at":"2017-10-11T07:55:15.000Z","size":63,"stargazers_count":20,"open_issues_count":1,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-27T12:02:16.952Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/binded.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-06T01:11:38.000Z","updated_at":"2025-03-11T07:58:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"e6c94859-bef1-4edf-bf8e-dd8b0cd5d554","html_url":"https://github.com/binded/bookshelf-cursor-pagination","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binded%2Fbookshelf-cursor-pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binded%2Fbookshelf-cursor-pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binded%2Fbookshelf-cursor-pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binded%2Fbookshelf-cursor-pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/binded","download_url":"https://codeload.github.com/binded/bookshelf-cursor-pagination/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253540462,"owners_count":21924522,"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-11-17T11:27:23.017Z","updated_at":"2025-05-11T08:34:06.441Z","avatar_url":"https://github.com/binded.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/binded/bookshelf-cursor-pagination.svg?branch=master)](https://travis-ci.org/binded/bookshelf-cursor-pagination)\n\n# bookshelf-cursor-pagination\n\nBookshelf plugin that implements [cursor based pagination](https://www.sitepoint.com/paginating-real-time-data-cursor-based-pagination/) (also known as [keyset pagination](http://use-the-index-luke.com/no-offset)).\n\n## Install\n\n```bash\nnpm install bookshelf-cursor-pagination\n```\n\n## Usage\n\n`fetchCursorPage` is the same as\n[fetchPage](http://bookshelfjs.org/#Model-instance-fetchPage) but with\ncursors instead. A cursor is a series of column values that uniquely\nidentify the position of a row in a result set. If only the primary ID\nis sorted a cursor is simply the primary ID of a row.\nArguments:\n- *limit*: size of page (defaults to 10)\n- *before*: array of values that correspond to sorted columns\n- *after*: array of values that correspond to sorted columns\n\nIf there is no sorting and the cursor (before or after) has one element,\nwe implicitly sort by the id attribute.\n\n`before` and `after` are mutually exclusive. `before` means we fetch the\npage of results before the row represented by the cursor. `after` means\nwe fetch the page of results before the row represented by the cursor.\n\n```javascript\nimport cursorPagination from 'bookshelf-cursor-pagination'\n\n// ...\n\nbookshelf.plugin(cursorPagination)\n\n// ...\nclass Car extends Bookshelf.Model {\n  get tableName() { return 'cars' }\n}\n\nconst result = await Car.collection()\n  .orderBy('manufacturer_id')\n  .orderBy('description')\n  .fetchCursorPage({\n    after: [/* manufacturer_id */ '8', /* description */ 'Cruze'],\n  })\n\nconsole.log(result.models)\n\n// ...\n\nconsole.log(result.pagination)\n\n/*\n{ limit: 10,\n  rowCount: 27,\n  hasMore: true,\n  cursors: { after: [ '17', 'Impreza' ], before: [ '8', 'Impala' ] },\n  orderedBy:\n   [ { name: 'manufacturer_id', direction: 'asc', tableName: 'cars' },\n     { name: 'description', direction: 'asc', tableName: 'cars' } ] }\n*/\n\n// A next() method is also available on the collection to fetch the next\n// set of result\n```\n\nExample of stable iteration with cursors:\n\n```javascript\n// will iterate by batches of 5 until the end\nconst iter = async (doSomething, after) =\u003e {\n  const coll = await Car.collection()\n    .orderBy('id')\n    .fetchCursorPage({ after, limit: 5 })\n  await doSomething(coll)\n  if (coll.pagination.hasMore) {\n    return iter(doSomething, coll.pagination.cursors.after)\n  }\n}\n\niter((collection) =\u003e {\n  console.log(collection.models.length)\n  // 5\n})\n```\n\nThis plugin also adds a `forEach` method that takes the same arguments\nas `fethPage` and a callback which is called for every result set.\n\nFor example:\n\n```javascript\nconst main = async () =\u003e {\n  await Car\n    .collection()\n    .orderBy('id')\n    .forEach({ limit: 5 }, async (coll) =\u003e {\n      // do something with collection\n    })\n  console.log('iterated over all rows!')\n}\n```\n\n### Joins and/or .format\n\n`fetchCursorPage` will break if one of the sorted columns is not\naccessible via `model.get(colName)` (either because the column is not\nreturned by the select or because the bookshelf object implements a\n`.format()` method).\n\nIn order to avoid this issue, you can implement a `toCursorValue` on\nyour model that will handle those edge cases. For example:\n\n```javascript\nCar.prototype.toCursorValue = function ({ name, tableName }) {\n  if (tableName === this.tableName) return this.get(name)\n  if (tableName === 'engines' \u0026\u0026 name === 'name') {\n    return this.get('engine_name')\n  }\n  throw new Error(`cannot extract cursor for ${tableName}.${name}`)\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinded%2Fbookshelf-cursor-pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinded%2Fbookshelf-cursor-pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinded%2Fbookshelf-cursor-pagination/lists"}