Home › Drizzle ORM › Drizzle ORM rules

Drizzle ORM rules

Cursor rules for Drizzle ORM with Postgres or SQLite: schema files, drizzle-kit migrations, and type-safe queries.

CursorDrizzle ORM

How to use

Save this as .cursor/rules/drizzle.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.cursor/rules/drizzle.mdcDownload  
---
description: Drizzle ORM conventions
globs: ["src/db/**","drizzle.config.ts"]
alwaysApply: false
---

# Drizzle ORM

- Tables are defined in `src/db/schema.ts` (or `src/db/schema/*.ts`) with `pgTable`/`sqliteTable`. Export inferred types: `type User = typeof users.$inferSelect`, `NewUser = typeof users.$inferInsert`.
- Migrations: `npx drizzle-kit generate` after schema changes, review the SQL, then `npx drizzle-kit migrate`. Use `drizzle-kit push` only for local prototypes.
- Declare `relations()` to use the relational query API (`db.query.users.findMany({ with: { posts: true } })`); use the SQL-like builder for joins and aggregates.
- Always pass a `where` to `update` and `delete`. Use `eq`, `and`, `inArray` helpers; the `sql` template only for things the builder can't express, with values interpolated as parameters.
- Return what you need: `.returning({ id: users.id })`, `db.select({ ... })`.
- Transactions with `db.transaction(async (tx) => ...)`, using `tx` inside.
- Indexes and unique constraints in the table's third argument; timestamps with `defaultNow()` and `$onUpdate`.
- Validate inputs with `drizzle-zod` (`createInsertSchema`) or zod schemas derived from the table.

## Don't
- Don't string-concatenate SQL or call `sql.raw()` with user input.
- Don't edit generated migration files after they've been applied anywhere.

## Done when
`drizzle-kit check`, typecheck and tests pass.

More: all Drizzle ORM rules · all Cursor rules

Related rules

Convex backend rules

Cursor rules for Convex backends: schema, validated queries/mutations/actions, indexes, and auth checks.

CursorConvex

Firebase (modular SDK) rules

Cursor rules for Firebase web apps with the modular v11+ SDK, Firestore security rules, and emulator tests.

CursorFirebase

Mongoose (MongoDB ODM) rules

Cursor rules for Mongoose 8 with TypeScript: typed schemas, indexes, lean queries, validation, and transactions.

CursorMongoDB

Prisma ORM rules

Cursor rules for Prisma ORM 6 with PostgreSQL: schema changes, migrations, typed queries, and transactions.

CursorPrisma

Supabase backend rules

Cursor rules for apps on Supabase: Row Level Security first, migrations, typed clients and Edge Functions.

CursorSupabase

MongoDB Windsurf rules

Windsurf rules for MongoDB 8 apps with the Node.js driver: schema design, indexes, aggregation, and transactions.

WindsurfMongoDB

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.