Command Palette

Search for a command to run...

YepAPI
Free · All Tools

Drizzle ORM Agent Skill

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

databasedrizzleormtypescript

The Skill

Full content, every format. Copy it, download it, or install with one command.

SKILL.md
---
description: Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.
homepage: https://yepapi.com/skills/drizzle-orm
metadata:
  tags: [database, drizzle, orm, typescript]
---

# Drizzle ORM

## Rules

- Schema definition: `pgTable()` / `mysqlTable()` / `sqliteTable()` in `src/db/schema.ts`
- Development: `drizzle-kit push` for fast iteration — no migration files needed
- Production: `drizzle-kit generate` + `drizzle-kit migrate` for trackable migration history
- Type-safe queries: `db.select().from(users).where(eq(users.email, email))` — full TypeScript inference
- Relations: define with `relations()` helper — `one()` and `many()` for relational queries with `db.query`
- Prepared statements: `db.select().from(users).where(eq(users.id, sql.placeholder("id"))).prepare()` — reuse for hot paths
- Transactions: `db.transaction(async (tx) => { ... })` — use `tx` not `db` inside the callback
- Indexes: define inline with `.index()` or in table config — add on columns used in `where` and `orderBy`
- Drizzle Studio: `npx drizzle-kit studio` for visual database browser during development

## Patterns

\`\`\`ts
// schema.ts
import { pgTable, text, timestamp, uuid, integer } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";

export const users = pgTable("users", {
  id: uuid("id").primaryKey().defaultRandom(),
  name: text("name").notNull(),
  email: text("email").notNull().unique(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
});

export const posts = pgTable("posts", {
  id: uuid("id").primaryKey().defaultRandom(),
  title: text("title").notNull(),
  authorId: uuid("author_id").references(() => users.id).notNull(),
});

export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
}));

export const postsRelations = relations(posts, ({ one }) => ({
  author: one(users, { fields: [posts.authorId], references: [users.id] }),
}));

// Query with relations
const result = await db.query.users.findFirst({
  where: eq(users.id, userId),
  with: { posts: true },
});
\`\`\`

## Avoid

- `drizzle-kit push` in production — use `generate` + `migrate` for tracked migrations
- Raw SQL when Drizzle API covers it — use `sql` template tag only for complex expressions
- Forgetting `relations()` definitions — without them, `db.query` relational queries won't work
- N+1 queries — use `with` in `db.query` to join relations in a single query
- Missing indexes on foreign keys and filtered columns — Drizzle doesn't auto-create them

Install

Why Use the Drizzle ORM Skill?

Without this skill, your AI guesses at drizzle orm patterns. It might hallucinate deprecated APIs, use outdated conventions, or miss best practices entirely. With it, your AI follows a proven ruleset — every suggestion aligns with current standards.

Drop this skill into your project and your AI instantly knows the rules. Better code suggestions, fewer errors, faster shipping.

Try These Prompts

These prompts work better with the Drizzle ORM skill installed. Your AI knows the context and writes code that fits.

"Set up a Drizzle ORM schema with users, posts, and relations"

"Create type-safe queries with Drizzle including joins and transactions"

"Configure drizzle-kit for development push and production migrations"

Drizzle ORM skill — FAQ

It provides rules for Drizzle schema definition, push vs migrate workflows, type-safe queries with full TypeScript inference, relations, transactions, and prepared statements. Your AI writes correct Drizzle code from the start.

Run `npx skills add YepAPI/skills --skill drizzle-orm` in your project root. This copies the skill file into your repo where your AI coding tool can read it automatically.

Use push for fast development iteration without migration files. Use generate + migrate for production deployments where you need trackable, reviewable migration history. The skill covers both workflows.

Want more skills?

Browse all 110 free skills for builders.

See All Skills