drizzle — правило для Cursor: best practices Drizzle ORM
Правило задаёт агенту Cursor стиль работы с Drizzle ORM: модульные схемы, явные ограничения полей и единый нейминг в TypeScript-проекте.
.cursor/rules/drizzle.mdcЧто делает drizzle
Это правило для Cursor описывает, как писать код на Drizzle ORM в TypeScript. Агент получает набор соглашений и следует им при генерации схем и запросов, а не придумывает структуру заново каждый раз.
Схемы предлагается разбивать по доменам: отдельный файл на сущность в src/db/schema/, а общий index.ts пересобирает их для Drizzle Kit. Поля в TypeScript именуются в camelCase, столбцы в базе — в snake_case, перечисления объявляются через pgEnum и переиспользуются. Для каждого столбца правило требует явно указать primaryKey(), notNull() и default() — пропущенный notNull() всплывёт уже в рантайме, когда в базу уйдёт null.
Правило подхватывается по маске **/*.{ts,js}, то есть действует на все TypeScript- и JavaScript-файлы проекта. Если у вас схема уже свалена в один общий файл, агент начнёт предлагать её разносить — это повод решить заранее, готовы ли вы к такой перестройке. Примеры в правиле построены на drizzle-orm/pg-core, то есть под PostgreSQL.
Какие задачи решает
- разложить таблицы по файлам вместо одной свалки
- проставить notNull и default для каждого столбца
- согласовать camelCase в TypeScript и snake_case в базе
- объявить enum один раз и переиспользовать
- собрать все схемы в index.ts для Drizzle Kit
Примеры запросов агенту
- добавь таблицу комментариев в src/db/schema и не забудь внешний ключ на users
- проверь мои схемы на пропущенные notNull и primaryKey
- разнеси src/db/schema.ts на файлы по доменам и обнови index.ts
Как установить drizzle
Вручную: положить файлы
.cursor/rules/drizzle.mdcВо frontmatter: description, globs, alwaysApply
Вручную: положить файлы
.claude/rules/drizzle.mdПоле paths во frontmatter ограничивает файлы: globs из источника → paths
AGENTS.md
Codex читает AGENTS.md: вставьте текст правила туда
Вручную: положить файлы
.github/instructions/drizzle.instructions.mdПоле applyTo задаёт, к каким файлам применять: globs из источника → applyTo
Вручную: положить файлы
.devin/rules/drizzle.mdВручную: положить файлы
.clinerules/drizzle.mdПо шагам, с командами для каждой платформы: Cursor rules: файлы .mdc, четыре режима и замена .cursorrules
Вопросы о drizzle
Как установить правило drizzle в Cursor?
Положите файл по пути .cursor/rules/drizzle.mdc в корне проекта — Cursor подхватит его как правило.
Что делает правило drizzle?
Задаёт агенту Cursor соглашения по Drizzle ORM: модульные файлы схем, явные ограничения столбцов, единый нейминг и переиспользуемые enum.
На какую базу рассчитано правило?
Примеры используют drizzle-orm/pg-core, то есть PostgreSQL. Для MySQL или SQLite импорты и типы столбцов придётся заменить.
Исходный текст
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