Connect and query

Create and bind a Hyperdrive configuration

Connect Cloudflare to a database, expose the binding to one Worker, and keep origin credentials out of source.

8 minute lesson

~~~

Create a Hyperdrive configuration from the database connection details. Cloudflare verifies the origin and returns a configuration ID for the Worker binding.

Use nodejs_compat for database drivers and regenerate Worker types. The Worker reads env.HYPERDRIVE.connectionString; it does not need the original database password in source. Scope the origin database user to the application’s real privileges.

Create a practice configuration and verify the binding name and database role before running any write query.

Create the configuration without putting the database password in source:

npx wrangler hyperdrive create app-db --connection-string "$DATABASE_URL"
npx wrangler hyperdrive get app-db

Copy only the returned binding ID into project configuration. Store the origin credential through the supported secret path. Connect from a local or preview environment first, then verify which database, role, and network path the binding actually reaches.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →