drizzle-orm

v2026.09.24

Drizzle ORM for type-safe SQL with PostgreSQL, MySQL, and SQLite. Use when defining schemas, writing queries, managing relations, running migrations, or using drizzle-kit. Use for drizzle, orm, schema, query, migration, pgTable, relations, drizzle-kit, drizzle-zod.

GitHub
Install command
npx skhub add oakoss/drizzle-orm
Markdown
SKILL.md

Drizzle ORM

Overview

Drizzle ORM is a lightweight, type-safe TypeScript ORM that maps directly to SQL for PostgreSQL, MySQL, and SQLite. It provides both a SQL-like query builder and a relational queries API, with zero dependencies and full serverless compatibility. Use Drizzle when you need compile-time type safety with SQL-level control; avoid it when you need a full active-record ORM with automatic migrations (use Prisma) or when working with MongoDB/NoSQL databases.

Quick Reference

PatternAPIKey Points
Schema definitionpgTable('name', { columns }, (t) => [indexes])Third arg returns array of indexes/constraints
Column typestext(), integer(), boolean(), timestamp()Import from drizzle-orm/pg-core
Type inferencetypeof table.$inferSelect, $inferInsertDerive TS types directly from schema
Relational queriesdb.query.table.findMany({ with, where })Requires schema passed to drizzle() client
SQL-like queriesdb.select().from(table).where()Chainable, returns array of rows
Insertdb.insert(table).values({}).returning().returning() for getting inserted rows
Updatedb.update(table).set({}).where().returning()Always include .where() to avoid full-table updates
Deletedb.delete(table).where()Always include .where() to avoid full-table deletes
Upsert.onConflictDoUpdate({ target, set })Chain after .insert().values()
Transactionsdb.transaction(async (tx) => { ... })Auto-rollback on thrown errors
Filterseq(), and(), or(), inArray(), sql\``Import operators from drizzle-orm
Relationsrelations(table, ({ one, many }) => ({}))Declares logical relations for relational queries
Generate migrationsdrizzle-kit generateCreates SQL migration files from schema diff
Apply migrationsdrizzle-kit migrate or migrate() in codeApplies pending migrations to database
Push schemadrizzle-kit pushDirect schema push without migration files
Prepared statementsdb.select().from(t).where(eq(t.id, sql.placeholder('id'))).prepare()Reusable parameterized queries
ViewspgView('name').as(qb => ...)Regular and materialized views
$count utilitydb.$count(table, filter?)Shorthand count, usable as subquery
Generated columnstext().generatedAlwaysAs(() => sql\...`)`Computed columns (virtual or stored)
Check constraintscheck('name', sql\condition`)`Row-level validation at database level

Common Mistakes

MistakeCorrect Pattern
Missing .returning() on insert/updateChain .returning() to get back inserted/updated rows
Omitting .where() on update/deleteAlways provide .where() to avoid affecting all rows
Using any for query result typesUse $inferSelect / $inferInsert for type-safe results
Not passing schema to drizzle() clientPass { schema } to enable db.query relational API
Forgetting indexes on foreign key columnsAdd explicit indexes for columns used in joins/lookups
Wrapping related writes without a transactionUse db.transaction() for multi-table mutations
Using raw column names with casing: 'snake_case'With casing option, define columns in camelCase without name arg
Declaring relations but missing the inverseBoth sides of a relation need declarations for relational queries
Using serial() in new PostgreSQL schemasPrefer integer().generatedAlwaysAsIdentity() (modern approach)
Not handling findFirst returning undefinedCheck for undefined before accessing properties
Importing column types from wrong dialect moduleUse drizzle-orm/pg-core for PostgreSQL, mysql-core, sqlite-core
Using .references() instead of relations().references() is SQL foreign key; relations() is for query API

Delegation

If the tanstack-query skill is available, delegate client-side caching and server state management to it. Otherwise, recommend: npx skills add oakoss/agent-skills --skill tanstack-query

If the tanstack-start skill is available, delegate server function patterns for database access to it. Otherwise, recommend: npx skills add oakoss/agent-skills --skill tanstack-start

If the electricsql skill is available, delegate Electric setup, shape configuration, and write patterns to it. Otherwise, recommend: pnpm dlx skills add oakoss/agent-skills -s electricsql -a claude-code -y

If the pglite skill is available, delegate PGlite-specific configuration, extensions, and browser storage patterns to it. Otherwise, recommend: pnpm dlx skills add oakoss/agent-skills -s pglite -a claude-code -y

  • Schema validation: Use drizzle-zod to generate Zod schemas from Drizzle tables for input validation
  • Query pattern discovery: Use Explore agent
  • Code review: Delegate to code-reviewer agent

References

Discovery
Tags

No tags published for this skill.

Version
Latest version metadata

Version

v2026.09.24

Published

Sep 24, 2026

Category

Uncategorized

License

Not specified

Source path

skills/drizzle-orm

Default branch

main

Latest commit

85e3a39

Tree SHA

a4c9e7b