prisma-custom-relay-pagination v2.0.0
Prisma Relay Pagination
Inspired by Prisma Korea's prisma-offset-pagination, Prisma Relay Pagination enhance pagination using Prisma cursor option.
If you want to know how to Prisma support pagination, see Prisma documentation
Installation
NPM
npm install prisma-custom-relay-pagination
Yarn
yarn add prisma-custom-relay-pagination
How to use
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}WhereInputomit
(optional) \ Same values as Prisma.{Model}OmitorderBy
(optional) \ Same values as Prisma.{Model}OrderByWithRelationInputselect
(optional) \ Same values as Prisma.{Model}Selectinclude
(optional) \ Same values as Prisma.{Model}Includepagination
(optional) \ If pagination is not added, all results will be returnedcursor
(optional) \ If cursor is not added, first page will be returneditems
\ 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
\
Selected Graphql model to convert to pagination
PrismaRelayPaginationArg / PrismaRelayPaginationOptionalArg
These classes help you add pagination parameters when you are using pagination queries. You can extend them if it's necessary 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 {}
4 months ago
11 months ago
11 months ago
11 months ago
11 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago