Lesson 19 of 49 intermediate

Prisma — Your Database Translator

Talk to Your Database in TypeScript

Open interactive version (quiz + challenge)

Real-world analogy

Imagine you only speak English but your database speaks a completely different language. Without Prisma, you'd have to learn that language yourself (raw queries). With Prisma, you speak English (TypeScript), and Prisma translates everything perfectly — with spell check!

What is it?

Prisma is a modern ORM (Object-Relational Mapping) for TypeScript and Node.js. It auto-generates a type-safe database client from your schema, making database operations feel like writing regular TypeScript code.

Real-world relevance

Prisma is used by companies like Daimler, Rapha, and many YC startups. It's the most popular TypeScript ORM, especially loved for its developer experience and type safety.

Key points

Code example

// Step 1: Define your schema (prisma/schema.prisma) 📝
model User {
  id    String @id @default(auto()) @map("_id") @db.ObjectId
  name  String
  email String @unique
  age   Int
  posts Post[]       // One user has many posts!
  createdAt DateTime @default(now())
}

model Post {
  id       String @id @default(auto()) @map("_id") @db.ObjectId
  title    String
  content  String
  author   User   @relation(fields: [authorId], references: [id])
  authorId String @db.ObjectId
}

// Step 2: Use it in your code — look how clean! ✨
// Create
const user = await prisma.user.create({
  data: {
    name: "Alex",
    email: "alex@dev.com",
    age: 25,
  },
});

// Read
const user = await prisma.user.findUnique({
  where: { email: "alex@dev.com" }
});

// Find with relations (auto-JOINs!)
const userWithPosts = await prisma.user.findUnique({
  where: { email: "alex@dev.com" },
  include: { posts: true }, // Magic! 🪄
});

// Type-safe filtering
const adults = await prisma.user.findMany({
  where: { age: { gte: 18 } },
  orderBy: { name: 'asc' },
  take: 10,
});
// Try typing prisma.user.fi... and see the autocomplete! 🤩

// Update
await prisma.user.update({
  where: { id: "userId" },
  data: { age: 26 }
});

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

Line-by-line walkthrough

  1. 1. Step 1: Define your schema (prisma/schema.prisma) 📝
  2. 2. Defining a database model/table
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9. Closing block
  10. 10.
  11. 11. Defining a database model/table
  12. 12.
  13. 13.
  14. 14.
  15. 15.
  16. 16.
  17. 17. Closing block
  18. 18.
  19. 19. Step 2: Use it in your code — look how clean! ✨
  20. 20. Create
  21. 21. Declaring a variable
  22. 22.
  23. 23.
  24. 24.
  25. 25.
  26. 26. Closing block
  27. 27.
  28. 28.
  29. 29. Read
  30. 30. Declaring a variable
  31. 31.
  32. 32.
  33. 33.
  34. 34. Find with relations (auto-JOINs!)
  35. 35. Declaring a variable
  36. 36.
  37. 37.
  38. 38.
  39. 39.
  40. 40. Type-safe filtering
  41. 41. Declaring a variable
  42. 42.
  43. 43.
  44. 44.
  45. 45.
  46. 46. Try typing prisma.user.fi... and see the autocomplete! 🤩
  47. 47.
  48. 48. Update
  49. 49. Waiting for an async operation to complete
  50. 50.
  51. 51.
  52. 52.
  53. 53.
  54. 54. Delete
  55. 55. Waiting for an async operation to complete
  56. 56.
  57. 57.

Spot the bug

const users = await prisma.user.findMany({
  where: { age: { gt: "18" } }
});
Need a hint?
Look at the type of the value being compared...
Show answer
The comparison value '18' is a string but age is a number field. Prisma expects a number for numeric comparisons. Fix: change '"18"' to just 18 (without quotes).

Explain like I'm 5

Imagine you only speak English but your toy box speaks French. Prisma is a magic translator friend! You say 'give me the red car' in English, Prisma translates to French, and the toy box gives you the red car. You never need to learn French!

Fun fact

Before Prisma, the most popular MongoDB ORM was Mongoose (created in 2010). Prisma launched MongoDB support in 2021 and quickly became the go-to choice for TypeScript devs! 🚀

Hands-on challenge

Design a Prisma schema for an online bookstore: a Book model (title, isbn, price, publishedAt), an Author model (name, bio), and a Publisher model. Set up the relationships — an author can write many books, a publisher publishes many books, but each book has exactly one publisher. Run migrate and write a query that finds all books by a specific author, sorted by price.

More resources

Open interactive version (quiz + challenge) ← Back to course: Full-Stack Playbook