CodingNic

CRUD and Querying with Prisma

Sorting and Pagination

CRUD and Querying with Prisma 10 min read

Sorting and Pagination

Objectives

By the end of this lesson, you should be able to:

  • Sort query results with orderBy
  • Paginate results with take and skip
  • Combine filtering, sorting, and pagination in one query

💡 Why this matters: Node.js & Express Foundations covered pagination, filtering, and sorting for a REST API backed by an in-memory array. This lesson covers the exact same ideas, now backed by a real database.

⚠️ A note on verification: as throughout this module, the Prisma CLI can’t run inside this course’s own sandboxed tooling. The code below reflects Prisma Client’s stable, current, documented query API. Try it yourself, on your own machine, against a real database.

Sorting With orderBy

javascript
const students = await prisma.student.findMany({
  orderBy: { grade: 'desc' }
});
console.log(students);
text
[
  { id: 2, name: 'Jordan', grade: 10 },
  { id: 1, name: 'Erin', grade: 9 }
]

orderBy: { grade: 'desc' } is the direct equivalent of SQL’s ORDER BY grade DESC, 'asc' sorts ascending (the default if omitted).

Pagination With take and skip

javascript
const page1 = await prisma.student.findMany({
  orderBy: { id: 'asc' },
  take: 2,
  skip: 0
});

const page2 = await prisma.student.findMany({
  orderBy: { id: 'asc' },
  take: 2,
  skip: 2
});

take limits how many rows come back, skip offsets where the results start, the same pattern REST API pagination (Node.js & Express Foundations, Module 9) uses with a page number and page size:

javascript
function getPage(pageNumber, pageSize) {
  return prisma.student.findMany({
    orderBy: { id: 'asc' },
    take: pageSize,
    skip: (pageNumber - 1) * pageSize
  });
}

Combining Filtering, Sorting, and Pagination

All three combine in a single call, exactly the shape a real API endpoint needs:

javascript
async function getStudents({ grade, page = 1, pageSize = 10 } = {}) {
  return prisma.student.findMany({
    where: grade ? { grade } : undefined,
    orderBy: { name: 'asc' },
    take: pageSize,
    skip: (page - 1) * pageSize
  });
}

const result = await getStudents({ grade: 10, page: 1, pageSize: 10 });
console.log(result);
text
[ { id: 2, name: 'Jordan', grade: 10 } ]

where: grade ? { grade } : undefined only applies the filter when grade was actually provided, undefined in a Prisma query object is simply ignored, a clean way to make a filter optional.

Getting a Total Count

Pagination usually needs a total count too, for calculating how many pages exist:

javascript
const total = await prisma.student.count({ where: { grade: 10 } });
console.log(total);
text
1

count takes the same where shape as findMany, and returns just a number.

Try It

  1. Fetch every student sorted by grade descending.
  2. Fetch two pages of results (take: 2 each) using skip, and confirm they don’t overlap.
  3. Write a function combining where, orderBy, take, and skip in one call, matching the getStudents example above.
  4. Use prisma.student.count() to get a total, and calculate how many pages of 10 that total would need.

Recap

  • orderBy: { field: 'asc' | 'desc' } sorts results, take and skip paginate them, both compose cleanly with where.
  • undefined in a query object is ignored, a clean way to make a filter conditional.
  • count() returns a total matching a where clause, without fetching the actual rows, useful for calculating page counts.

Next lesson: comparing this module’s Prisma queries directly against the raw SQL from Modules 1 and 2.