$npx -y skills add tranhieutt/software_development_department --skill drizzle-orm-expertProvides Drizzle ORM schema design, query patterns, migrations, and TypeScript integration for SQL databases. Use when working with Drizzle files (schema.ts, drizzle.config.ts) or when the user mentions Drizzle ORM or drizzle-kit.
| 1 | # Drizzle ORM Expert |
| 2 | |
| 3 | You are a production-grade Drizzle ORM expert. You help developers build type-safe, performant database layers using Drizzle ORM with TypeScript. You know schema design, the relational query API, Drizzle Kit migrations, and integrations with Next.js, tRPC, and serverless databases (Neon, PlanetScale, Turso, Supabase). |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Use when the user asks to set up Drizzle ORM in a new or existing project |
| 8 | - Use when designing database schemas with Drizzle's TypeScript-first approach |
| 9 | - Use when writing complex relational queries (joins, subqueries, aggregations) |
| 10 | - Use when setting up or troubleshooting Drizzle Kit migrations |
| 11 | - Use when integrating Drizzle with Next.js App Router, tRPC, or Hono |
| 12 | - Use when optimizing database performance (prepared statements, batching, connection pooling) |
| 13 | - Use when migrating from Prisma, TypeORM, or Knex to Drizzle |
| 14 | |
| 15 | ## Core Concepts |
| 16 | |
| 17 | ### Why Drizzle |
| 18 | |
| 19 | Drizzle ORM is a TypeScript-first ORM that generates zero runtime overhead. Unlike Prisma (which uses a query engine binary), Drizzle compiles to raw SQL — making it ideal for edge runtimes and serverless. Key advantages: |
| 20 | |
| 21 | - **SQL-like API**: If you know SQL, you know Drizzle |
| 22 | - **Zero dependencies**: Tiny bundle, works in Cloudflare Workers, Vercel Edge, Deno |
| 23 | - **Full type inference**: Schema → types → queries are all connected at compile time |
| 24 | - **Relational Query API**: Prisma-like nested includes without N+1 problems |
| 25 | |
| 26 | ## Schema Design Patterns |
| 27 | |
| 28 | ### Table Definitions |
| 29 | |
| 30 | ```typescript |
| 31 | // db/schema.ts |
| 32 | import { pgTable, text, integer, timestamp, boolean, uuid, pgEnum } from "drizzle-orm/pg-core"; |
| 33 | import { relations } from "drizzle-orm"; |
| 34 | |
| 35 | // Enums |
| 36 | export const roleEnum = pgEnum("role", ["admin", "user", "moderator"]); |
| 37 | |
| 38 | // Users table |
| 39 | export const users = pgTable("users", { |
| 40 | id: uuid("id").defaultRandom().primaryKey(), |
| 41 | email: text("email").notNull().unique(), |
| 42 | name: text("name").notNull(), |
| 43 | role: roleEnum("role").default("user").notNull(), |
| 44 | createdAt: timestamp("created_at").defaultNow().notNull(), |
| 45 | updatedAt: timestamp("updated_at").defaultNow().notNull(), |
| 46 | }); |
| 47 | |
| 48 | // Posts table with foreign key |
| 49 | export const posts = pgTable("posts", { |
| 50 | id: uuid("id").defaultRandom().primaryKey(), |
| 51 | title: text("title").notNull(), |
| 52 | content: text("content"), |
| 53 | published: boolean("published").default(false).notNull(), |
| 54 | authorId: uuid("author_id").references(() => users.id, { onDelete: "cascade" }).notNull(), |
| 55 | createdAt: timestamp("created_at").defaultNow().notNull(), |
| 56 | }); |
| 57 | ``` |
| 58 | |
| 59 | ### Relations |
| 60 | |
| 61 | ```typescript |
| 62 | // db/relations.ts |
| 63 | export const usersRelations = relations(users, ({ many }) => ({ |
| 64 | posts: many(posts), |
| 65 | })); |
| 66 | |
| 67 | export const postsRelations = relations(posts, ({ one }) => ({ |
| 68 | author: one(users, { |
| 69 | fields: [posts.authorId], |
| 70 | references: [users.id], |
| 71 | }), |
| 72 | })); |
| 73 | ``` |
| 74 | |
| 75 | ### Type Inference |
| 76 | |
| 77 | ```typescript |
| 78 | // Infer types directly from your schema — no separate type files needed |
| 79 | import type { InferSelectModel, InferInsertModel } from "drizzle-orm"; |
| 80 | |
| 81 | export type User = InferSelectModel<typeof users>; |
| 82 | export type NewUser = InferInsertModel<typeof users>; |
| 83 | export type Post = InferSelectModel<typeof posts>; |
| 84 | export type NewPost = InferInsertModel<typeof posts>; |
| 85 | ``` |
| 86 | |
| 87 | ## Query Patterns |
| 88 | |
| 89 | ### Select Queries (SQL-like API) |
| 90 | |
| 91 | ```typescript |
| 92 | import { eq, and, like, desc, count, sql } from "drizzle-orm"; |
| 93 | |
| 94 | // Basic select |
| 95 | const allUsers = await db.select().from(users); |
| 96 | |
| 97 | // Filtered with conditions |
| 98 | const admins = await db.select().from(users).where(eq(users.role, "admin")); |
| 99 | |
| 100 | // Partial select (only specific columns) |
| 101 | const emails = await db.select({ email: users.email }).from(users); |
| 102 | |
| 103 | // Join query |
| 104 | const postsWithAuthors = await db |
| 105 | .select({ |
| 106 | title: posts.title, |
| 107 | authorName: users.name, |
| 108 | }) |
| 109 | .from(posts) |
| 110 | .innerJoin(users, eq(posts.authorId, users.id)) |
| 111 | .where(eq(posts.published, true)) |
| 112 | .orderBy(desc(posts.createdAt)) |
| 113 | .limit(10); |
| 114 | |
| 115 | // Aggregation |
| 116 | const postCounts = await db |
| 117 | .select({ |
| 118 | authorId: posts.authorId, |
| 119 | postCount: count(posts.id), |
| 120 | }) |
| 121 | .from(posts) |
| 122 | .groupBy(posts.authorId); |
| 123 | ``` |
| 124 | |
| 125 | ### Relational Queries (Prisma-like API) |
| 126 | |
| 127 | ```typescript |
| 128 | // Nested includes — Drizzle resolves in a single query |
| 129 | const usersWithPosts = await db.query.users.findMany({ |
| 130 | with: { |
| 131 | posts: { |
| 132 | where: eq(posts.published, true), |
| 133 | orderBy: [desc(posts.createdAt)], |
| 134 | limit: 5, |
| 135 | }, |
| 136 | }, |
| 137 | }); |
| 138 | |
| 139 | // Find one with nested data |
| 140 | const user = await db.query.users.findFirst({ |
| 141 | where: eq(users.id |