Use Cloudflare Hyperdrive with DaloyJS
Hyperdrive lets a Worker talk to an existing Postgres or MySQL database with the ordinary pg, postgres, or mysql2 drivers. It is not a database: it keeps a pool of warm connections close to your database, so each Worker request skips the TCP, TLS, and auth handshakes to the origin, and it can cache the results of read-only queries at the edge.
Use it with any standard Postgres or MySQL host: Neon, Supabase, PlanetScale (Postgres and MySQL), Amazon RDS, Aurora Postgres or MySQL, or your own server.
- 01requestDaloyJS WorkerHyperdrivenew Client(env.HYPERDRIVE.connectionString)per request · connect is fast and local
- 02noteHyperdriveHyperdriveCached read? return iteligible SELECTs only · max_age 60 s by default
- 03requestHyperdriveDatabaseQuery on a pooled, warm connectionno new TCP / TLS / auth handshake
- 04responseDatabaseDaloyJS WorkerRows
- 05asyncHyperdriveHyperdriveRequest ends: client cleaned uporigin connection stays in the pool
1. Create a Hyperdrive config
Use a mysql:// connection string for MySQL. Connect Hyperdrive with a role that only has the privileges the app needs, not the database owner. Caching is on by default; pass --caching-disabled to turn it off, or --max-age to change how long results are kept.
2. Bind it in wrangler.jsonc
Then generate the binding types, so env.HYPERDRIVE is typed as Hyperdrive without a cast:
3. Install a driver
Cloudflare's Hyperdrive guides require pg 8.13.0 or later, postgres 3.4.5 or later, and mysql2 3.13.0 or later.
4. Decorate a client factory
Sockets cannot be created at module scope or shared across requests, so do not decorate a connected client. Decorate a small factory once, before the routes, and call it inside the handler. Creating a client per request is cheap because Hyperdrive holds the real pool. Hyperdrive cleans up the client when the request ends and keeps the origin connection pooled, so calling client.end() is optional.
Always pass request values as parameters ($1), never by string concatenation. See SQL injection.
postgres.js
MySQL with mysql2
With Drizzle ORM
For Prisma, use @prisma/adapter-pg with env.HYPERDRIVE.connectionString and build the client inside the request; see Prisma.
Query caching caveats
- Only eligible read-only queries are cached. Writes and queries that call volatile or stable functions (
NOW(),CURRENT_DATE,RANDOM()) are never cached; pass such values in as parameters if you want the query cached. - Writes do not invalidate the cache. A
SELECTright after anUPDATEcan return the old row untilmax_age(60 seconds by default, plus a shortstale_while_revalidatewindow) expires. - For read-after-write paths (auth, sessions, balances, anything an ORM or auth library issues for you), bind a second Hyperdrive config for the same database created with
--caching-disabled, and use it for those queries. Both configs count toward the origin's connection limit. - Do not try to control caching with SQL comments; Cloudflare documents that detection is text-based and parser behavior varies.
Local development
wrangler dev connects to localConnectionString (or the CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE environment variable) directly, so run a local Postgres or MySQL in Docker or point it at a development branch. Add sslmode=require (Postgres) or sslMode=REQUIRED (MySQL) when the database needs TLS. Caching does not apply locally; wrangler dev --remote runs on Cloudflare's network with caching on.
Things to remember
- Hyperdrive needs a database reachable from Cloudflare (public endpoint, or a private network through Cloudflare Tunnel) and uses the static credentials stored in the config. Databases that only accept short-lived IAM tokens, such as Aurora DSQL, are not covered by this guide.
- Hyperdrive pools for you, so the provider's own pooler is usually unnecessary. Check Cloudflare's provider guide for which connection string to use.
- Combine Hyperdrive with Smart Placement when one request runs several sequential queries.
- Hyperdrive does not report database health to DaloyJS. For a database readiness probe, see Readiness checks.
See also the Cloudflare Workers adapter, Cloudflare D1, and the database hosting overview.