Introduction to Prisma
Prisma is a next-generation ORM that consists of the following tools:
- Prisma Client: Auto-generated, type-safe query builder for Node.js & TypeScript.
- Prisma Migrate: Migration system.
- Prisma Studio: GUI to view and edit data in your database.
The Schema File
Everything starts in schema.prisma:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
Type-Safe Queries
Prisma generates a client based on your schema, giving you incredible autocomplete:
const user = await prisma.user.findUnique({
where: { email: 'alice@prisma.io' },
include: { posts: true }, // Typed result includes posts!
});