@prisma-next/postgres/serverless) and walks through a complete Cloudflare Workers + Hyperdrive deployment.
Two facades, one driver
@prisma-next/postgres exports two facades that compose the same execution stack and differ only in lifecycle ergonomics:
The static authoring surface (
sql, context, stack, contract) is identical on both facades — it is a pure function of the contract and is safe to cache at module scope. The runtime-bound surface differs because closure-caching a Runtime (and the pg.Client wired into it) across fetch invocations causes stale-connection failures after isolate idle and concurrent-fetch races on a shared client.
- Cloudflare Workers + Hyperdrive
- Other runtimes
Cloudflare Workers + Hyperdrive
Cloudflare Workers + Hyperdrive is the primary tested deployment path. Hyperdrive is Cloudflare’s managed Postgres connection pooler at the edge: the Worker connects to it using the standard Postgres wire protocol via thepg library, and Hyperdrive pools connections to your origin Postgres. The Worker reads the connection string off env.HYPERDRIVE.connectionString.Architecture
Step 1: Provision Hyperdrive
wrangler.jsonc:nodejs_compat is required. The Postgres driver (pg) uses Node built-ins that workerd polyfills under that flag.Step 2: Configure local dev
Forwrangler dev, set the local connection string in .env (not .dev.vars):Step 3: Module-scope setup
Construct thedb client once per isolate at module scope. Only the static authoring surface is closure-cached here — the runtime-bound surface is acquired per request:Step 4: Per-request handler
Wiring the ORM client
createOrmClient(runtime) is a factory that passes the per-request runtime to orm({...}):Migrations from Node
Migrations stay on Node, against the origin database connection string — not Hyperdrive or any edge pooler. Migration commands (prisma-next migration apply, prisma-next db init, etc.) are control-plane operations that run in long-lived Node processes (CI runners, deploy pipelines, dev workstations). Routing DDL through Hyperdrive is explicitly not recommended: Hyperdrive caches query results at the edge, which is desirable for runtime reads but unsafe for migration ledger reads (a stale read could cause duplicate-apply or skipped-apply).