prisma-cursor-pagination
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

prisma-cursor-pagination

Relay cursor pagination helpers for prisma.

mit licence npm version bundlephobia

Installation

$ npm i prisma-cursor-pagination --save
# --- or ---
$ yarn add prisma-cursor-pagination

📘 Usage

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.

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: <T extends { id: string }>(
    items: T[],
  ) => {
    edges: Edge<T>[];
    pageInfo: PageInfo;
  };
};

Package Sidebar

Install

npm i prisma-cursor-pagination

Weekly Downloads

10

Version

0.2.2

License

MIT

Unpacked Size

11.1 kB

Total Files

11

Last publish

Collaborators

  • zoontek