npm install prisma-paginate
For more details and type definitions see:
http://sandrewtx08.github.io/prisma-paginate/
// ESM
import { PrismaClient } from "@prisma/client";
import paginator from "prisma-paginate";
// Commonjs
const { PrismaClient } = require("@prisma/client");
const paginator = require("prisma-paginate");
const prisma = new PrismaClient();
const paginate = paginator(prisma);
paginate.myTable.paginate({ limit: 20, page: 1, where: {} }, (err, result) => {
console.log(err, result);
});
paginate.myOtherTable
.paginate({ where: {} }, { limit: 10, page: 1 })
.then((result) => {
console.log(result);
});
// on database = [ { id: 1 }, { id: 2 }, {...}, { id: 100 } ]
paginate.myTable
.paginate(
{
where: {
// query stuff...
},
},
{ page: 1, limit: 50 }
)
.then((result) => {
console.log(result); // [ {...}, { id: 48 }, { id: 49 }, { id: 50 } ]
});
const prisma = new PrismaClient();
const paginate = paginator(prisma);
paginate.myTable.findMany({ where: { field: true } });
paginate.myTable.findOne({ where: { id: 1 } });
paginate.myOtherTable.count();
// Or
const myTable = paginator(prisma.myTable);
const myOtherTable = paginator(prisma.myOtherTable);
myTable.findMany({ where: { field: true } });
myTable.findOne({ where: { id: 1 } });
myOtherTable.count();
paginator("myTable").paginate({
where: { from_id: 2 },
pageIndex: 0,
limit: 30,
});
const paginate = paginator();
paginate.myTable.paginate({ where: {} }, { limit: 10, page: 1 });
paginate.myOtherTable.paginate({ where: {}, limit: 10, page: 2 });
findManyPaginationArgs
{Object} - Query with findMany Prisma and pagination argumentspaginationOrCallback?
{Object|(err, result)} - Pagination arguments or callbackpage
{Number}pageIndex
{Number}limit
{Number}exceedCount
{Boolean}
callback?
{(err, result)}
result
{Array} - Pagination resulttotalPages
{Number} - Total of pages based on pagination argumentshasNextPage
{Boolean} - If has result on next page indexhasPrevPage
{Boolean} - If has result on last page indexcount
{Number} - Count how many rows on has on table/model with query filternextPage
{(err, result)} - Request next page