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'
const paginate = createPaginator({ perPage: 20 })
app.get('/', async ({ query, prisma }, res) => {
const result = await paginate<User, Prisma.UserFindManyArgs>(
prisma.user,
{
where: {
name: {
contains: 'Alice'
}
}
orderBy: {
id: 'desc',
}
},
{ page: query.page }
})
})
For Express-like frameworks, page
and perPage
can be initialized in the middleware:
import { createPaginator } from 'prisma-pagination'
const pagination = (req, res, next) => {
req.paginate = createPaginator({ page: req.query.page, perPage: 20 })
next()
}
app.use(pagination)
so there is no need to pass page
value in each handler:
app.get('/', async ({ paginate, prisma }, res) => {
return await paginate(prisma.user)
})
To have proper Typescript support, Request interface can be extended this way:
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