CloudflareHyperdrive
Source:
packages/better-auth/src/CloudflareHyperdrive.tsKind: Layer · Provides:BetterAuth.Database· Peer dependencies:pg,mysql2
Cloudflare Hyperdrive layer for Better Auth — pooled TCP Postgres (or MySQL) through a Hyperdrive connection.
Hyperdrive’s connection string is minted per-invocation inside the
Worker and cannot be used at deploy time, so pass the ORIGIN database
URL as migrate for deploy-time schema migrations (or omit it to skip
them).
Pooled Postgres through Hyperdrive
Section titled “Pooled Postgres through Hyperdrive”The Worker connects over TCP through Hyperdrive’s pooler; auth reads should disable Hyperdrive caching so sessions are never stale.
import { BetterAuth } from "@alchemy.run/better-auth";import { CloudflareHyperdrive } from "@alchemy.run/better-auth/CloudflareHyperdrive";
export const PgProject = Neon.Project("AuthDb");export const Hyperdrive = Effect.gen(function* () { const project = yield* PgProject; return yield* Cloudflare.Hyperdrive.Connection("AuthHd", { origin: project.origin, caching: { disabled: true }, });});
Effect.gen(function* () { const auth = yield* BetterAuth({ emailAndPassword: { enabled: true } }); return { fetch: ... };}).pipe( Effect.provide( Layer.unwrap( Effect.gen(function* () { const project = yield* PgProject; const connection = yield* Hyperdrive; return CloudflareHyperdrive(connection, { migrate: project.connectionUri, }); }), ), ),)MySQL origins
Section titled “MySQL origins”Hyperdrive also fronts MySQL origins — select the dialect and the layer
drives mysql2 instead of pg.
CloudflareHyperdrive(connection, { dialect: "mysql", migrate: password.connectionUrl,})