Create a minimal response payload
An API endpoint should expose only approved user properties rather than the complete model.
Prisma · Const
Prisma Client's select option defines the scalar fields and relation fields included in a query result. It narrows both the returned payload and its generated TypeScript shape.
Use select inside a Prisma Client query to define the fields returned for a model. Set scalar fields to true, and use nested select objects to control which fields are returned from relations; the resulting TypeScript type reflects the selected shape.
select: {
fieldName: true,
relationName: {
select: {
relatedField: true,
},
},
}| Name | Type | Description |
|---|---|---|
select | XOR<ModelSelect, null> | An object whose boolean field entries and nested relation selections specify which properties Prisma Client returns. |
A Prisma Client result containing only the selected scalar fields and selected relation fields, with the corresponding generated result type.
select as a field allowlist when the query needs only part of a model's scalar data.true inside the selection object.import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
const user = await prisma.user.findFirst({
select: {
email: true,
name: true,
posts: {
select: {
title: true,
published: true,
},
},
},
});
console.log(user);
await prisma.$disconnect();Returns only the selected user fields and the selected fields from each related post.
An API endpoint should expose only approved user properties rather than the complete model.
A list view needs a few columns from each record and no full relation objects.
A detail view needs a relation but only selected fields from that relation.
select option do?select specifies which properties Prisma Client includes in the returned object. A scalar field is enabled with true, and relations can contain nested selection objects.
Follow-up: How would you select a user's email and only each post's title? Use a top-level select for email and a nested posts.select containing title: true.
select option is supplied?By default, Prisma Client returns all scalar fields and no relations. Adding select narrows the result to the fields explicitly requested, which makes the payload and inferred result shape more intentional.
Follow-up: What happens if a relation is not selected? It is excluded from the returned result unless it is selected through a nested selection.
select and include be combined?select and include cannot be used together at the same query level. To return selected root fields and selected relation fields, use one top-level select and put another select inside the relation.
Follow-up: Can select appear inside include? Yes, a nested select can focus the fields of an included relation, but the root query must not combine select and include at the same level.
includeomitnested selectPrisma Client queries