EngineeringEngineering Reference
Hyperdrive + Neon DB Routing
This document describes the repo-specific setup to route app, studio, and admin database traffic through Cloudflare Hyperdrive backed by Neon.
Scope
- Applies to Worker runtime DB calls from
apps/app,apps/studio, andapps/admin - Keeps local scripts and Drizzle CLI workflows on direct
DATABASE_URL - Preserves existing Better Auth architecture (regular user auth + isolated admin Slack auth)
Runtime Behavior
The DB layer in packages/core/features/db.ts now supports three modes via DB_CONNECTION_MODE:
auto(default): use Hyperdrive when the worker provides a Hyperdrive connection string, otherwise fallback to directDATABASE_URLhyperdrive: require Hyperdrive (throws if missing)direct: always useDATABASE_URL
Each worker isolate logs the selected path once:
[db] Using hyperdrive connection (auto mode)or
[db] Using direct connection (auto mode)Files Updated
packages/core/features/db.ts— dual-path DB resolver + Hyperdrive runtime configuratorpackages/core/env/schema.ts—DB_CONNECTION_MODEschemaapps/app/src/start.ts— passes Hyperdrive connection string into core DB configapps/studio/src/start.ts— passes Hyperdrive connection string into core DB configapps/admin/src/start.ts— passes Hyperdrive connection string into core DB configapps/app/wrangler.jsonc— dev Hyperdrive binding scaffoldapps/studio/wrangler.jsonc— dev Hyperdrive binding scaffoldapps/admin/wrangler.jsonc— dev Hyperdrive binding scaffoldnx.json— addsDB_CONNECTION_MODEtonamedInputs.sharedGlobals
Cloudflare Setup
Create Hyperdrive configs (recommended: one per environment) with caching disabled for auth-heavy traffic:
pnpm dlx wrangler hyperdrive create valguide-neon-dev --connection-string="postgres://..." --caching-disabled
pnpm dlx wrangler hyperdrive create valguide-neon-prod --connection-string="postgres://..." --caching-disabledThen replace the placeholder id 00000000-0000-0000-0000-000000000000 in each app's wrangler.jsonc env.dev.hyperdrive[0].id with your real dev config id.
Rollout Sequence
- Set
DB_CONNECTION_MODE=autoeverywhere (safe default). - Configure real Hyperdrive IDs in
env.devforadmin,studio,app. - Deploy and validate in this order:
adminthenstudiothenapp. - After dev validation, add production Hyperdrive bindings and deploy in same order.
- Optionally move to
DB_CONNECTION_MODE=hyperdriveafter full confidence.
Validation Checklist
- Admin Slack login + protected admin queries work
- Studio OTP login/invite/team flows work
- App public data reads work
- Better Auth session checks still hit DB correctly
- No stale-read issues in write-then-read paths
- Worker logs show expected DB path
Rollback
Immediate rollback path:
- Set
DB_CONNECTION_MODE=direct - Redeploy affected app
No schema migration or auth-logic rollback is needed for this transport-level rollback.