Drizzle ORM: getting started with type-safe database access in Next.js
Drizzle ORM is the database toolkit used in all CheapStack kits. It provides type-safe database access for both SQLite and PostgreSQL.
Why Drizzle
- Zero-abstraction: Write SQL-like queries with TypeScript types
- Supports SQLite, PostgreSQL, and MySQL
- No code generation step (unlike Prisma)
- Tiny bundle size (compared to Prisma's ~5MB)
- Works with edge runtimes (Cloudflare Workers, Vercel Edge)
Schema definition
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: text("id").primaryKey(),
name: text("name"),
email: text("email").notNull().unique(),
createdAt: integer("created_at", { mode: "timestamp" })
.$defaultFn(() => new Date()),
});
For PostgreSQL, import from drizzle-orm/pg-core instead.
Queries
import { db, users } from "@/lib/db";
// Select all users
const allUsers = await db.select().from(users);
// Insert
await db.insert(users).values({
email: "user@example.com",
name: "Alice",
});
Migrations
npm run db:generate # Generate SQL migration
npm run db:push # Apply to database
npm run db:studio # Open Drizzle Studio (GUI)
SQLite vs Postgres in Drizzle
Drizzle supports both dialects with the same query API. The CheapStack SaaS Boilerplate ($59) allows switching via environment variable:
DATABASE_TYPE=sqlite # Uses Turso/libSQL
DATABASE_TYPE=postgres # Uses Neon/any Postgres
The schema files export separate table definitions for each dialect, and the database client is initialized based on the environment variable.
About the author: Muhammad Akbar builds affordable developer tools at CheapStack and writes about bootstrapping products on realistic budgets.