Helper library for Prisma offset pagination with Typescript support.
Installation
npm i prisma-pagination
Usage
import { createPaginator } from 'prisma-pagination'
import { User, Prisma } from '@prisma/client'
app.get('/', async ({ prisma }, res) => {
const paginate = createPaginator({ page: 2 })
const result = await paginate<User, Prisma.UserFindManyArgs>(
prisma.user,
{
where: {
name: {
contains: 'Alice'
}
}
orderBy: {
id: 'desc',
}
}
})
})
If you use Express-like framework, initializing createPaginator
could be moved to the middleware:
import { createPaginator } from 'prisma-pagination'
export const pagination = (req, res, next) => {
const page = Number(req.query.page) || 1
req.paginate = createPaginator({ page })
}
so then it can be used in any route handler:
app.get('/', async ({ paginate, prisma }, res) => {
return await paginate(prisma.user)
})
To have proper Typescript support you can extend Request interface like so:
import { PaginateFunction } from 'prisma-pagination'
declare global {
namespace Express {
export interface Request {
paginate: PaginateFunction
}
}
}
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
MIT