Prisma Setup & Migrations

How to set up Prisma, run database migrations, and manage schema changes.

Last updated:

Prisma configuration

BloggFast uses Prisma 7 with the driverAdapters preview feature enabled. This allows Prisma to use Neon's serverless WebSocket driver (@neondatabase/serverless) via @prisma/adapter-neon, which is required for serverless and edge environments.

prisma/schema.prisma
generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["driverAdapters"]
}

datasource db {
  provider = "postgresql"
}

The DATABASE_URL is read from your environment. Notice the datasource does not hardcode the URL — it's always pulled from the environment variable at runtime.

Generate the Prisma client

After installing dependencies and setting DATABASE_URL, generate the typed client:

bash
npm run db:generate
# Runs: prisma generate

This creates the typed client in node_modules/.prisma/client based on your schema. This step is automatically run during npm run build.

Run migrations

Apply the initial schema to your Neon database:

bash
npm run db:migrate
# Runs: prisma migrate dev

This creates all the tables defined in prisma/schema.prisma. You'll see output confirming each migration was applied. For a quick schema sync without migration history (useful in early development):

bash
npm run db:push
# Runs: prisma db push

Warning

Always use DATABASE_URL_UNPOOLED for migrations. The pooled connection string (via pgBouncer) does not support the persistent connections Prisma needs during migrations. Make sure your .env.local has both connection strings set.

Prisma Studio

Open the visual database browser:

bash
npm run db:studio
# Opens at http://localhost:5555

Studio lets you create, read, update, and delete records directly — useful for seeding test data, granting admin roles to users, or debugging database state.

Schema overview

The BloggFast Prisma schema (prisma/schema.prisma) includes these models:

ModelPurpose
UserAuthenticated users with roles (USER, EDITOR, ADMIN)
AuthorArticle bylines with bio, avatar, social links, and specialties
CategoryArticle categories with slug and color
TagArticle tags for fine-grained classification
ArticleCore article model — title, subtitle, body (markdown), status, SEO fields, cover image, and social counts
ArticleTagMany-to-many join table for article ↔ tag relationships
CommentUser comments on articles with moderation status
SavedArticleUser-saved articles (bookmarks)
LikedArticleUser-liked articles
UserInterestUser interest categories (for personalization onboarding)
ArticleViewArticle view analytics with session IDs
ArticleGenerationRequestAI generation history — prompt, status, result data
AuditLogAdmin action audit trail
SiteSettingsSingleton site config (name, description, logo, social links)
AiSettingsSingleton AI config (model, image model, system prompt, tone, word count)
SubscriberEmail newsletter subscribers with full status tracking
SubscriberEventEmail event log (subscribed, confirmed, bounced, etc.)

Making schema changes

  1. Edit prisma/schema.prisma
  2. Run npm run db:migrate to generate and apply a migration
  3. Run npm run db:generate if you need updated TypeScript types immediately

Production migrations

On Vercel, migrations run automatically during the build step via the build command:

bash
# Build command in package.json scripts:
prisma generate && next build

For applying pending migrations in production, use prisma migrate deploy (not prisma migrate dev). You can add this to your Vercel build command or run it manually from the CLI with production environment variables:

bash
# Run from your local machine with production DATABASE_URL
DATABASE_URL="your-production-db-url" npx prisma migrate deploy