Node.js, Prisma & PostgreSQL

Lesson 5 of 12

Prisma Client: Basic CRUD

Once your schema is migrated, Prisma Client gives you a typed method for every model.

import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

// Create
const user = await prisma.user.create({
  data: { email: 'ada@example.com', name: 'Ada' },
});

// Read
const users = await prisma.user.findMany();
const one = await prisma.user.findUnique({ where: { id: 1 } });

// Update
const updated = await prisma.user.update({
  where: { id: 1 },
  data: { name: 'Ada Lovelace' },
});

// Delete
await prisma.user.delete({ where: { id: 1 } });

This needs a running Node.js process connected to a real PostgreSQL database, so it's read-only here, you'll run code exactly like this in the final project.

Notice the shape: every method takes a single options object (where, data, ...), and every method returns a fully-typed result, your editor knows user.email is a string without you writing a type annotation anywhere.

📝 CRUD Quiz

Passing score: 70%
  1. 1.Which Prisma Client method fetches every row that matches optional filters?

  2. 2.prisma.user.____({ where: { id: 1 } }) fetches a single user by a unique field.

  3. 3.Prisma Client's create and update methods return the affected row, fully typed.