Claude Code Prompts for Drizzle ORM

Copy-paste Claude Code prompts for Drizzle ORM — schema definitions, migrations with drizzle-kit, relational queries, and Postgres RLS. Type-safe end-to-end.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

Drizzle's TypeScript-first approach lets Claude produce schema and queries that round-trip cleanly through the type system. The pitfalls: forgetting to regenerate after schema edits, and conflating select-style with query-style.

Prompt 1 — Schema definition with relations

Add a 'projects' table to src/db/schema.ts.

Fields:
- id: uuid, primary, defaultRandom
- ownerId: uuid, references users.id, ON DELETE CASCADE
- name: varchar(200), not null
- slug: varchar(80), not null
- visibility: pgEnum 'project_visibility' [private, team, public]
- archivedAt: timestamp, null
- createdAt: timestamp, default now()
- updatedAt: timestamp, default now()

Constraints:
- Indexes: unique on (ownerId, slug); index on (visibility) WHERE archivedAt IS NULL.
- Define a relations(projects, ({ one, many }) => ({ ... })) block under the table
  so the relational API works.
- Read src/db/schema.ts first; match the existing import style and table-naming convention.
- Run drizzle-kit generate to produce a new migration in drizzle/. Do NOT apply.
- Add the new pgEnum to the existing enums section, not a new place.

Prompt 2 — Relational query with depth limit

Add a server function getProjectWithDetails(projectId: string, viewerId: string)
at src/server/projects.ts that returns:
- Project core fields
- Owner (user.id, user.name, user.avatarUrl only)
- Latest 5 members (membership.role, user.id, user.name)
- Count of open tasks (not the tasks themselves)

Constraints:
- Use the relational query API (db.query.projects.findFirst with `with: { ... }`).
- Use columns: { ... } to restrict fields — never pull the full row.
- For the open-tasks count, use sql`...` for a subquery — or a second db.select
  call if cleaner; pick the one with fewer round trips.
- Authorisation: throw a typed Error('NotAuthorized') if viewer can't see the
  project — implement the rule by calling existing canViewProject() helper.
- Return type must be inferrable; don't manually type the return.

Prompt 3 — Row Level Security via raw SQL migration

Drizzle doesn't manage Postgres RLS policies. Add a raw migration that:
- Enables RLS on projects: ALTER TABLE projects ENABLE ROW LEVEL SECURITY;
- Creates policy 'projects_owner_all' for SELECT, INSERT, UPDATE, DELETE
  where owner_id = current_setting('app.current_user_id')::uuid
- Creates policy 'projects_member_read' for SELECT where the user has a
  matching membership row
- Grants USAGE/SELECT on the table to the application role 'app_user'

Constraints:
- Add as a new SQL file in drizzle/0034_enable_rls_projects.sql (next number
  after the latest).
- Include a journal entry update to drizzle/meta/_journal.json (or note that
  the consumer must run drizzle-kit generate to sync).
- Tests: a TypeScript script in scripts/test-rls.ts that connects with two
  different SETs of app.current_user_id and asserts the visibility differs.

CLAUDE.md for Drizzle

## Drizzle policy
- Schema: src/db/schema.ts — single source of truth.
- Migrations: drizzle/ — generated, not hand-edited.
- Queries: prefer the relational API for reads with joins, db.select() for
  simple fetches or aggregates.
- Always restrict fields with columns: {...} — never select * implicitly.
- Auth context for RLS: set app.current_user_id at the start of every connection
  (already wired in src/db/client.ts).

Related: Prisma prompts.

Frequently asked questions

Will Claude write SQL-style or relational-query-style Drizzle?
Both — pin one. Default is the SQL-style db.select(). For nested reads, the relational query API (db.query.users.findMany with `with:`) is cleaner. Tell Claude which to use.
Drizzle Studio or psql?
Doesn't matter to Claude, but say `Schema source of truth is schema.ts. Do not edit migrations by hand — regenerate with drizzle-kit.`
Will Claude add RLS policies?
Yes if asked. Use the RLS prompt below — Drizzle doesn't manage policies, so they live in raw SQL migrations.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript