Skip to content

Cloudflare.Hyperdrive reference

Source: src/Cloudflare/Hyperdrive/Connect.ts

A typed accessor for a Cloudflare Hyperdrive runtime binding inside a Worker. Provides the same shape as the raw Hyperdrive runtime object (connection string, host, port, user, password, database) plus a raw escape hatch for libraries that want direct access.

Bind Hyperdrive in a Worker

const hd = yield* Cloudflare.Hyperdrive.Connect(MyConnection);
const url = yield* hd.connectionString;

/ /** Bind a Connection to a Worker and obtain the Effect-native Hyperdrive client (connection string, host, port, …).

Connect is a single identifier that is simultaneously the binding’s Context tag, its type, and the callable — yield* Cloudflare.Hyperdrive.Connect(conn).

Using Hyperdrive inside a Worker

const hd = yield* Cloudflare.Hyperdrive.Connect(MyConnection);
const url = yield* hd.connectionString;

Source: src/Cloudflare/Hyperdrive/Connection.ts

A Cloudflare Hyperdrive configuration.

Hyperdrive accelerates and pools connections to existing PostgreSQL or MySQL databases, exposing them to Workers via a binding. Create a config as a resource, then bind it to a Worker to obtain a connection string.

const hd = yield* Cloudflare.Hyperdrive.Connection("my-pg", {
origin: {
scheme: "postgres",
host: "db.example.com",
port: 5432,
database: "app",
user: "app",
password: yield* Config.Redacted("DB_PASSWORD"),
},
});

Connection: Connecting to a private database

Section titled “Connection: Connecting to a private database”

A database inside a private network (e.g. Aurora or RDS in an AWS VPC) is reached through Workers VPC: run cloudflared inside the network with the Tunnel’s token, expose the database as a TCP VPC Service, and point Hyperdrive at the service.

const tunnel = yield* Cloudflare.Tunnel.Tunnel("VpcTunnel");
const service = yield* Cloudflare.Connectivity.DirectoryService("Aurora", {
type: "tcp",
tcpPort: 5432,
appProtocol: "postgresql",
host: {
hostname: "my-cluster.cluster-abc123.us-east-1.rds.amazonaws.com",
resolverNetwork: { tunnelId: tunnel.tunnelId },
},
});
const hd = yield* Cloudflare.Hyperdrive.Connection("Aurora", {
origin: {
scheme: "postgres",
serviceId: service.serviceId,
database: "app",
user: "app",
password: yield* Config.Redacted("DB_PASSWORD"),
},
});
const hd = yield* Cloudflare.Hyperdrive.Connect(MyConnection);
const url = yield* hd.connectionString;