{"id":16370292,"url":"https://github.com/deptyped/prisma-extension-pagination","last_synced_at":"2025-05-15T06:03:08.078Z","repository":{"id":70296407,"uuid":"590533273","full_name":"deptyped/prisma-extension-pagination","owner":"deptyped","description":"Prisma Client extension for pagination","archived":false,"fork":false,"pushed_at":"2024-12-02T14:29:00.000Z","size":327,"stargazers_count":268,"open_issues_count":14,"forks_count":21,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-15T06:02:45.391Z","etag":null,"topics":["nodejs","orm","pagination","prisma","prisma-client","prisma-extension","typescript"],"latest_commit_sha":null,"homepage":"https://npmjs.com/prisma-extension-pagination","language":"TypeScript","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/deptyped.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-18T16:25:56.000Z","updated_at":"2025-05-12T10:51:46.000Z","dependencies_parsed_at":"2023-12-13T14:30:21.285Z","dependency_job_id":"d2030fce-2cc1-444c-a8d0-05f3a5f53062","html_url":"https://github.com/deptyped/prisma-extension-pagination","commit_stats":{"total_commits":47,"total_committers":5,"mean_commits":9.4,"dds":0.1063829787234043,"last_synced_commit":"6c5f396bc2c31d509a2818a544ead859aea4cc4d"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":"deptyped/prisma-extension-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deptyped%2Fprisma-extension-pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deptyped%2Fprisma-extension-pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deptyped%2Fprisma-extension-pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deptyped%2Fprisma-extension-pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deptyped","download_url":"https://codeload.github.com/deptyped/prisma-extension-pagination/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254283336,"owners_count":22045140,"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":["nodejs","orm","pagination","prisma","prisma-client","prisma-extension","typescript"],"created_at":"2024-10-11T03:04:37.124Z","updated_at":"2025-05-15T06:03:08.048Z","avatar_url":"https://github.com/deptyped.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prisma Pagination Extension\n\n## Introduction\n\nPrisma Client extension for pagination.\n\n\u003e **Note**\n\u003e [Breaking change in `0.5.0`](https://github.com/deptyped/prisma-extension-pagination/releases/tag/v0.5.0)\n\n## Features\n\n- [Page number pagination](#page-number-pagination)\n- [Cursor-based pagination](#cursor-based-pagination)\n- Fully tested\n\n## Installation\n\n```bash\nnpm i prisma-extension-pagination\n```\n\n### Install extension to all models\n\n```ts\nimport { PrismaClient } from \"@prisma/client\";\nimport { pagination } from \"prisma-extension-pagination\";\n\nconst prisma = new PrismaClient().$extends(pagination());\n```\n\n### Install extension on some models\n\n```ts\nimport { PrismaClient } from \"@prisma/client\";\nimport { paginate } from \"prisma-extension-pagination\";\n\nconst prisma = new PrismaClient().$extends({\n  model: {\n    user: {\n      paginate,\n    },\n  },\n});\n```\n\n### Change default settings\n\n```ts\nimport { PrismaClient } from \"@prisma/client\";\nimport { pagination } from \"prisma-extension-pagination\";\n\nconst prisma = new PrismaClient().$extends(\n  pagination({\n    pages: {\n      limit: 10, // set default limit to 10\n      includePageCount: true, // include counters by default\n    },\n    cursor: {\n      limit: 10, // set default limit to 10\n\n      // set default cursor serialization/deserialization\n      getCursor(user: UserType) {\n        // ...\n      },\n      parseCursor(cursor) {\n        // ...\n      },\n    },\n  })\n);\n```\n\nWhen using the extension on some models, you need to use `createPaginator` function to set the default values:\n\n```ts\nimport { PrismaClient } from \"@prisma/client\";\nimport { createPaginator } from \"prisma-extension-pagination\";\n\nconst paginate = createPaginator({\n  // available settings are the same as above\n  pages: {\n    // ...\n  },\n  cursor: {\n    //...\n  },\n});\n\n// You can create many paginators with different settings.\n// They can be reused for different models.\n\nconst prisma = new PrismaClient().$extends({\n  model: {\n    user: {\n      paginate,\n    },\n    post: {\n      paginate,\n    },\n  },\n});\n```\n\n## Usage\n\n### Page number pagination\n\nPage number pagination uses `limit` to select a limited range and `page` to load a specific page of results.\n\n- [Load first page](#load-first-page)\n- [Load specific page](#load-specific-page)\n- [Calculate page count](#calculate-page-count)\n\n#### Load first page\n\n```ts\nconst [users, meta] = await prisma.user\n  .paginate({\n    select: {\n      id: true,\n    }\n  })\n  .withPages({\n    limit: 10,\n  });\n\n// meta contains the following\n{\n  currentPage: 1,\n  isFirstPage: true,\n  isLastPage: false,\n  previousPage: null,\n  nextPage: 2,\n}\n```\n\n#### Load specific page\n\n```ts\nconst [users, meta] = await prisma.user\n  .paginate()\n  .withPages({\n    limit: 10,\n    page: 2,\n  });\n\n// meta contains the following\n{\n  currentPage: 2,\n  isFirstPage: false,\n  isLastPage: false,\n  previousPage: 1,\n  nextPage: 3,\n}\n```\n\n#### Calculate page count\n\n```ts\nconst [users, meta] = await prisma.user\n  .paginate()\n  .withPages({\n    limit: 10,\n    page: 2,\n    includePageCount: true,\n  });\n\n// meta contains the following\n{\n  currentPage: 2,\n  isFirstPage: false,\n  isLastPage: false,\n  previousPage: 1,\n  nextPage: 3,\n  pageCount: 10, // the number of pages is calculated\n  totalCount: 100, // the total number of results is calculated\n}\n```\n\n### Cursor-based pagination\n\nCursor-based pagination uses `limit` to select a limited range\nand `before` or `after` to return a set of results before or after a given cursor.\n\n- [Load first records](#load-first-records)\n- [Load next page](#load-next-page)\n- [Load previous page](#load-previous-page)\n- [Custom cursor serialization](#custom-cursor-serialization)\n- [Load all results](#load-all-results)\n\n#### Load first records\n\n```ts\nconst [users, meta] = await prisma.user\n  .paginate({\n    select: {\n      id: true,\n    }\n  })\n  .withCursor({\n    limit: 10,\n  });\n\n// meta contains the following\n{\n  hasPreviousPage: false,\n  hasNextPage: true,\n  startCursor: \"1\",\n  endCursor: \"10\"\n}\n```\n\n#### Load next page\n\n```ts\nconst [users, meta] = await prisma.user\n  .paginate()\n  .withCursor({\n    limit: 10,\n    after: \"10\"\n  });\n\n// meta contains the following\n{\n  hasPreviousPage: true,\n  hasNextPage: true,\n  startCursor: \"11\",\n  endCursor: \"20\"\n}\n```\n\n#### Load previous page\n\n```ts\nconst [users, meta] = await prisma.user\n  .paginate()\n  .withCursor({\n    limit: 10,\n    before: \"11\"\n  });\n\n// meta contains the following\n{\n  hasPreviousPage: false,\n  hasNextPage: true,\n  startCursor: \"1\",\n  endCursor: \"10\"\n}\n```\n\n#### Custom cursor serialization\n\n```ts\nconst getCustomCursor = (postId: number, userId: number) =\u003e\n  [postId, userId].join(\":\");\n\nconst parseCustomCursor = (cursor: string) =\u003e\n  cursor.split(\":\");\n\nconst [results, meta] = await prisma.postOnUser\n  .paginate({\n    select: {\n      postId: true,\n      userId: true,\n    },\n  })\n  .withCursor({\n    limit: 10,\n    after: getCustomCursor(1, 1), // \"1:1\"\n\n    // custom cursor serialization\n    getCursor({ postId, userId }) {\n      return getCustomCursor(postId, userId)\n    },\n\n    // custom cursor deserialization\n    parseCursor(cursor) {\n      const [postId, userId] = parseCustomCursor(cursor);\n\n      return {\n        userId_postId: {\n          postId: parseInt(postId),\n          userId: parseInt(userId),\n        },\n      };\n    },\n  });\n\n// meta contains the following\n{\n  hasPreviousPage: false,\n  hasNextPage: true,\n  startCursor: \"1:2\",\n  endCursor: \"1:11\"\n}\n```\n\n### Load all results\n\nSometimes it's useful to return all results, if you need to do that, you can pass `limit: null`.\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeptyped%2Fprisma-extension-pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeptyped%2Fprisma-extension-pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeptyped%2Fprisma-extension-pagination/lists"}