Skip to main content
rule

drizzle — Cursor rule: Drizzle ORM best practices

A Cursor rule that hands your agent a fixed set of Drizzle ORM conventions: split schema files, explicit column constraints, consistent naming.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/drizzle.mdc
Open on GitHub

What drizzle does

This rule tells Cursor how to write Drizzle ORM code in TypeScript. Instead of inventing a schema layout on every prompt, the agent follows the conventions spelled out in the file.

Schemas get split by domain: one file per entity under src/db/schema/, with a central index.ts re-exporting them for Drizzle Kit. TypeScript fields stay in camelCase, database columns in snake_case, and enums are declared once with pgEnum and reused. Every column needs primaryKey(), notNull() and default() stated outright — a missing notNull() only shows up at runtime, when a null reaches the database.

The rule is scoped by the glob **/*.{ts,js}, so it applies to every TypeScript and JavaScript file in the project. If your schema currently lives in one big file, expect the agent to start proposing a split — worth deciding up front whether you want that churn. The examples import from drizzle-orm/pg-core, so they target PostgreSQL.

What it helps with

  • split tables into per-domain files instead of one dump
  • add notNull and default to every column
  • keep camelCase in TypeScript and snake_case in the database
  • declare an enum once and reuse it
  • re-export all schemas from index.ts for Drizzle Kit

Example prompts for the agent

  • add a comments table under src/db/schema with a foreign key to users
  • check my schemas for missing notNull and primaryKey
  • split src/db/schema.ts into per-domain files and update index.ts

How to install drizzle

Manually: put the files in place

.cursor/rules/drizzle.mdc

Frontmatter: description, globs, alwaysApply

Step by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules

Questions about drizzle

How do I install the drizzle rule in Cursor?

Place the file at .cursor/rules/drizzle.mdc in your project root and Cursor picks it up as a rule.

What does the drizzle rule do?

It gives Cursor's agent Drizzle ORM conventions: modular schema files, explicit column constraints, consistent naming and reusable enums.

Which database does the rule target?

The examples import from drizzle-orm/pg-core, so they assume PostgreSQL. For MySQL or SQLite you swap the imports and column types.

Source text

drizzle.mdc · CC0-1.0
---
description: This guide provides definitive, opinionated best practices for using Drizzle ORM to build robust, type-safe, and performant database applications in TypeScript.
globs: **/*.{ts,js}
---
# Drizzle ORM Best Practices

Drizzle ORM is our go-to for type-safe SQL. Follow these guidelines religiously to ensure maintainable, performant, and secure database interactions.

## 1. Code Organization and Structure

Organize your schema logically. Each domain should have its own file, and all schemas should be re-exported from a central `index.ts` for Drizzle Kit.

### Schema File Structure

**✅ GOOD: Modular Schema Files**
Keep each logical domain in its own file under `src/db/schema/`.

```typescript
// src/db/schema/users.ts
import { pgTable, serial, text, varchar } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  fullName: text('full_name').notNull(),
  email: varchar('email', { length: 256 }).unique().notNull(),
});

// src/db/schema/posts.ts
import { pgTable, serial, text, timestamp, varchar } from 'drizzle-orm/pg-core';
import { users } from './users'; // Import related schema

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  title: varchar('title', { length: 256 }).notNull(),
  content: text('content'),
  authorId: serial('author_id').references(() => users.id).notNull(), // Foreign key
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

// src/db/schema/index.ts (for Drizzle Kit and client)
import * as usersSchema from './users';
import * as postsSchema from './posts';

export const schema = {
  ...usersSchema,
  ...postsSchema,
};
```

**❌ BAD: Monolithic Schema File**
Avoid dumping all tables into a single file. It hinders discoverability and tree-shaking.

```typescript
// src/db/schema.ts
import { pgTable, serial, text, varchar, timestamp } from 'drizzle-orm/pg-core';

export const users = pgTable('users', { /* ... */ });
export const posts = pgTable('posts', { /* ... */ });
// ... many more tables
```

### Naming Conventions

**✅ GOOD: `camelCase` for TypeScript, `snake_case` for Database**
This aligns with community standards and keeps your code idiomatic. Declare enums with `pgEnum` and reuse them.

```typescript
import { pgTable, serial, text, varchar, pgEnum } from 'drizzle-orm/pg-core';

export const userRoleEnum = pgEnum('user_role', ['admin', 'editor', 'viewer']);

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  fullName: text('full_name').notNull(), // TS: fullName, DB: full_name
  emailAddress: varchar('email_address', { length: 256 }).unique().notNull(), // TS: emailAddress, DB: email_address
  role: userRoleEnum('role').default('viewer').notNull(),
});
```

**❌ BAD: Inconsistent Naming**
Mixing casing styles leads to confusion and potential errors.

```typescript
export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  full_name: text('full_name').notNull(), // TS: full_name (camelCase expected)
  email_address: varchar('email_address', { length: 256 }).unique().notNull(),
});
```

## 2. Data Modeling

Define your schema precisely. Leverage Drizzle's type inference by explicitly marking constraints and properties.

### Primary Keys, Nullability, and Defaults

Always define primary keys, explicitly mark `notNull()`, and use `default()` for consistent data.

**✅ GOOD: Explicit Constraints**

```typescript
import { pgTable, serial, text, timestamp, boolean } from 'drizzle-orm/pg-core';

export const tasks = pgTable('tasks', {
  id: serial('id').primaryKey(), // Explicit Primary Key
  title: text('title').notNull(), // Required field
  description: text('description'), // Optional field
  isComplete: boolean('is_complete').default(false).notNull(), // Default value, required
  createdAt: timestamp('created_at').defaultNow().notNull(), // Default to current timestamp
  updatedAt: timestamp('updated_at').defaultNow().onUpdateFn(() => new Date()).notNull(), // Auto-update timestamp
});
```

**❌ BAD: Missing Constraints**
Omitting `primaryKey()` or `notNull()` can lead to runtime errors and unexpected database behavior.

```typescript
export const tasks = pgTable('tasks', {
  id: serial('id'), // Missing .primaryKey()
  title: text('title'), // Missing .not

© sanjeed5 · CC0-1.0 · sanjeed5/awesome-cursor-rules-mdc

Similar IDE rules

All IDE rules for Data and databases →

From the same set69

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor