mcpField
D
drizzle-best-practices

drizzle-best-practices

github.com/honra-ioData & databases22694 installsUpdated 22w ago
Repository

About drizzle-best-practices

A best-practices skill for developers using Drizzle ORM with PostgreSQL, covering schemas, queries, migrations, types, and performance. It highlights API differences between Drizzle versions to guide code choices.

Key features

  1. 01API guidance by Drizzle version
  2. 02Schema and query patterns
  3. 03Relations and migration guidance
  4. 04TypeScript type-safety advice
  5. 05Performance and database-driver guidance

Use cases

  1. 01Write PostgreSQL schemas and CRUD queries
  2. 02Choose syntax for Drizzle v1 or legacy APIs
  3. 03Configure drizzle-kit migrations
  4. 04Select practices for queries and database drivers

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/honra-io/drizzle-best-practices --skill drizzle-best-practices

After installing, type/drizzle-best-practicesin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

.gitignore
AGENTS.md
CLAUDE.md
README.md
LICENSE
evals/
evals.json
evals/fixtures/eval-2/
package.json
references/
_sections.md
advanced-dynamic-queries.md
advanced-sql-operator.md
migrations-config.md
perf-prepared-statements.md
query-filters-operators.md
driver-serverless.md
driver-postgres.md
migrations-workflow.md
perf-batch-operations.md
query-error-handling.md
engine-postgres.md
query-mutations.md
schema-column-types.md
schema-table-definitions.md
schema-indexes-constraints.md
types-custom-types.md
types-inference.md
query-select-patterns.md
relations-defining.md
types-validators.md
relations-querying.md

Details

From the project README

Drizzle ORM Best Practices (PostgreSQL)

Comprehensive best practices guide for Drizzle ORM with PostgreSQL. Contains guidance across 8 categories, prioritized by impact to help you write correct, performant, and maintainable database code.

Version & API Detection (read this first)

Drizzle is mid-transition to v1, and the relations + relational-query APIs differ between the two major lines. Check the project's package.json before writing code so you emit the right syntax:

Installed drizzle-ormAPI to useInstall command
^1.0.0-rc or ^1.0.0-betav1 — the default in this skill. defineRelations, object-syntax relational queries (where: { … }, orderBy: { … }), validators from drizzle-orm/zodnpm i drizzle-orm@rc + npm i -D drizzle-kit@rc
^0.4x (e.g. 0.45.2)Legacy. relations() helper, callback/operator relational queries, validators from the separate drizzle-zod packagenpm i drizzle-orm (resolves to 0.45.2)

The current release candidate is [email protected] (the @rc tag), which supersedes the older @beta tag the docs still reference in places.

Three things that routinely trip up agents:

  • @latest is still 0.45.2, not v1. npm i drizzle-orm does not get you v1 — a new project that wants the v1 API must ask for @rc explicitly.
  • The SQL-like query builder is unchanged across versions. db.select().from(t).where(eq(t.id, 1)), db.insert(), db.update(), db.delete() work identically in 0.45.x and v1. Only the relational query API (db.query) and relation definitions changed. Never rewrite db.select() operator filters into the relational object syntax.
  • On v1, the relational query API (db.query) is driven by defineRelations. Passing a legacy schema does not populate db.query. The old relations() helper is still importable from drizzle-orm/_relations so existing relation modules keep compiling during a port, but to query relationally on v1 you convert them to defineRelations. (Earlier betas exposed a db._query path; it was removed before 1.0.0-rc.3.) See references/relations-defining.md.

When the version is unstated, default to v1 (RC) for new code, but match the existing codebase's API if you already see legacy relations() / callback-style db.query patterns in use. Throughout the reference files, version-specific differences are marked with "Legacy (v0.45.x)" callouts.

When to Apply

Reference these guidelines when:

  • Defining table schemas with pgTable
  • Writing select, insert, update, or delete queries
  • Setting up relations between tables using defineRelations or the legacy relations API
  • Configuring drizzle-kit for migrations (generate, push, pull)
  • Inferring TypeScript types from your schema
  • Choosing between the SQL-like API and the relational query API
  • Optimizing query performance with prepared statements or batch operations
  • Integrating Drizzle with serverless Postgres providers (Neon, Supabase, etc.)

Rule Categories by Priority

PriorityCategoryImpactPrefix
1Schema DesignCRITICALschema-
2Query PatternsCRITICALquery-
3RelationsHIGHrelations-
4MigrationsHIGHmigrations-
5Type SafetyMEDIUM-HIGHtypes-
6PerformanceMEDIUMperf-
7Database DriversMEDIUMdriver-
8Advanced PatternsLOWadvanced-

How to Use

Read individual reference files for detailed explanations and code examples:

references/engine-postgres.md          # Postgres-specific types, features, and patterns
references/schema-table-definitions.md
references/query-select-patterns.md
references/relations-defining.md
references/_sections.md                # Full index of all references

Each reference file contains:

  • Brief explanation of why it matters
  • Incorrect code example with explanation
  • Correct code example with explanation
  • Links to official Drizzle documentation

References

Comments

Markdown supported