JSPM

prisma-cursor-pagination

0.2.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q23636F
  • License MIT

Relay cursor pagination helpers for prisma.

Package Exports

  • prisma-cursor-pagination
  • prisma-cursor-pagination/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (prisma-cursor-pagination) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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;
  };
};