Prisma
databasesprismaGuideBeginner
Prisma CRUD Operations
Comprehensive guide to Create, Read, Update, and Delete operations using Prisma Client: findUnique, findMany, update, delete, and upsert.
Verified: 2026-10-01
Tested on:prisma 6.x
Platforms:
node
#prisma#crud#queries#mutations#typescript
Prisma CRUD Operations
Prisma Client provides an intuitive, type-safe API for executing database queries and mutations.
1. Create (Insert Records)
Single Record:
const newUser = await prisma.user.create({
data: {
email: "alex@example.com",
name: "Alex Rivera",
role: "USER",
},
});Nested Relational Creation:
Create a parent user and their first post in a single atomic database query:
const userWithPost = await prisma.user.create({
data: {
email: "sarah@example.com",
name: "Sarah Chen",
posts: {
create: {
title: "Getting Started with Next.js & Prisma",
content: "Prisma makes full-stack development seamless.",
published: true,
},
},
},
include: { posts: true },
});Batch Create (createMany):
await prisma.tag.createMany({
data: [
{ name: "typescript" },
{ name: "nextjs" },
{ name: "tailwindcss" },
],
skipDuplicates: true,
});2. Read (Query Records)
Find by Unique Identifier / Index:
// Returns User or null
const user = await prisma.user.findUnique({
where: { email: "alex@example.com" },
});
// Throws error if record is not found (Useful in API routes)
const requiredUser = await prisma.user.findUniqueOrThrow({
where: { id: "usr_12345" },
});Find Many with Selective Fields (select vs include):
// Fetch only specific columns (reduces bandwidth and memory)
const userProfiles = await prisma.user.findMany({
select: {
id: true,
name: true,
email: true,
},
});3. Update & Upsert
Update Single Record:
const updatedUser = await prisma.user.update({
where: { id: "usr_12345" },
data: {
name: "Alexander Rivera",
},
});Upsert (Update if exists, Create if not):
const user = await prisma.user.upsert({
where: { email: "claire@example.com" },
update: { name: "Claire Novak" },
create: {
email: "claire@example.com",
name: "Claire Novak",
},
});4. Delete Records
// Delete single record
const deletedPost = await prisma.post.delete({
where: { id: 42 },
});
// Delete many matching a filter
const result = await prisma.session.deleteMany({
where: {
expiresAt: { lt: new Date() }, // Delete all expired sessions
},
});
console.log(`Deleted ${result.count} expired sessions.`);Related Topics
Prisma Client & Singleton Instantiation
Initializing PrismaClient, preventing connection pool exhaustion during Next.js hot-reloading using the global singleton pattern, and logging options.
Prisma Filtering & Advanced Queries
Master where clauses in Prisma: string filters (contains, startsWith), numbers (gt, lt), lists (in), logical operators (AND, OR, NOT), and relation filters.