MyApp

Getting Started

IntroductionInstallationPull Updates
Architecture

Setup

IDEAI AgentsMCP ServersEnvironment Variables

Workflow

Git WorkflowBuild & DeployTroubleshooting

Authentication

OverviewSetup & ConfigurationUsage & IntegrationTroubleshooting

Payments

OverviewSetup & ConfigurationUsage & IntegrationTroubleshooting

Supabase

OverviewSetup & ConfigurationTroubleshooting

Database

Database SetupPrisma ORMUsage & IntegrationMigrationsTroubleshooting

Storage

OverviewSetup & ConfigurationUsage & IntegrationTroubleshooting

Emails

OverviewSetup and ConfigurationUsage and IntegrationTroubleshooting

SEO

OverviewConfiguration & Best PracticesCustomization & Optimization

UI

OverviewSetup and ConfigurationThemingTroubleshooting
MyApp

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

  • 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

Database Setup

Configure your PostgreSQL database connection for Plainform.

Usage & Integration

Learn how to use Prisma Client in your Plainform application.

On this page

Schema File Structure
Defining Models
Basic Model
Relationships
Indexes
Common Field Types
Generating the Client
Schema Validation
Introspection
Next Steps