Prisma Pagination & Sorting
Implementing offset-based pagination (skip/take) and high-performance cursor-based pagination (cursor/take) with multi-column sorting.
Prisma Pagination & Sorting
1. Sorting (orderBy)
Sort records by single or multiple columns:
const sortedProducts = await prisma.product.findMany({
orderBy: [
{ isFeatured: "desc" },
{ price: "asc" },
{ createdAt: "desc" },
],
});2. Offset-Based Pagination (skip / take)
Best suited for traditional page-number pagination (Page 1, Page 2, Page 3):
const PAGE_SIZE = 20;
const pageNumber = 3; // 1-indexed
const [totalCount, items] = await prisma.$transaction([
prisma.product.count({ where: { category: "electronics" } }),
prisma.product.findMany({
where: { category: "electronics" },
skip: (pageNumber - 1) * PAGE_SIZE,
take: PAGE_SIZE,
orderBy: { createdAt: "desc" },
}),
]);
const totalPages = Math.ceil(totalCount / PAGE_SIZE);[!NOTE] Offset pagination (
OFFSET 100000) degrades in performance on large tables because the database must scan and discard all skipped rows. For large datasets or infinite scrolling, use Cursor-based pagination.
3. Cursor-Based Pagination (cursor / take)
High-performance pagination designed for infinite scrolling feeds and large datasets:
// Fetch the next 20 items starting AFTER cursorId
const nextBatch = await prisma.post.findMany({
take: 20,
skip: 1, // Skip the cursor item itself
cursor: {
id: "last_seen_post_id",
},
orderBy: {
id: "asc", // Must sort by a unique column used as the cursor
},
});Related Topics
Prisma Filtering & Advanced Queries
Master where clauses in Prisma: string filters (contains, startsWith), numbers (gt, lt), lists (in), logical operators (AND, OR, NOT), and relation filters.
Prisma Database Transactions ($transaction)
Executing atomic operations in Prisma: sequential batch transactions and interactive callback transactions with ACID guarantees.