{"_id":"@ekhein/prisma-extension-pagination","name":"@ekhein/prisma-extension-pagination","dist-tags":{"latest":"0.7.4"},"versions":{"0.7.4":{"name":"@ekhein/prisma-extension-pagination","version":"0.7.4","description":"Prisma Client extension for pagination","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"build":"tsc","dev":"tsc -w","test":"jest","lint":"eslint \"src/**/*.ts\" \"test/**/*.ts\"","format":"npm run lint -- --fix","prepare":"npm run build","setup":"prisma generate && prisma migrate deploy"},"repository":{"type":"git","url":"git+https://github.com/deptyped/prisma-extension-pagination.git"},"keywords":["node","prisma","prisma-client","prisma-extension"],"author":{"name":"deptyped","email":"deptyped@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/deptyped/prisma-extension-pagination/issues"},"homepage":"https://github.com/deptyped/prisma-extension-pagination#readme","peerDependencies":{"@prisma/client":"^4.9.0 || ^5.0.0"},"devDependencies":{"@types/jest":"^29.5.12","@types/node":"^20.12.7","@typescript-eslint/eslint-plugin":"^7.6.0","@typescript-eslint/parser":"^7.6.0","eslint":"^8.55.0","eslint-config-prettier":"^9.1.0","eslint-plugin-prettier":"^5.1.3","jest":"^29.7.0","prisma":"^5.7.0","ts-jest":"^29.1.2","typescript":"^5.4.5"},"dependencies":{"@prisma/client":"5.18.0"},"gitHead":"6c5f396bc2c31d509a2818a544ead859aea4cc4d","_id":"@ekhein/prisma-extension-pagination@0.7.4","_nodeVersion":"16.20.2","_npmVersion":"8.19.4","dist":{"integrity":"sha512-9+vWHBzn4p+URyhfLnR0l16jQxHNO0wi5KyfvcfbfmLZVa2gzPWhZ0CD0SMB7UNAIEEajKnIJ112/1NMWclhPg==","shasum":"1c86cef2892800824f0a2ada6330719659634c11","tarball":"https://registry.npmjs.org/@ekhein/prisma-extension-pagination/-/prisma-extension-pagination-0.7.4.tgz","fileCount":15,"unpackedSize":23920,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDwh1YTiOZP+gXpRFAQpcIxW/61UXEoKF3aVk7Ih56ysgIgfJYpGYyIYKO5rrf0TdLpFasa2vowDOg/g10Wl514Tj4="}]},"_npmUser":{"name":"ekhein","email":"1373657363@qq.com"},"directories":{},"maintainers":[{"name":"ekhein","email":"1373657363@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/prisma-extension-pagination_0.7.4_1729043342945_0.26215944784640755"},"_hasShrinkwrap":false}},"time":{"created":"2024-10-16T01:49:02.849Z","0.7.4":"2024-10-16T01:49:03.154Z","modified":"2024-10-16T01:49:03.434Z"},"maintainers":[{"name":"ekhein","email":"1373657363@qq.com"}],"description":"Prisma Client extension for pagination","homepage":"https://github.com/deptyped/prisma-extension-pagination#readme","keywords":["node","prisma","prisma-client","prisma-extension"],"repository":{"type":"git","url":"git+https://github.com/deptyped/prisma-extension-pagination.git"},"author":{"name":"deptyped","email":"deptyped@gmail.com"},"bugs":{"url":"https://github.com/deptyped/prisma-extension-pagination/issues"},"license":"MIT","readme":"# Prisma Pagination Extension\n\n## Introduction\n\nPrisma Client extension for pagination.\n\n> **Note**\n> [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) =>\n  [postId, userId].join(\":\");\n\nconst parseCustomCursor = (cursor: string) =>\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","readmeFilename":"README.md"}