---
name: drizzle-orm-builder
description: "Drizzle ORM — SQL-first TypeScript, schema, queries, migrations, edge-runtime compatible. Builds Drizzle ORM stacks: schema-as-code, type-inferred queries, relational query builder, migrations (drizzle-kit), edge-runtime compatibility (Cloudflare Workers, Vercel Edge), and the comparison framework for picking Drizzle vs Prisma."
license: MIT
---

# Drizzle ORM Builder

Scaffolds and wires up a complete Drizzle ORM data layer for TypeScript projects: schema files, type-inferred queries, relations, and `drizzle-kit` migrations. It targets developers who want SQL they can read and a thin, fully-typed client — without the codegen step or runtime engine that heavier ORMs require. Works across Postgres, MySQL, SQLite/Turso, and edge runtimes.

## When to use it

- You're starting a new backend or API and want schema-as-code with end-to-end type inference.
- You're deploying to Cloudflare Workers, Vercel Edge, or Bun, where a native binary engine won't run.
- You're migrating off Prisma and want to map an existing schema introspected from a live database.
- You need raw SQL escape hatches alongside a query builder, not one or the other.

## What it does

The skill generates dialect-specific schema using `pgTable`, `mysqlTable`, or `sqliteTable`, with column helpers (`serial`, `text`, `integer`, `timestamp`, `boolean`) and constraints. It sets up `relations()` for the relational query builder so you can call `db.query.users.findMany({ with: { posts: true } })`. Types are inferred with `$inferSelect` / `$inferInsert` — no generated client.

It configures `drizzle.config.ts` and the `drizzle-kit` workflow: `drizzle-kit generate` to emit SQL migrations from schema diffs, `drizzle-kit migrate` to apply them, `drizzle-kit push` for prototyping, and `drizzle-kit studio` for the browser data viewer. For edge targets it picks the right driver — `drizzle-orm/neon-http`, `drizzle-orm/d1`, `drizzle-orm/libsql`, or `postgres-js`. It also includes a decision framework comparing Drizzle vs Prisma on bundle size, cold starts, and SQL control.

## Usage

```ts
// src/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(),
});

export type User = typeof users.$inferSelect;
```

```bash
npx drizzle-kit generate   # diff schema -> SQL migration
npx drizzle-kit migrate    # apply pending migrations
```

## Tips & gotchas

- `push` is great for local iteration but skips the migration history — never run it against production; use `generate` + `migrate` there.
- The relational query API (`db.query.*`) and the SQL-like builder (`db.select().from()`) are separate; relations must be declared for the former to resolve `with`.
- On Workers/D1, import from the dialect-specific edge driver, not the Node driver, or you'll hit runtime import errors.
- `defaultNow()` sets the default in SQL; `$defaultFn()` runs in JS at insert time — pick based on where you want the value generated.

## FAQ

**Does Drizzle need a code-generation step?** No. Types come from your schema objects via `$inferSelect`/`$inferInsert` at compile time, so there's no generated client to keep in sync.

**Drizzle or Prisma?** Choose Drizzle for minimal bundle size, edge/serverless cold starts, and direct SQL control; Prisma still leads on its GUI tooling and some DX conveniences. The skill walks you through the tradeoffs for your stack.

**Can I write raw SQL?** Yes — use the `sql` template tag for fragments or `db.execute(sql\`...\`)` for full statements, with parameters still escaped safely.
