$npx -y skills add jgamaraalv/ts-dev-kit --skill drizzle-pgDrizzle ORM reference for PostgreSQL — schema definition, typesafe queries, relations, and migrations with drizzle-kit. Use when: (1) defining pgTable schemas with column types, indexes, constraints, or enums, (2) writing select/insert/update/delete queries or joins, (3) defining
| 1 | # Drizzle ORM — PostgreSQL |
| 2 | |
| 3 | Drizzle is a headless TypeScript ORM. Zero dependencies, SQL-like API, single-query output. |
| 4 | Packages: `drizzle-orm` (runtime), `drizzle-kit` (CLI/migrations). |
| 5 | |
| 6 | ## Table of Contents |
| 7 | |
| 8 | - [Quick Start](#quick-start) |
| 9 | - [Import Cheat Sheet](#import-cheat-sheet) |
| 10 | - [Common Patterns](#common-patterns) |
| 11 | - [Reference Files](#reference-files) |
| 12 | |
| 13 | <examples> |
| 14 | |
| 15 | ## Quick Start |
| 16 | |
| 17 | ### Connect |
| 18 | |
| 19 | ```typescript |
| 20 | import { drizzle } from "drizzle-orm/node-postgres"; |
| 21 | import * as schema from "./schema"; |
| 22 | import { relations } from "./relations"; |
| 23 | |
| 24 | const db = drizzle(process.env.DATABASE_URL, { schema, relations }); |
| 25 | ``` |
| 26 | |
| 27 | Or with existing Pool: |
| 28 | |
| 29 | ```typescript |
| 30 | import { Pool } from "pg"; |
| 31 | const pool = new Pool({ connectionString: process.env.DATABASE_URL }); |
| 32 | const db = drizzle({ client: pool, schema, relations }); |
| 33 | ``` |
| 34 | |
| 35 | ### Define Schema |
| 36 | |
| 37 | ```typescript |
| 38 | import { |
| 39 | pgTable, |
| 40 | pgEnum, |
| 41 | serial, |
| 42 | text, |
| 43 | integer, |
| 44 | timestamp, |
| 45 | uuid, |
| 46 | jsonb, |
| 47 | index, |
| 48 | uniqueIndex, |
| 49 | } from "drizzle-orm/pg-core"; |
| 50 | import { sql } from "drizzle-orm"; |
| 51 | |
| 52 | export const statusEnum = pgEnum("status", ["active", "inactive", "banned"]); |
| 53 | |
| 54 | export const users = pgTable( |
| 55 | "users", |
| 56 | { |
| 57 | id: uuid("id") |
| 58 | .default(sql`gen_random_uuid()`) |
| 59 | .primaryKey(), |
| 60 | name: text("name").notNull(), |
| 61 | email: text("email").notNull().unique(), |
| 62 | status: statusEnum().default("active").notNull(), |
| 63 | metadata: jsonb("metadata").$type<{ roles: string[] }>(), |
| 64 | createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(), |
| 65 | }, |
| 66 | (t) => [index("users_email_idx").on(t.email)], |
| 67 | ); |
| 68 | |
| 69 | export const posts = pgTable("posts", { |
| 70 | id: serial("id").primaryKey(), |
| 71 | title: text("title").notNull(), |
| 72 | authorId: uuid("author_id") |
| 73 | .notNull() |
| 74 | .references(() => users.id, { onDelete: "cascade" }), |
| 75 | createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(), |
| 76 | }); |
| 77 | ``` |
| 78 | |
| 79 | ### Define Relations |
| 80 | |
| 81 | ```typescript |
| 82 | import { defineRelations } from "drizzle-orm"; |
| 83 | import * as schema from "./schema"; |
| 84 | |
| 85 | export const relations = defineRelations(schema, (r) => ({ |
| 86 | users: { |
| 87 | posts: r.many.posts({ from: r.users.id, to: r.posts.authorId }), |
| 88 | }, |
| 89 | posts: { |
| 90 | author: r.one.users({ from: r.posts.authorId, to: r.users.id }), |
| 91 | }, |
| 92 | })); |
| 93 | ``` |
| 94 | |
| 95 | ### CRUD |
| 96 | |
| 97 | ```typescript |
| 98 | import { eq, and, ilike, sql } from "drizzle-orm"; |
| 99 | |
| 100 | // SELECT |
| 101 | const allUsers = await db.select().from(users); |
| 102 | const user = await db.select().from(users).where(eq(users.id, id)); |
| 103 | |
| 104 | // INSERT |
| 105 | const [created] = await db |
| 106 | .insert(users) |
| 107 | .values({ name: "Dan", email: "dan@example.com" }) |
| 108 | .returning(); |
| 109 | |
| 110 | // UPDATE |
| 111 | await db.update(users).set({ name: "Updated" }).where(eq(users.id, id)); |
| 112 | |
| 113 | // DELETE |
| 114 | await db.delete(users).where(eq(users.id, id)); |
| 115 | |
| 116 | // UPSERT |
| 117 | await db |
| 118 | .insert(users) |
| 119 | .values({ id, name: "Dan", email: "dan@ex.com" }) |
| 120 | .onConflictDoUpdate({ target: users.id, set: { name: "Dan" } }); |
| 121 | ``` |
| 122 | |
| 123 | ### Relational Queries |
| 124 | |
| 125 | ```typescript |
| 126 | // Nested eager loading (single SQL query) |
| 127 | const usersWithPosts = await db.query.users.findMany({ |
| 128 | with: { posts: true }, |
| 129 | where: { status: "active" }, |
| 130 | orderBy: { createdAt: "desc" }, |
| 131 | limit: 10, |
| 132 | }); |
| 133 | |
| 134 | const user = await db.query.users.findFirst({ |
| 135 | where: { id: userId }, |
| 136 | with: { posts: { columns: { id: true, title: true } } }, |
| 137 | }); |
| 138 | ``` |
| 139 | |
| 140 | ### Migrations |
| 141 | |
| 142 | ```bash |
| 143 | # drizzle.config.ts -> see references/migrations.md |
| 144 | npx drizzle-kit generate # schema diff -> SQL files |
| 145 | npx drizzle-kit migrate # apply SQL to database |
| 146 | npx drizzle-kit push # direct push (no SQL files) |
| 147 | npx drizzle-kit pull # introspect DB -> Drizzle schema |
| 148 | npx drizzle-kit studio # visual browser UI |
| 149 | ``` |
| 150 | |
| 151 | ## Common Patterns |
| 152 | |
| 153 | ### Conditional filters |
| 154 | |
| 155 | ```typescript |
| 156 | const filters: SQL[] = []; |
| 157 | if (name) filters.push(ilike(users.name, `%${name}%`)); |
| 158 | if (status) filters.push(eq(users.status, status)); |
| 159 | await db |
| 160 | .select() |
| 161 | .from(users) |
| 162 | .where(and(...filters)); |
| 163 | ``` |
| 164 | |
| 165 | ### Transactions |
| 166 | |
| 167 | ```typescript |
| 168 | await db.transaction(async (tx) => { |
| 169 | const [user] = await tx.insert(users).values({ name: "Dan" }).returning(); |
| 170 | await tx.insert(posts).values({ title: "Hello", authorId: user.id }); |
| 171 | }); |
| 172 | ``` |
| 173 | |
| 174 | ### Type inference |
| 175 | |
| 176 | ```typescript |
| 177 | type User = typeof users.$inferSelect; |
| 178 | type NewUser = typeof users.$inferInsert; |
| 179 | ``` |
| 180 | |
| 181 | </examples> |
| 182 | |
| 183 | <quick_reference> |
| 184 | |
| 185 | ## Import Cheat Sheet |
| 186 | |
| 187 | | Import path | Key exports |