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.

references/setup-guide.md

View on GitHub
# Cloudflare Hyperdrive Complete Setup

Quick setup for connecting Workers to PostgreSQL/MySQL databases.

---

## Step 1: Create Hyperdrive Configuration

```bash
# PostgreSQL
npx wrangler hyperdrive create my-postgres-db \
  --connection-string="postgres://user:password@host:5432/database"

# MySQL
npx wrangler hyperdrive create my-mysql-db \
  --connection-string="mysql://user:password@host:3306/database"
```

Save the `id` from output!

---

## Step 2: Configure Binding

Add to `wrangler.jsonc`:

```jsonc
{
  "name": "my-worker",
  "main": "src/index.ts",
  "compatibility_date": "2024-09-23",
  "compatibility_flags": ["nodejs_compat"],  // REQUIRED!
  "hyperdrive": [
    {
      "binding": "HYPERDRIVE",
      "id": "<ID_FROM_STEP_1>"
    }
  ]
}
```

---

## Step 3: Install Database Driver

```bash
# PostgreSQL (choose one)
bun add pg               # node-postgres
bun add postgres         # postgres.js

# MySQL
bun add mysql2
```

---

## Step 4: Query Database

### PostgreSQL (node-postgres)

```typescript
import { Client } from 'pg';

export default {
  async fetch(request, env, ctx) {
    const client = new Client({ connectionString: env.HYPERDRIVE.connectionString });
    await client.connect();

    const result = await client.query('SELECT * FROM users LIMIT 10');
    await client.end();

    return Response.json(result.rows);
  }
};
```

### PostgreSQL (postgres.js)

```typescript
import postgres from 'postgres';

export default {
  async fetch(request, env, ctx) {
    const sql = postgres(env.HYPERDRIVE.connectionString);

    const users = await sql`SELECT * FROM users LIMIT 10`;

    return Response.json(users);
  }
};
```

### MySQL

```typescript
import mysql from 'mysql2/promise';

export default {
  async fetch(request, env, ctx) {
    const connection = await mysql.createConnection(env.HYPERDRIVE.connectionString);

    const [rows] = await connection.execute('SELECT * FROM users LIMIT 10');
    await connection.end();

    return Response.json(rows);
  }
};
```

---

## Step 5: Deploy

```bash
npx wrangler deploy
```

---

## Production Checklist

- [ ] Hyperdrive configuration created
- [ ] Binding configured in wrangler.jsonc
- [ ] nodejs_compat flag enabled
- [ ] Database driver installed
- [ ] Connection properly closed after queries
- [ ] Error handling implemented
- [ ] Query caching considered
- [ ] Connection pooling verified

---

## Official Documentation

- **Hyperdrive Overview**: https://developers.cloudflare.com/hyperdrive/
- **Get Started**: https://developers.cloudflare.com/hyperdrive/get-started/
- **Configuration**: https://developers.cloudflare.com/hyperdrive/configuration/
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