drizzle
Rules, standards, and references for Drizzle ORM query styles, performance optimization, type safety, and Drizzle Kit CLI/MCP tooling automation. Use when writing Drizzle queries, defining database services, or configuring migration automation.
- License 许可
- MIT
- 最近更新
- 2026-08-02
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- drizzle
- license
- MIT
- metadata
- { "author": "ngxc", "version": "1.1.0" }
- description
- Rules, standards, and references for Drizzle ORM query styles, performance optimization, type safety, and Drizzle Kit CLI/MCP tooling automation. Use when writing Drizzle queries, defining database services, or configuring migration automation.
- argument-hint
- [--table <string>] [--query-type <string>]
02 资源文件 15
- 参考文档AGENTS.md14.7 KB
- 参考文档references/drizzle-kit-cli-mcp.md1.9 KB
- 参考文档references/dto-omit-for-objects.md1.1 KB
- 参考文档references/error-handling-undefined-vs-throw.md1.4 KB
- 参考文档references/lock-pessimistic-row.md1.6 KB
- 参考文档references/relations-configuration.md3.2 KB
- 参考文档references/schema-creation.md2.6 KB
- 参考文档references/select-insert-from-select.md1.4 KB
- 参考文档references/select-returning-caller-fields.md2.3 KB
- 参考文档references/select-yagni-returning.md985 B
- 参考文档references/style-conditional-aggregation.md2.5 KB
- 参考文档references/style-query-vs-select.md927 B
- 参考文档references/style-typed-sql-nullable.md1.3 KB
- 参考文档references/style-where-sql-expression.md1.2 KB
- 参考文档references/type-union-exclude.md629 B
03 SKILL.md 正文
Drizzle ORM Best Practices
Comprehensive guide for writing type-safe, high-performance database queries and services using Drizzle ORM and Drizzle Kit. Contains 14 rules across 8 categories, prioritized by impact from critical (setup, schemas, type safety) to high/medium (query selection, performance, tooling automation).
When to Apply
Reference these guidelines when:
- Defining database services and interfaces.
- Writing read/write operations with Drizzle ORM.
- Designing Data Transfer Objects (DTOs) and API boundary types.
- Deciding between Drizzle Relational Queries (
db.query) and Core SQL Queries (db.select). - Handling database query failures and non-existent records.
- Automating database migrations with Drizzle Kit CLI, SDK, or MCP server.
Rule Categories by Priority
| Priority | Category | Impact | Prefix | Rules |
|---|---|---|---|---|
| 1 | DTO & Schema Isolation | CRITICAL | dto- |
1 |
| 2 | Type Safety & Unions | CRITICAL | type- |
1 |
| 3 | Transactions & Locking | CRITICAL | lock- |
1 |
| 4 | Schema & Relations Config | CRITICAL | schema-, relations- |
2 |
| 5 | Query Selection & YAGNI | HIGH | select- |
3 |
| 6 | Query Style & Performance | HIGH | style- |
4 |
| 7 | Error Handling | MEDIUM | error- |
1 |
| 8 | Tooling & MCP Automation | HIGH | drizzle-kit- |
1 |
Quick Reference
1. DTO & Schema Isolation (CRITICAL)
- dto-omit-for-objects - Define DTOs using Omit on Object types to isolate database schemas from presentation layers.
2. Type Safety & Unions (CRITICAL)
- type-union-exclude - Use Exclude for filtering string literal union types and Omit for object types.
3. Transactions & Locking (CRITICAL)
- lock-pessimistic-row - Use pessimistic row locking in transactions to prevent concurrent write hazards.
4. Schema & Relations Config (CRITICAL)
- schema-creation - Use snakeCase.table for automatic case conversion and mix in baseEntity/fullEntity helpers.
- relations-configuration - Centralize table relations using defineRelations with explicit from, to, optional, and alias parameters.
5. Query Selection & YAGNI (HIGH)
- select-yagni-returning - Explicitly select columns mapping to DTOs and apply YAGNI to returning clauses.
- select-returning-caller-fields - Select and return only the columns callers actually access; default unused returns to id only.
- select-insert-from-select - Omit default columns and leverage column-order independence in
db.insert().select()queries.
6. Query Style & Performance (HIGH)
- style-query-vs-select - Prefer Core select queries for flat lookups and Relational queries for deeply nested relation hydration.
- style-where-sql-expression - Never pass raw SQL expressions directly to Relational Query where clauses.
- style-conditional-aggregation - Use conditional aggregation via CASE WHEN to consolidate multiple aggregate queries into a single query.
- style-typed-sql-nullable - Use type-safe
mapWith()callbacks and chain.nullable()on raw SQL expressions to preserve nullability.
7. Error Handling (MEDIUM)
- error-handling-undefined-vs-throw - Return undefined for missing records in read-only queries and throw for write/update/invariant failures.
8. Tooling & MCP Automation (HIGH)
- drizzle-kit-cli-mcp - Automate migration workflows with
--output json,--hints,@drizzle-kit/cliSDK, and stdio MCP server (drizzle-kit mcp).
How to Use
Read individual reference files for detailed guidelines, configurations, and code examples:
- dto-omit-for-objects
- type-union-exclude
- lock-pessimistic-row
- select-yagni-returning
- select-returning-caller-fields
- select-insert-from-select
- style-query-vs-select
- style-where-sql-expression
- style-conditional-aggregation
- style-typed-sql-nullable
- error-handling-undefined-vs-throw
- schema-creation
- relations-configuration
- drizzle-kit-cli-mcp
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
References
04 同仓库更多 Skills
zustand
Rules and standards for state management using Zustand in React and Next.js. Use when implementing client-side stores, persistence with localStorage, or preventing SSR hydration mismatch.
zod
Zod v4 schema validation best practices. Use when defining schemas, utilizing safeParse, or customizing error messages.
upstash-ratelimit
Rules, standards, and references for rate limiting in serverless environments using Upstash Ratelimit. Use when implementing rate limiting, configuring Upstash Redis, or handling request blocking.
ui-design
UI/UX and frontend design best practices guidelines (formerly frontend-design). This skill should be used when writing, reviewing, or designing frontend code to ensure accessibility, performance, and usability. Triggers on tasks involving HTML structure, CSS styling, responsive layouts, form design, animations, or accessibility improvements.
typescript
TypeScript performance, tsconfig, type errors, async patterns — triggered when the user asks to "optimize TypeScript performance", "speed up tsc compilation", "configure tsconfig.json", "fix type errors", "improve async patterns", or encounters TS errors (TS2322, TS2339, "is not assignable to"). Also triggers on .ts, .tsx, .d.ts file work involving type definitions, module organization, or memory management. Does NOT cover TypeScript basics, framework-specific patterns, or testing.
typescript-refactor
TypeScript and TSX refactoring and modernization guidelines from a principal specialist perspective, current to TypeScript 6.0 and React 19. This skill should be used when refactoring, reviewing, or modernizing TypeScript or React/TSX code for type safety, compiler performance, and idiomatic patterns. Triggers on tasks involving type architecture, narrowing, generics, discriminated unions, error handling, React component and hook typing, or migration to modern TypeScript features (satisfies, using, const type parameters, inferred type predicates, isolatedDeclarations, erasable syntax, import attributes).