Prisma
Prisma is a next-generation ORM with an intuitive data model, automated migrations, and powerful type safety.
Prisma is a modern database toolkit that makes database access easy with auto-generated type-safe queries and a visual database browser.
Why use Prisma?
- Auto-Generated Types: Fully typed client generated from your schema
- Prisma Studio: Visual database browser for exploring and editing data
- Migration System: Declarative migrations with automatic SQL generation
- Introspection: Generate Prisma schema from existing database
- Developer Experience: Excellent IntelliSense and error messages
- Active Development: Large community and frequent updates
Requirements: Database (PostgreSQL or MySQL)
Technical Changes
Package Structure
- Turborepo mode: Creates
packages/db/with@repo/dbpackage - Single repo mode: Database logic in app's
src/lib/db/directory
Dependencies
package.jsonincludes:@prisma/client: Auto-generated database clientprisma(dev): CLI for migrations and introspection
Scripts
build: Compile package todist/index.js(turborepo only)db:generate: Generate Prisma Client from schemadb:pull: Introspect database and update schemadb:push: Push schema to database without migrations (dev)db:studio: Launch Prisma Studio visual editordb:seed: Run seed script to populate database
Prisma Schema
- Creates
prisma/schema.prismawith:- Generator:
prisma-client-jswith custom output path (turborepo:../generated/prisma) - Datasource: Provider
postgresqlormysql, URL from env - User Model: id (uuid), username (unique), email (unique), emailVerified, avatarUrl, phone, firstName, lastName, timestamps
- Post Model: id (uuid), title, content, published, authorId (FK), timestamps, author relation
- Relations: User hasMany Posts, Post belongsTo User with
onDelete: Cascade - Better Auth Models (conditional if better-auth module selected):
- Session Model: id, userId (FK), token (unique), expiresAt, ipAddress, userAgent, timestamps
- Account Model: id, userId (FK), accountId, providerId, tokens, scope, idToken, password, timestamps
- Verification Model: id, identifier, value, expiresAt, timestamps
- Generator:
Database Client
- Creates
src/index.ts:- PrismaClient singleton pattern with global caching (prevents hot-reload connection issues)
- Logging:
query,error,warnin development;erroronly in production - Re-exports all Prisma Client types
- Type Helpers:
User,Post: Basic model typesUserWithPosts: User with posts includedPostWithAuthor: Post with author includedSession,Account,Verification(conditional): Better Auth types
Environment
- Creates
.env.example:DATABASE_URLtemplate with connection string format
Seed Script
- Creates
scripts/seed.ts:- Clears existing data with
.deleteMany()(posts, users) - Creates 2 sample users (Alice, Bob) with
.create() - Creates 3 sample posts with
.createMany()(2 published, 1 draft) - Demonstrates Prisma Client API and relations
- Properly disconnects with
$disconnect()in finally block
- Clears existing data with

