Lightweight, TS-First, SQL-Like
Database With Drizzle
Drizzle is a popular Prisma alternative — closer to raw SQL, smaller bundle, and edge-friendly.
What you'll learn
- Install `drizzle-orm` and a driver
- Define a schema in TypeScript
- Run queries with the fluent API
Drizzle takes a different approach from Prisma: schemas live in TypeScript files, queries look like SQL builders, and the runtime is small enough to run at the Edge.
Install
npm i drizzle-orm postgres
npm i -D drizzle-kit For Neon or Cloudflare D1, swap postgres for @neondatabase/serverless or the D1 driver.
Define a Schema
// 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(),
name: text('name'),
createdAt: timestamp('created_at').defaultNow(),
}) Connect
// db/index.ts
import 'server-only'
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import * as schema from './schema'
const client = postgres(process.env.DATABASE_URL!)
export const db = drizzle(client, { schema }) Query
import { eq } from 'drizzle-orm'
import { db } from '@/db'
import { users } from '@/db/schema'
// SELECT
const rows = await db.select().from(users).where(eq(users.id, 1))
// INSERT
await db.insert(users).values({ email: 'a@b.com', name: 'A' })
// UPDATE
await db.update(users).set({ name: 'B' }).where(eq(users.id, 1)) Migrations live in drizzle/; generate them with drizzle-kit generate and apply with
drizzle-kit migrate. Because the runtime is so small, Drizzle is the go-to choice when
you need to query a DB from the Edge.