Sikau

Prisma · Day 4 · about 30 min

Filtering, sorting and pagination

where operators, AND/OR/NOT, orderBy, skip and take.

Search pages, the Prisma way

A menu search with filters and pages needs flexible conditions. Prisma's where mirrors SQL's power with plain objects — and the same shape works across every query method.

where, orderBy, skip, take

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

console.log(await prisma.dish.findMany({
  where: {
    OR: [{ name: { contains: "MOMO", mode: "insensitive" } }, { tags: { has: "popular" } }],
    price: { gte: 100, lte: 250 },
    NOT: { category: "drink" },
  },
  orderBy: [{ category: "asc" }, { price: "desc" }],
  select: { name: true, category: true, price: true },
}));

const page = 2, perPage = 3;
console.log(await prisma.dish.findMany({ orderBy: { id: "asc" }, skip: (page - 1) * perPage, take: perPage, select: { id: true, name: true } }));

console.log(await prisma.dish.findMany({ where: { category: { in: ["snack", "drink"] } }, select: { name: true } }));

The rest of this lesson

  • Building filters from user inputexplain
  • Cursor paginationexplain
  • Which page?predict
  • Undefined in wherepredict
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 15 lessons of Prisma