npx skills add ...
npx skills add prisma/cursor-plugin --skill prisma-client-api-model-queries
npx skills add prisma/cursor-plugin --skill prisma-client-api-model-queries
Model Queries. Reference when using this Prisma feature.
CRUD operations for your Prisma models.
Find a single record by unique field:
Same as findUnique but throws if not found:
Find first matching record:
Find multiple records:
Create a single record:
Create multiple records:
Create multiple and return them:
Update a single record:
Update multiple records:
Update or create:
Delete a single record:
Delete multiple records:
| Method | Returns |
|---|---|
findUnique | Record | null |
findUniqueOrThrow | Record (throws if not found) |
findFirst | Record | null |
findFirstOrThrow | Record (throws if not found) |
findMany | Record[] |
create | Record |
createMany | { count: number } |
createManyAndReturn | Record[] |
update | Record |
updateMany | { count: number } |
delete | Record |
deleteMany | { count: number } |
count | number |
aggregate | Aggregate result |
groupBy | Group result[] |
// Model with @@unique([firstName, lastName])
const user = await prisma.user.findUnique({
where: {
firstName_lastName: {
firstName: 'Alice',
lastName: 'Smith'
}
}
})const user = await prisma.user.findUniqueOrThrow({
where: { id: 1 }
})
// Throws PrismaClientKnownRequestError if not foundconst user = await prisma.user.findFirst({
where: { role: 'ADMIN' },
orderBy: { createdAt: 'desc' }
})const user = await prisma.user.findFirstOrThrow({
where: { role: 'ADMIN' }
})const users = await prisma.user.findMany({
where: { role: 'USER' },
orderBy: { name: 'asc' },
take: 10,
skip: 0
})const user = await prisma.user.create({
data: {
email: 'alice@prisma.io',
name: 'Alice'
}
})const user = await prisma.user.create({
data: {
email: 'alice@prisma.io',
posts: {
create: [
{ title: 'First Post' },
{ title: 'Second Post' }
]
}
},
include: { posts: true }
})const result = await prisma.user.createMany({
data: [
{ email: 'alice@prisma.io', name: 'Alice' },
{ email: 'bob@prisma.io', name: 'Bob' }
],
skipDuplicates: true // Skip records with duplicate unique fields
})
// Returns { count: 2 }const users = await prisma.user.createManyAndReturn({
data: [
{ email: 'alice@prisma.io', name: 'Alice' },
{ email: 'bob@prisma.io', name: 'Bob' }
]
})
// Returns array of created usersconst user = await prisma.user.update({
where: { id: 1 },
data: { name: 'Alice Smith' }
})const post = await prisma.post.update({
where: { id: 1 },
data: {
views: { increment: 1 },
likes: { decrement: 1 },
score: { multiply: 2 },
rating: { divide: 2 },
version: { set: 5 }
}
})const result = await prisma.user.updateMany({
where: { role: 'USER' },
data: { verified: true }
})
// Returns { count: 42 }const users = await prisma.user.updateManyAndReturn({
where: { role: 'USER' },
data: { verified: true }
})
// Returns array of updated usersconst user = await prisma.user.upsert({
where: { email: 'alice@prisma.io' },
update: { name: 'Alice Smith' },
create: { email: 'alice@prisma.io', name: 'Alice' }
})const user = await prisma.user.delete({
where: { id: 1 }
})
// Returns deleted recordconst result = await prisma.user.deleteMany({
where: { role: 'GUEST' }
})
// Returns { count: 5 }
// Delete all
const result = await prisma.user.deleteMany({})const count = await prisma.user.count({
where: { role: 'ADMIN' }
})const result = await prisma.post.aggregate({
_avg: { views: true },
_sum: { views: true },
_min: { views: true },
_max: { views: true },
_count: { _all: true }
})const groups = await prisma.user.groupBy({
by: ['country'],
_count: { _all: true },
_avg: { age: true },
having: {
age: { _avg: { gt: 30 } }
}
})