Create FasterCreate Faster

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/db package
  • Single repo mode: Database logic in app's src/lib/db/ directory

Dependencies

  • package.json includes:
    • @prisma/client: Auto-generated database client
    • prisma (dev): CLI for migrations and introspection

Scripts

  • build: Compile package to dist/index.js (turborepo only)
  • db:generate: Generate Prisma Client from schema
  • db:pull: Introspect database and update schema
  • db:push: Push schema to database without migrations (dev)
  • db:studio: Launch Prisma Studio visual editor
  • db:seed: Run seed script to populate database

Prisma Schema

  • Creates prisma/schema.prisma with:
    • Generator: prisma-client-js with custom output path (turborepo: ../generated/prisma)
    • Datasource: Provider postgresql or mysql, 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

Database Client

  • Creates src/index.ts:
    • PrismaClient singleton pattern with global caching (prevents hot-reload connection issues)
    • Logging: query, error, warn in development; error only in production
    • Re-exports all Prisma Client types
    • Type Helpers:
      • User, Post: Basic model types
      • UserWithPosts: User with posts included
      • PostWithAuthor: Post with author included
      • Session, Account, Verification (conditional): Better Auth types

Environment

  • Creates .env.example:
    • DATABASE_URL template 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