Skip to content

secondsky/claude-skills / plugins/cloudflare-hyperdrive/skills/cloudflare-hyperdrive

cloudflare-hyperdrive

Verified

Cloudflare Hyperdrive for Workers-to-database connections with pooling and caching. Use for PostgreSQL/MySQL, Drizzle/Prisma, or encountering pool errors, TLS issues, connection refused.

templates/drizzle-postgres.ts

View on GitHub
/**
 * Drizzle ORM with PostgreSQL
 *
 * Type-safe ORM for PostgreSQL via Hyperdrive.
 *
 * Install: npm install drizzle-orm postgres
 * Install (dev): npm install -D drizzle-kit
 *
 * Minimum version: postgres@3.4.5
 */

import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { pgTable, serial, varchar, timestamp } from "drizzle-orm/pg-core";
import { eq } from "drizzle-orm";

// Define schema
export const users = pgTable("users", {
  id: serial("id").primaryKey(),
  name: varchar("name", { length: 255 }).notNull(),
  email: varchar("email", { length: 255 }).notNull().unique(),
  createdAt: timestamp("created_at").defaultNow(),
});

type Bindings = {
  HYPERDRIVE: Hyperdrive;
};

export default {
  async fetch(
    request: Request,
    env: Bindings,
    ctx: ExecutionContext
  ): Promise<Response> {
    // Create postgres.js connection
    const sql = postgres(env.HYPERDRIVE.connectionString, {
      max: 5,
      prepare: true,
      fetch_types: false
    });

    // Create Drizzle client
    const db = drizzle(sql);

    try {
      // Example: Select all users
      const allUsers = await db.select().from(users);

      // Example: Select with where clause
      const recentUsers = await db
        .select()
        .from(users)
        .where(eq(users.createdAt, new Date('2024-01-01')));

      // Example: Insert
      const [newUser] = await db
        .insert(users)
        .values({
          name: "John Doe",
          email: `john.${Date.now()}@example.com`
        })
        .returning();

      // Example: Update
      await db
        .update(users)
        .set({ name: "Jane Doe" })
        .where(eq(users.id, newUser.id));

      // Example: Delete
      // await db.delete(users).where(eq(users.id, 123));

      return Response.json({
        success: true,
        data: {
          allUsers,
          recentUsers,
          newUser
        }
      });

    } catch (error: any) {
      console.error("Database error:", error);

      return Response.json({
        success: false,
        error: error.message
      }, {
        status: 500
      });

    } finally {
      ctx.waitUntil(sql.end());
    }
  }
};
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