We use tracking cookies to understand how you use the product and help us improve it. For more information on how we store cookies, read our  privacy policy.

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:

prisma/schema.prisma
// 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

prisma/schema.prisma
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

prisma/schema.prisma
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 records
  • onDelete: SetNull: Set foreign key to null
  • onDelete: Restrict: Prevent deletion if related records exist

Indexes

Add indexes for frequently queried fields:

prisma/schema.prisma
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

prisma/schema.prisma
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:

Terminal
npx prisma generate

This 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:

Terminal
npx prisma validate

Introspection

Pull existing database schema into Prisma:

Terminal
npx prisma db pull

Useful when connecting to an existing database or syncing after external changes.

db pull overwrites your schema.prisma. Commit changes first.

Next Steps

How is this guide ?

Last updated on