Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/hyperdrive/README.md
1# Hyperdrive23Accelerates database queries from Workers via connection pooling, edge setup, query caching.45## Key Features67- **Connection Pooling**: Persistent connections eliminate TCP/TLS/auth handshakes (~7 round-trips)8- **Edge Setup**: Connection negotiation at edge, pooling near origin9- **Query Caching**: Auto-cache non-mutating queries (default 60s TTL)10- **Support**: PostgreSQL, MySQL + compatibles (CockroachDB, Timescale, PlanetScale, Neon, Supabase)1112## Architecture1314```15Worker → Edge (setup) → Pool (near DB) → Origin16↓ cached reads17Cache18```1920## Quick Start2122```bash23# Create config24npx wrangler hyperdrive create my-db \25--connection-string="postgres://user:pass@host:5432/db"2627# wrangler.jsonc28{29"compatibility_flags": ["nodejs_compat"],30"hyperdrive": [{"binding": "HYPERDRIVE", "id": "<ID>"}]31}32```3334```typescript35import { Client } from "pg";3637export default {38async fetch(req: Request, env: Env): Promise<Response> {39const client = new Client({40connectionString: env.HYPERDRIVE.connectionString,41});42await client.connect();43const result = await client.query("SELECT * FROM users WHERE id = $1", [123]);44await client.end();45return Response.json(result.rows);46},47};48```4950## When to Use5152✅ Global access to single-region DBs, high read ratios, popular queries, connection-heavy loads53❌ Write-heavy, real-time data (<1s), single-region apps close to DB5455**💡 Pair with Smart Placement** for Workers making multiple queries - executes near DB to minimize latency.5657## Driver Choice5859| Driver | Use When | Notes |60|--------|----------|-------|61| **pg** (recommended) | General use, TypeScript, ecosystem compatibility | Stable, widely used, works with most ORMs |62| **postgres.js** | Advanced features, template literals, streaming | Lighter than pg, `prepare: true` is default |63| **mysql2** | MySQL/MariaDB/PlanetScale | MySQL only, less mature support |6465## Reading Order6667| New to Hyperdrive | Implementing | Troubleshooting |68|-------------------|--------------|-----------------|69| 1. README (this) | 1. [configuration.md](./configuration.md) | 1. [gotchas.md](./gotchas.md) |70| 2. [configuration.md](./configuration.md) | 2. [api.md](./api.md) | 2. [patterns.md](./patterns.md) |71| 3. [api.md](./api.md) | 3. [patterns.md](./patterns.md) | 3. [api.md](./api.md) |7273## In This Reference74- [configuration.md](./configuration.md) - Setup, wrangler config, Smart Placement75- [api.md](./api.md) - Binding APIs, query patterns, driver usage76- [patterns.md](./patterns.md) - Use cases, ORMs, multi-query optimization77- [gotchas.md](./gotchas.md) - Limits, troubleshooting, connection management7879## See Also80- [smart-placement](../smart-placement/) - Optimize multi-query Workers near databases81- [d1](../d1/) - Serverless SQLite alternative for edge-native apps82- [workers](../workers/) - Worker runtime with database bindings83