Skip to content

PostgreSQL with Drizzle ORM

Nuxt Starter Kit uses PostgreSQL with Drizzle ORM. Each layer owns its tables, drizzle-kit generates the migrations, and any PostgreSQL provider works.

The starter kit uses PostgreSQL as the database, managed through Drizzle ORM for excellent developer experience and type safety.

Database Provider

You can use PostgreSQL from various providers:

  • Neon - Serverless PostgreSQL with generous free tier
  • Supabase - Open source Firebase alternative with PostgreSQL
  • Railway - Simple PostgreSQL hosting
  • Render - Managed PostgreSQL databases
  • Local PostgreSQL - For development

Drizzle ORM

The starter kit uses Drizzle ORM instead of raw SQL for several benefits:

  • Type safety - Full TypeScript support with auto-completion
  • Migrations - Automated schema migrations
  • Relations - Easy relationship management
  • Developer experience - Intuitive query builder

Database Schema

Every layer owns its tables in server/db/schema/*.ts:

Table fileLayer
auth.ts - user, session, account, verification, subscription, passkey (better-auth)apps/web/layers/auth
file.ts - File storage metadataapps/web/layers/storage
testimonial.ts - Testimonialsapps/web/layers/testimonials
waitlist.ts - Waitlist signupsapps/web/layers/waitlist
banner.ts - App bannersapps/web/layers/banner

drizzle-kit collects the schema from apps/web/layers/*/server/db/schema/*.ts. The configuration is in apps/web/layers/auth/server/db/drizzle.config.ts, and all migrations are in apps/web/layers/auth/server/db/migrations/.

When you delete an optional layer, pnpm db:generate creates a migration that drops its tables.

Database Utilities

useDb() is auto-imported in server code. Import the tables of your own layer with a relative path:

// apps/web/layers/testimonials/server/utils/useTestimonials.ts
import { desc } from 'drizzle-orm'

import { testimonial } from '../db/schema/testimonial'

const db = useDb()
const testimonials = await db.select()
  .from(testimonial)
  .orderBy(desc(testimonial.createdAt))

Commands

Run these commands from the repository root:

# Generate migrations from schema changes
pnpm db:generate

# Apply migrations to database
pnpm db:migrate

# Open Drizzle Studio (database GUI)
pnpm db:studio

Environment Variables

Set your PostgreSQL connection string in apps/web/.env. drizzle-kit reads it from there, too:

NUXT_PRIVATE_DATABASE_URL="postgresql://user:password@host:5432/dbname"

For local development, you might use:

NUXT_PRIVATE_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/nuxtstarterkit"

Migration Workflow

  1. Change or add a schema file in server/db/schema/ of the layer the table belongs to
  2. Run pnpm db:generate to create migration files
  3. Review generated migrations in apps/web/layers/auth/server/db/migrations/
  4. Run pnpm db:migrate to apply migrations
  5. Commit both schema and migration files

Drizzle Studio

Drizzle Studio provides a visual interface to browse and edit your database:

pnpm db:studio

This opens a web interface where you can:

  • Browse tables and data
  • Run queries
  • Edit records
  • View relationships

Type Safety

Drizzle infers TypeScript types from your schema. User and Session are auto-imported from apps/web/layers/auth/shared/types/db.ts, and each feature layer exports its types from shared/types/:

// apps/web/layers/auth/shared/types/db.ts
import type { session, user } from '../../server/db/schema/auth'

export type User = typeof user.$inferSelect
export type Session = typeof session.$inferSelect

Tables of another layer are imported with a relative path, because drizzle-kit doesn't resolve aliases:

// apps/web/layers/storage/server/db/schema/file.ts
import { user } from '../../../../auth/server/db/schema/auth'

Best Practices

  • Always use migrations - never modify the database schema directly
  • Keep schema files organized by feature
  • Use meaningful column and table names
  • Add indexes for frequently queried columns
  • Use transactions for multiple related operations