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/api.md
1# API Reference23See [README.md](./README.md) for overview, [configuration.md](./configuration.md) for setup.45## Binding Interface67```typescript8interface Hyperdrive {9connectionString: string; // PostgreSQL10// MySQL properties:11host: string;12port: number;13user: string;14password: string;15database: string;16}1718interface Env {19HYPERDRIVE: Hyperdrive;20}21```2223**Generate types:** `npx wrangler types` (auto-creates worker-configuration.d.ts from wrangler.jsonc)2425## PostgreSQL (node-postgres) - RECOMMENDED2627```typescript28import { Client } from "pg"; // pg@^8.17.22930export default {31async fetch(req: Request, env: Env): Promise<Response> {32const client = new Client({connectionString: env.HYPERDRIVE.connectionString});33try {34await client.connect();35const result = await client.query("SELECT * FROM users WHERE id = $1", [123]);36return Response.json(result.rows);37} finally {38await client.end();39}40},41};42```4344**⚠️ Workers connection limit: 6 per Worker invocation** - use connection pooling wisely.4546## PostgreSQL (postgres.js)4748```typescript49import postgres from "postgres"; // postgres@^3.4.85051const sql = postgres(env.HYPERDRIVE.connectionString, {52max: 5, // Limit per Worker (Workers max: 6)53prepare: true, // Enabled by default, required for caching54fetch_types: false, // Reduce latency if not using arrays55});5657const users = await sql`SELECT * FROM users WHERE active = ${true} LIMIT 10`;58```5960**⚠️ `prepare: true` is enabled by default and required for Hyperdrive caching.** Setting to `false` disables prepared statements + cache.6162## MySQL (mysql2)6364```typescript65import { createConnection } from "mysql2/promise"; // mysql2@^3.16.26667const conn = await createConnection({68host: env.HYPERDRIVE.host,69user: env.HYPERDRIVE.user,70password: env.HYPERDRIVE.password,71database: env.HYPERDRIVE.database,72port: env.HYPERDRIVE.port,73disableEval: true, // ⚠️ REQUIRED for Workers74});7576const [results] = await conn.query("SELECT * FROM users WHERE active = ? LIMIT ?", [true, 10]);77ctx.waitUntil(conn.end());78```7980**⚠️ MySQL support is less mature than PostgreSQL** - expect fewer optimizations and potential edge cases.8182## Query Caching8384**Cacheable:**85```sql86SELECT * FROM posts WHERE published = true;87SELECT COUNT(*) FROM users;88```8990**NOT cacheable:**91```sql92-- Writes93INSERT/UPDATE/DELETE9495-- Volatile functions96SELECT NOW();97SELECT random();98SELECT LASTVAL(); -- PostgreSQL99SELECT UUID(); -- MySQL100```101102**Cache config:**103- Default: `max_age=60s`, `swr=15s`104- Max `max_age`: 3600s105- Disable: `--caching-disabled=true`106107**Multiple configs pattern:**108```typescript109// Reads: cached110const sqlCached = postgres(env.HYPERDRIVE_CACHED.connectionString);111const posts = await sqlCached`SELECT * FROM posts ORDER BY views DESC LIMIT 10`;112113// Writes/time-sensitive: no cache114const sqlNoCache = postgres(env.HYPERDRIVE_NO_CACHE.connectionString);115const orders = await sqlNoCache`SELECT * FROM orders WHERE created_at > NOW() - INTERVAL 5 MINUTE`;116```117118## ORMs119120**Drizzle:**121```typescript122import { drizzle } from "drizzle-orm/postgres-js"; // drizzle-orm@^0.45.1123import postgres from "postgres";124125const client = postgres(env.HYPERDRIVE.connectionString, {max: 5, prepare: true});126const db = drizzle(client);127const users = await db.select().from(users).where(eq(users.active, true)).limit(10);128```129130**Kysely:**131```typescript132import { Kysely, PostgresDialect } from "kysely"; // kysely@^0.27+133import postgres from "postgres";134135const db = new Kysely({136dialect: new PostgresDialect({137postgres: postgres(env.HYPERDRIVE.connectionString, {max: 5, prepare: true}),138}),139});140const users = await db.selectFrom("users").selectAll().where("active", "=", true).execute();141```142143See [patterns.md](./patterns.md) for use cases, [gotchas.md](./gotchas.md) for limits.144