🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

prisma-cursor-pagination

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prisma-cursor-pagination

Relay cursor pagination helpers for prisma.

0.2.2
latest
Source
npm
Version published
Weekly downloads
116
169.77%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

prisma

FAQs

Package last updated on 28 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts