JSPM

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

This package merges cursor, offset and relay pagination

Package Exports

  • prisma-custom-relay-pagination
  • prisma-custom-relay-pagination/lib/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-custom-relay-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 Relay Pagination

Inspired by Prisma Korea's prisma-offset-pagination, Prisma Relay Pagination enhances pagination using Prisma cursor option.

If you want to know how Prisma supports pagination, see Prisma documentation

Installation

NPM

npm install prisma-custom-relay-pagination

Yarn

yarn add prisma-custom-relay-pagination

How to use

Add to tsconfig.json a path named @libs/prisma-custom-relay-pagination where your prisma output is generated

"@libs/prisma-custom-relay-pagination/*": [
  //Directory where your generated prisma is
  //Example: "../src/generated/prisma/*"
]

Add PrismaRelay where you need

import { PrismaRelay } from 'prisma-custom-relay-pagination';

...
const result = new PrismaRelay(PrismaClient, {
    model: 'User',
    buttons: 5,
    where:{ id: 1 }
    omit: { id: true },
    orderBy: { id: 'asc' },
    select: { id: true },
    include: { createdBy: true },
    pagination: { items: 10, cursor: 'ABCD...' }
});

Parameters

prisma: Prisma client object

args:

  • model Model name (Typescript intellisense enabled from your available Prisma's models)

  • buttons (optional) Number of available pagination pages (Default value: 5 )

  • where (optional) Same values as Prisma.${Model}WhereInput

  • omit (optional) Same values as Prisma.{Model}Omit

  • orderBy (optional) Same values as Prisma.{Model}OrderByWithRelationInput

  • select (optional) Same values as Prisma.{Model}Select

  • include (optional) Same values as Prisma.{Model}Include

  • pagination (optional) If pagination is not added, all results will be returned

    • cursor (optional) If cursor is not added, first page will be returned
    • items Number of results returned

Notes

Intellisense in fields: where, omit, orderBy, select and include are available when model parameter is added.

Return data example

{
    pageEdges: [ 
        {  cursor: 'c2FsdHl256x0MQ==', node: { id: 1 } },
        {  cursor: 'c2FsdH4gw2x0MQ==', node: { id: 2 } },
        {  cursor: 'c2FsdHldwfx0MQ==', node: { id: 3 } } 
    ],
    pageCursors: {
        previous: null,
        next: { isCurrent: false, page: 2, cursor: 'c2FsdHldwfx0MQ==' },
        first: null,
        last: { isCurrent: false, page: 15, cursor: 'c2FsdHlz45x0MjExNTY=' },
        around: [
            { isCurrent: true, page: 1, cursor: 'c2FsdHl256x0MQ==' },
            { isCurrent: false, page: 2, cursor: 'c2FsdHldwfx0MQ==' },
            { isCurrent: false, page: 3, cursor: 'c2Fs75lzYWx0Mw==' }
        ]
    },
    totalCount: 45
}

NestJS

There are some utilities for NestJS that help you to reduce boilerplate when you are using Graphql:

ResolverSelect

Using Pal.js, this decorator converts Query fields from Graphql to Prisma select fields. You can check Pal.js documentation to see how it works.

How to use

import { ResolverSelect } from 'prisma-custom-relay-pagination';

@Resolver(() => User)
export class UserResolver {
    @Query(() => User)
    getUser(@ResolverSelect() select: Prisma.UserSelect) {
        ///Your code
    }
}

Parameters

isPagination (optional) Set parameter to true if you are using PrismaRelay (Default value: false)

omit (optional) Omit fields not declared in Prisma's schema or fields with their resolver defined in Graphql.

model (optional) GraphQL model name if it does not match with the Prisma schema model name


PrismaRelayPagination

This decorator converts Graphql model to PrismaRelay pagination model

How to use

import { PrismaRelayPagination } from 'prisma-custom-relay-pagination';

@PrismaRelayPagination({ type: User })
export class UserPagination {}

Graphql result example

getUserPagination(...parameters) {
    totalCount
    pageCursors {
        first {
            cursor
            page
        }
        previous {
            cursor
            page
        }
        around {
            cursor
            page
        }
        next {
            cursor
            page
        }
        last {
            cursor
            page
        }
    }
    pageEdges {
        cursor
        node {
            ///User fields
        }
    }
}

Parameters

type The Graphql model you want to convert to a pagination


PrismaRelayPaginationArg / PrismaRelayPaginationOptionalArg

These classes help you add pagination parameters when you are using pagination queries. You can extend them with other fields like where, orderBy, etc.

If you want to force pagination, then use PrismaRelayPaginationArg, otherwise, use PrismaRelayPaginationOptionalArg

How to use

import { PrismaRelayPaginationArg } from 'prisma-custom-relay-pagination';

@ArgsType()
export class UserPaginationArgs extends PrismaRelayPaginationArg {}