Skip to content

secondsky/claude-skills / plugins/drizzle-orm-d1/skills/drizzle-orm-d1

drizzle-orm-d1

Verified

| Type-safe ORM for Cloudflare D1 databases using Drizzle. Use when: building D1 database schemas, writing type-safe SQL queries, managing migrations with Drizzle Kit, defining table relations, implementing prepared statements, using D1 batch API, or encountering D1_ERROR, transaction errors, foreign key constraint failures, or schema inference issues.

templates/client.ts

View on GitHub
/**
 * Drizzle Client for Cloudflare D1
 *
 * This file shows how to initialize the Drizzle client with D1 in a Cloudflare Worker.
 */

import { drizzle } from 'drizzle-orm/d1';
import * as schema from './schema';

/**
 * Environment Interface
 *
 * Define your Worker's environment bindings
 */
export interface Env {
  // D1 database binding (name must match wrangler.jsonc)
  DB: D1Database;

  // Add other bindings as needed
  // KV: KVNamespace;
  // R2: R2Bucket;
}

/**
 * Create Drizzle Client
 *
 * Initialize Drizzle with your D1 database binding
 *
 * @param db - D1Database instance from env.DB
 * @returns Drizzle database client
 */
export function createDrizzleClient(db: D1Database) {
  // Option 1: Without schema (for basic queries)
  // return drizzle(db);

  // Option 2: With schema (enables relational queries)
  return drizzle(db, { schema });
}

/**
 * Usage in Worker:
 *
 * export default {
 *   async fetch(request: Request, env: Env): Promise<Response> {
 *     const db = createDrizzleClient(env.DB);
 *
 *     // Now you can use db for queries
 *     const users = await db.select().from(schema.users).all();
 *
 *     return Response.json(users);
 *   },
 * };
 */

/**
 * Type-Safe Client
 *
 * For better TypeScript inference, you can create a typed client
 */
export type DrizzleD1 = ReturnType<typeof createDrizzleClient>;

/**
 * Usage with typed client:
 *
 * async function getUsers(db: DrizzleD1) {
 *   return await db.select().from(schema.users).all();
 * }
 */

/**
 * Relational Queries
 *
 * When you pass schema to drizzle(), you get access to db.query API
 * for type-safe relational queries:
 *
 * const db = drizzle(env.DB, { schema });
 *
 * // Get user with all their posts
 * const user = await db.query.users.findFirst({
 *   where: eq(schema.users.id, 1),
 *   with: {
 *     posts: true,
 *   },
 * });
 */

/**
 * IMPORTANT: D1 Binding Name
 *
 * The binding name "DB" must match exactly between:
 *
 * 1. wrangler.jsonc:
 *    {
 *      "d1_databases": [
 *        {
 *          "binding": "DB",  // ← Must match
 *          ...
 *        }
 *      ]
 *    }
 *
 * 2. Env interface:
 *    export interface Env {
 *      DB: D1Database;  // ← Must match
 *    }
 *
 * 3. Worker code:
 *    const db = drizzle(env.DB);  // ← Must match
 */
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall