Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zoontek/prisma-cursor-pagination
Relay cursor pagination helpers for prisma
https://github.com/zoontek/prisma-cursor-pagination
connection cursor graphql pagination prisma relay
Last synced: about 1 month ago
JSON representation
Relay cursor pagination helpers for prisma
- Host: GitHub
- URL: https://github.com/zoontek/prisma-cursor-pagination
- Owner: zoontek
- License: mit
- Created: 2022-02-18T18:54:24.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-06T18:37:12.000Z (5 months ago)
- Last Synced: 2024-10-02T09:03:45.739Z (about 2 months ago)
- Topics: connection, cursor, graphql, pagination, prisma, relay
- Language: TypeScript
- Homepage:
- Size: 109 KB
- Stars: 15
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# prisma-cursor-pagination
Relay cursor pagination helpers for prisma.
[![mit licence](https://img.shields.io/dub/l/vibe-d.svg?style=for-the-badge)](https://github.com/zoontek/prisma-cursor-pagination/blob/main/LICENSE)
[![npm version](https://img.shields.io/npm/v/prisma-cursor-pagination?style=for-the-badge)](https://www.npmjs.org/package/prisma-cursor-pagination)
[![bundlephobia](https://img.shields.io/bundlephobia/minzip/prisma-cursor-pagination?label=size&style=for-the-badge)](https://bundlephobia.com/result?p=prisma-cursor-pagination)## Installation
```sh
$ npm i prisma-cursor-pagination --save
# --- or ---
$ yarn add prisma-cursor-pagination
```## 📘 Usage
```ts
import { parsePaginationArgs } from "prisma-cursor-pagination";const resolvers = {
Query: {
projects: async (_, args) => {
// parse pagination arguments (first: Int! & after: ID / last: Int! & before: ID)
const { findManyArgs, toConnection } = parsePaginationArgs(args);
const projects = await prisma.project.findMany(findManyArgs);// transform prisma result into a relay connection
return toConnection(projects);
},users: async (_, args) => {
const { findManyArgs, toConnection } = parsePaginationArgs(args);const [totalCount, users] = await Promise.all([
prisma.user.count(),
prisma.user.findMany(findManyArgs),
]);// add non-standard data in connection (here totalCount)
return { totalCount, ...toConnection(users) };
},
},
};
```## ⚙️ API
### parsePaginationArgs(args, options)
Take the query arguments and return prisma `findMany` arguments and a function to transform prisma result into a relay connection.
```ts
type parsePaginationArgs = (
args: Partial<{
first: number | null;
after: string | null;
last: number | null;
before: string | null;
}>,
options: {
connectionName?: string; // optional, used only to improve error message
},
) => {
findManyArgs: {
cursor?: {
id: string;
};
skip?: number;
take: number;
};
toConnection: (
items: T[],
) => {
edges: Edge[];
pageInfo: PageInfo;
};
};
```