Working with Read Operations

1. Finding Unique Record

const user = await prisma.user.findUnique({ where: { id: 1 } });
const byCompound = await prisma.userRole.findUnique({
  where: { userId_roleId: { userId: 1, roleId: 2 } }
});
AspectDetail
WhereMust be unique field or compound
ReturnsRecord or null

2. Finding First Match

OptionUse
whereArbitrary filter
orderByDefine "first"
ReturnsRecord or null

3. Finding Multiple Records

const posts = await prisma.post.findMany({
  where: { published: true },
  orderBy: { createdAt: "desc" },
  take: 20,
  skip: 0
});
ParamDetail
whereFilter
take / skipLimit/offset
cursorCursor-based pagination
distinctDistinct fields
OptionNotes
includeFetch entire related rows
NestedDrill into relations
relationLoadStrategy"join" | "query" (preview)

5. Selecting Specific Fields

await prisma.user.findMany({
  select: { id: true, email: true, posts: { select: { title: true } } }
});
TipDetail
BandwidthAvoid SELECT *
CombineUse nested select for relations

6. Using findUniqueOrThrow Method

AspectDetail
BehaviorThrows NotFoundError if no match
Error codeP2025

7. Using findFirstOrThrow Method

UseDetail
Non-unique filtersFirst match required
ThrowsP2025 on miss

8. Counting Records

const total = await prisma.post.count({ where: { published: true } });
VariantUse
count()Total rows
count({ select: { id: true } })Per-field non-null counts

9. Using Aggregations

const stats = await prisma.order.aggregate({
  _sum: { total: true },
  _avg: { total: true },
  _count: { _all: true }
});
OpResult
_sum/_avg/_min/_maxNumeric/DateTime fields
_countRow count

10. Checking Record Existence

const exists = (await prisma.user.count({ where: { email } })) > 0;
ApproachDetail
countFast for indexed fields
findUnique + null checkFetches whole row (more cost)