Skip to content

CloudflareHyperdrive

Source: packages/better-auth/src/CloudflareHyperdrive.ts Kind: 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).

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,
});
}),
),
),
)

Hyperdrive also fronts MySQL origins — select the dialect and the layer drives mysql2 instead of pg.

CloudflareHyperdrive(connection, {
dialect: "mysql",
migrate: password.connectionUrl,
})