Get the FREE Ultimate OpenClaw Setup Guide →

postgres-drizzle

npx machina-cli add skill ccheney/robust-skills/postgres-drizzle --openclaw
Files (1)
SKILL.md
6.1 KB

PostgreSQL + Drizzle ORM

Type-safe database applications with PostgreSQL 18 and Drizzle ORM.

Essential Commands

npx drizzle-kit generate   # Generate migration from schema changes
npx drizzle-kit migrate    # Apply pending migrations
npx drizzle-kit push       # Push schema directly (dev only!)
npx drizzle-kit studio     # Open database browser

Quick Decision Trees

"How do I model this relationship?"

Relationship type?
├─ One-to-many (user has posts)     → FK on "many" side + relations()
├─ Many-to-many (posts have tags)   → Junction table + relations()
├─ One-to-one (user has profile)    → FK with unique constraint
└─ Self-referential (comments)      → FK to same table

"Why is my query slow?"

Slow query?
├─ Missing index on WHERE/JOIN columns  → Add index
├─ N+1 queries in loop                  → Use relational queries API
├─ Full table scan                      → EXPLAIN ANALYZE, add index
├─ Large result set                     → Add pagination (limit/offset)
└─ Connection overhead                  → Enable connection pooling

"Which drizzle-kit command?"

What do I need?
├─ Schema changed, need SQL migration   → drizzle-kit generate
├─ Apply migrations to database         → drizzle-kit migrate
├─ Quick dev iteration (no migration)   → drizzle-kit push
└─ Browse/edit data visually            → drizzle-kit studio

Directory Structure

src/db/
├── schema/
│   ├── index.ts          # Re-export all tables
│   ├── users.ts          # Table + relations
│   └── posts.ts          # Table + relations
├── db.ts                 # Connection with pooling
└── migrate.ts            # Migration runner
drizzle/
└── migrations/           # Generated SQL files
drizzle.config.ts         # drizzle-kit config

Schema Patterns

Basic Table with Timestamps

export const users = pgTable('users', {
  id: uuid('id').primaryKey().defaultRandom(),
  email: varchar('email', { length: 255 }).notNull().unique(),
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
});

Foreign Key with Index

export const posts = pgTable('posts', {
  id: uuid('id').primaryKey().defaultRandom(),
  userId: uuid('user_id').notNull().references(() => users.id),
  title: varchar('title', { length: 255 }).notNull(),
}, (table) => [
  index('posts_user_id_idx').on(table.userId), // ALWAYS index FKs
]);

Relations

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

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

Query Patterns

Relational Query (Avoid N+1)

// ✓ Single query with nested data
const usersWithPosts = await db.query.users.findMany({
  with: { posts: true },
});

Filtered Query

const activeUsers = await db
  .select()
  .from(users)
  .where(eq(users.status, 'active'));

Transaction

await db.transaction(async (tx) => {
  const [user] = await tx.insert(users).values({ email }).returning();
  await tx.insert(profiles).values({ userId: user.id });
});

Performance Checklist

PriorityCheckImpact
CRITICALIndex all foreign keysPrevents full table scans on JOINs
CRITICALUse relational queries for nested dataAvoids N+1
HIGHConnection pooling in productionReduces connection overhead
HIGHEXPLAIN ANALYZE slow queriesIdentifies missing indexes
MEDIUMPartial indexes for filtered subsetsSmaller, faster indexes
MEDIUMUUIDv7 for PKs (PG18+)Better index locality

Anti-Patterns (CRITICAL)

Anti-PatternProblemFix
No FK indexSlow JOINs, full scansAdd index on every FK column
N+1 in loopsQuery per rowUse with: relational queries
No poolingConnection per requestUse @neondatabase/serverless or similar
push in prodData loss riskAlways use generate + migrate
Storing JSON as textNo validation, bad queriesUse jsonb() column type

Reference Documentation

FilePurpose
references/SCHEMA.mdColumn types, constraints
references/QUERIES.mdOperators, joins, aggregations
references/RELATIONS.mdOne-to-many, many-to-many
references/MIGRATIONS.mddrizzle-kit workflows
references/POSTGRES.mdPG18 features, RLS, partitioning
references/PERFORMANCE.mdIndexing, optimization
references/CHEATSHEET.mdQuick reference

Resources

Drizzle ORM

PostgreSQL

Source

git clone https://github.com/ccheney/robust-skills/blob/main/skills/postgres-drizzle/SKILL.mdView on GitHub

Overview

Build type-safe database applications using PostgreSQL 18 and Drizzle ORM. This skill guides schema design, migrations, and queries with best practices for relations, joins, and performance, using drizzle-kit tooling. It emphasizes robust Postgres + Drizzle workflows for APIs, backends, and data models.

How This Skill Works

Define your schema with pgTable and relations in src/db/schema, including foreign keys and indexes. Use drizzle-kit to generate and apply migrations, then query via Drizzle ORM to fetch relational data in a single, type-safe operation. Manage database connections with pooling and iterate quickly with drizzle-kit studio for visual data exploration.

When to Use It

  • Designing a new API backend or service that uses PostgreSQL with Drizzle ORM
  • Modeling relationships (one-to-many, many-to-many, one-to-one, self-referential) in the schema
  • Writing or evolving schemas, migrations, or SQL with a focus on type-safety and ORM patterns
  • Optimizing queries, debugging performance issues, and preventing N+1 problems with relational queries
  • Prototyping and exploring data visually using drizzle-kit studio during development

Quick Start

  1. Step 1: npx drizzle-kit generate # Generate migration from schema changes
  2. Step 2: npx drizzle-kit migrate # Apply pending migrations
  3. Step 3: npx drizzle-kit studio # Open database browser for visual data exploration

Best Practices

  • Index all foreign keys to prevent full table scans on JOINs
  • Use the relational queries API to fetch nested data and avoid N+1 queries
  • Add indexes on WHERE and JOIN columns; use EXPLAIN ANALYZE to verify performance
  • Define clear relations with the relations() API and keep schema references explicit
  • Leverage drizzle-kit for migrations and drizzle-kit studio for data exploration; enable connection pooling in production

Example Use Cases

  • Model users and posts with a foreign key and an index to speed up lookups
  • Implement a many-to-many relationship (posts <-> tags) using a junction table and relations
  • Fetch a user with their posts in a single query using with: { posts: true }
  • Execute a transactional flow: create a user and a profile atomically
  • Iterate schema changes with drizzle-kit generate/migrate and explore data with drizzle-kit studio

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers