Skip to main content

Hyperdrive

Cloudflare Hyperdrive accelerates access to existing databases with connection pooling and global caching.

Props

name
string
Name of the Hyperdrive configuration.
origin
HyperdriveOriginInput
required
Database connection origin configuration.Can be:
  • Connection string (e.g., "postgres://user:pass@host:5432/db")
  • Secret containing connection string
  • HyperdrivePublicOrigin object
  • HyperdriveOriginWithAccess object
caching
HyperdriveCaching
Caching configuration.
mtls
HyperdriveMtls
mTLS configuration for Hyperdrive.
adopt
boolean
default:"false"
Whether to adopt an existing hyperdrive config.
delete
boolean
default:"true"
Whether to delete the hyperdrive config when the resource is deleted.

Output

id
string
The ID of the resource.
name
string
Name of the Hyperdrive configuration.
hyperdriveId
string
The Cloudflare-generated UUID of the hyperdrive.
origin
HyperdrivePublicOrigin | HyperdriveOriginWithAccess
Database connection origin configuration.
type
hyperdrive
Type identifier for the binding.

Examples

PostgreSQL Connection

const hyperdrive = await Hyperdrive("postgres-db", {
  name: "my-postgres-db",
  origin: {
    database: "postgres",
    host: "database.example.com",
    password: alchemy.secret(process.env.DB_PASSWORD),
    port: 5432,
    user: "postgres"
  }
});

const worker = await Worker("api", {
  entrypoint: "./src/api.ts",
  bindings: {
    DATABASE: hyperdrive
  }
});

MySQL Connection

const hyperdrive = await Hyperdrive("mysql-db", {
  name: "my-mysql-db",
  origin: {
    database: "mydb",
    host: "mysql.example.com",
    password: alchemy.secret(process.env.DB_PASSWORD),
    port: 3306,
    scheme: "mysql",
    user: "mysql_user"
  }
});

Connection String

const hyperdrive = await Hyperdrive("db", {
  origin: alchemy.secret(process.env.DATABASE_URL)
});

Hyperdrive with Caching Disabled

const hyperdrive = await Hyperdrive("no-cache-db", {
  name: "no-cache-db",
  origin: {
    database: "postgres",
    host: "database.example.com",
    password: alchemy.secret(process.env.DB_PASSWORD),
    port: 5432,
    user: "postgres"
  },
  caching: {
    disabled: true
  }
});

Hyperdrive with mTLS

const hyperdrive = await Hyperdrive("secure-db", {
  name: "secure-db",
  origin: {
    database: "postgres",
    host: "database.example.com",
    password: alchemy.secret(process.env.DB_PASSWORD),
    port: 5432,
    user: "postgres"
  },
  mtls: {
    ca_certificate_id: "00000000-0000-0000-0000-0000000000",
    mtls_certificate_id: "00000000-0000-0000-0000-0000000000",
    sslmode: "verify-full"
  }
});

Hyperdrive with Access Client Credentials

const hyperdrive = await Hyperdrive("access-db", {
  name: "access-db",
  origin: {
    database: "postgres",
    host: "database.example.com",
    access_client_id: "client-id",
    access_client_secret: alchemy.secret(process.env.ACCESS_CLIENT_SECRET),
    port: 5432,
    user: "postgres"
  }
});

Build docs developers (and LLMs) love