Script Valley
REST API Development: Beginner to Production
Database Integration with PostgreSQL and PrismaLesson 5.2

Prisma CRUD — create, read, update, delete with real queries

create, findUnique, findMany, update, delete, upsert, select, where clause, include relations, orderBy, take and skip

Prisma CRUD Operations

Prisma's client API maps closely to SQL operations but with full autocompletion and type safety.

All Five CRUD Operations

const prisma = require('../lib/prisma');

// CREATE
const user = await prisma.user.create({
  data: { email: 'alice@example.com', name: 'Alice' }
});

// READ one
const user = await prisma.user.findUnique({
  where: { id: 42 },
  select: { id: true, email: true, name: true } // never return passwordHash
});

// READ many with filters, pagination, relations
const posts = await prisma.post.findMany({
  where: { authorId: 5, published: true },
  include: { author: { select: { name: true } } },
  orderBy: { createdAt: 'desc' },
  take: 20,   // LIMIT
  skip: 40    // OFFSET
});

// UPDATE
const updated = await prisma.user.update({
  where: { id: 42 },
  data: { name: 'Alice Smith' }
});

// DELETE
await prisma.user.delete({ where: { id: 42 } });

// UPSERT — create if not exists, update if it does
await prisma.user.upsert({
  where: { email: 'bob@example.com' },
  update: { name: 'Bobby' },
  create: { email: 'bob@example.com', name: 'Bobby' }
});

Always use select to explicitly choose fields to return — never return the full object from the database to the client. This prevents accidentally exposing passwordHash, internal flags, or foreign keys clients don't need.

Up next

Database transactions in Prisma — when and how to use them

Sign in to track progress