Prisma ORM — Schema, Client & Migrations
Prisma is a type-safe ORM for Node.js. Its schema.prisma file is the single source of truth for your data model — the Prisma Client is auto-generated from it, giving you full TypeScript type safety on every query. Prisma Migrate keeps your database schema in sync with your code.
Installation & Setup
npm install prisma @prisma/client
npx prisma init --datasource-provider postgresql
# This creates:
# prisma/schema.prisma ← your database schema
# .env ← DATABASE_URL
# schema.prisma
# generator client {
# provider = "prisma-client-js"
# }
# datasource db {
# provider = "postgresql"
# url = env("DATABASE_URL")
# }Schema Definition
// prisma/schema.prisma
model User {
id String @id @default(uuid())
email String @unique
name String
role Role @default(USER)
password String // hashed — never store plaintext
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
posts Post[] // one-to-many
profile UserProfile? // one-to-one (optional)
@@index([email]) // index for fast lookups
@@map("users") // actual table name
}
enum Role { USER ADMIN MODERATOR }
model Post {
id String @id @default(uuid())
title String
content String @db.Text // TEXT column (unlimited length)
published Boolean @default(false)
authorId String
categoryId String?
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
category Category? @relation(fields: [categoryId], references: [id])
tags Tag[] // many-to-many via implicit join table
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([authorId])
@@index([published, createdAt(sort: Desc)]) // compound index
@@map("posts")
}
model Category {
id String @id @default(uuid())
name String @unique
slug String @unique
posts Post[]
@@map("categories")
}
model Tag {
id String @id @default(uuid())
name String @unique
posts Post[]
@@map("tags")
}
model UserProfile {
id String @id @default(uuid())
bio String?
avatar String?
userId String @unique
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@map("user_profiles")
}
# Commands:
# npx prisma migrate dev --name init ← create migration, apply, regenerate client
# npx prisma migrate deploy ← apply in production (no dev tooling)
# npx prisma db push ← sync schema without migration (prototyping)
# npx prisma studio ← browser-based DB GUITip
Tip
Practice Prisma ORM Schema Client Migrations in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Technical diagram.
Practice Task
Note
Practice Task — (1) Write a working example of Prisma ORM Schema Client Migrations from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Prisma ORM Schema Client Migrations is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready node code.