Prisma ORM
Learn how to define database models and generate the Prisma Client.
Prisma ORM provides a type-safe database client for Plainform, generated from your database schema. The prisma/schema.prisma file defines your database structure, and Prisma generates TypeScript types and a client for querying your data.
Schema File Structure
The prisma/schema.prisma file has three main sections:
// 1. Generator - how Prisma generates the client
generator client {
provider = "prisma-client-js"
}
// 2. Datasource - database connection
datasource db {
provider = "postgresql"
url = env("DATABASE_URL") // Pooled connection
directUrl = env("DIRECT_URL") // Direct connection
}
// 3. Models - database tables
model Event {
id String @id @default(cuid())
type String
slug String
text String
timestamp BigInt
}Defining Models
Basic Model
model User {
id String @id @default(cuid())
email String @unique
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}Common attributes:
@id: Primary key@default(cuid()): Auto-generate unique ID@unique: Enforce uniqueness@updatedAt: Auto-update on changes?: Optional field
Relationships
model Post {
id String @id @default(cuid())
title String
authorId String
author User @relation(fields: [authorId], references: [id])
comments Comment[]
@@index([authorId])
}
model Comment {
id Int @id @default(autoincrement())
text String
postId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
@@index([postId])
}
model User {
id String @id @default(cuid())
name String
posts Post[]
}Referential actions:
onDelete: Cascade: Delete related recordsonDelete: SetNull: Set foreign key to nullonDelete: Restrict: Prevent deletion if related records exist
Indexes
Add indexes for frequently queried fields:
model Post {
id String @id @default(cuid())
authorId String
status String
createdAt DateTime @default(now())
@@index([authorId])
@@index([status, createdAt]) // Composite index
}Indexes improve query performance but slow down writes. Add them for fields used in where, orderBy, or join clauses.
Common Field Types
model Example {
id String @id @default(cuid())
// Text
name String // VARCHAR(191)
description String @db.Text // TEXT (unlimited)
// Numbers
age Int // INTEGER
price Float // DOUBLE PRECISION
count BigInt // BIGINT
// Dates
createdAt DateTime @default(now()) // TIMESTAMP
// Boolean
isActive Boolean @default(true)
// JSON
metadata Json // JSONB
}Generating the Client
After modifying schema.prisma, regenerate the Prisma Client:
npx prisma generateThis updates the Prisma Client with your schema changes and generates TypeScript types.
Run npx prisma generate after every schema change. The postinstall script in package.json runs this automatically after npm install.
Schema Validation
Validate your schema without generating the client:
npx prisma validateIntrospection
Pull existing database schema into Prisma:
npx prisma db pullUseful when connecting to an existing database or syncing after external changes.
db pull overwrites your schema.prisma. Commit changes first.
Next Steps
- Usage & Integration - Use Prisma Client in your code
- Migrations - Apply schema changes to your database
- Prisma Schema Reference - Complete documentation
How is this guide ?
Last updated on