Pick an ORM, Use It From Server Routes
Database Access — Drizzle / Prisma
Both Drizzle and Prisma work in Nitro server routes. Drizzle is the better choice for edge runtimes; Prisma works well on Node-based deployments.
What you'll learn
- Install either drizzle-orm or prisma and configure your schema
- Define a singleton client in server/utils so it's reused across requests
- Query the database from a handler and return typed results
Real apps need a database. Drizzle and Prisma both work cleanly inside Nitro server routes — they differ mostly in runtime compatibility and ergonomic preferences.
Option 1: Drizzle
npm i drizzle-orm postgres
npm i -D drizzle-kit Define a schema:
// server/db/schema.ts
import { pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core'
export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: text('email').notNull().unique(),
createdAt: timestamp('created_at').defaultNow().notNull(),
}) Singleton client:
// server/utils/db.ts
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
const client = postgres(useRuntimeConfig().dbUrl)
export const db = drizzle(client) Query from a route:
// server/api/users.get.ts
import { users } from '~/server/db/schema'
export default defineEventHandler(async () => {
return await db.select().from(users)
}) Option 2: Prisma
npm i @prisma/client
npm i -D prisma
npx prisma init // server/utils/prisma.ts
import { PrismaClient } from '@prisma/client'
const globalForPrisma = globalThis as unknown as { prisma?: PrismaClient }
export const prisma = globalForPrisma.prisma ?? new PrismaClient()
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma // server/api/users.get.ts
export default defineEventHandler(async () => {
return await prisma.user.findMany()
}) Edge Runtimes
Cloudflare Workers and similar edge runtimes don’t allow TCP sockets, so the classic postgres
driver won’t work. Options:
- Neon serverless driver with Drizzle: HTTP-based, works on edge.
- Prisma Accelerate / Data Proxy: HTTP proxy in front of your DB.
- Cloudflare D1 (SQLite): native to Workers, supported by Drizzle.
Migrations
- Drizzle:
drizzle-kit generateproduces SQL, then rundrizzle-kit migrate. - Prisma:
prisma migrate devandprisma migrate deploy.
Run them in your CI pipeline before the app deploys, not at runtime.
SEO & Head Management →