[Bug]: High failure rate (~95%) with `useDatabase` and Cloudflare Hyperdrive compared to manual connection
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 11.2k
- Forks
- 899
- Avg merge
- 2d 24m
- Merged PRs (30d)
- 40
Description
Environment
Operating System: macOS / Windows - locally, but issue is on Cloudflare Workers
Bun Version: v1.3.5
Nitro Version: nitro-nightly@3.0.1-20251219-124228-00598a8a
PackageManager: bun
Deployment Target: Cloudflare Workers
Database: PostgreSQL (via Cloudflare Hyperdrive)
Reproduction
I am using the experimental useDatabase feature in Nitro 3 with a Cloudflare Hyperdrive binding.
- Configuration (
nitro.config.ts):
export default defineNitroConfig({
experimental: {
database: true,
},
database: {
default: {
connector: 'cloudflare-hyperdrive-postgresql',
options: {
bindingName: 'HYPERDRIVE'
}
}
}
})
- The Failing Implementation (Using
useDatabase): When using the built-inuseDatabasewithdb0/integrations/drizzle, requests fail with a 500 error approximately 95% of the time.
import { os } from '@orpc/server'
import { drizzle } from 'db0/integrations/drizzle'
import { useDatabase } from 'nitro/database'
import type { schema } from '#server/database'
export const dbProvider = os.middleware(async ({ context, next }) => {
// This fails 95% of the time with 500 errors
const db0 = useDatabase()
const db = drizzle<typeof schema>(db0)
return next({
context: {
...context,
db,
},
})
})
- The Working Implementation (Manual Connection): When I manually create the connection using the Hyperdrive connection string directly (bypassing
useDatabaseabstraction), it works 100% of the time.
import type { Hyperdrive } from '@cloudflare/workers-types'
import { os } from '@orpc/server'
import { drizzle } from 'drizzle-orm/node-postgres'
import type { H3Event } from 'nitro/h3'
import { useRuntimeConfig } from 'nitro/runtime-config'
import { schema } from '#server/database'
type CloudflareEnv = {
HYPERDRIVE?: Hyperdrive
}
const getDbUrl = (event: H3Event): string => {
const env = event.runtime?.cloudflare?.env as CloudflareEnv | undefined
const config = useRuntimeConfig()
return env?.HYPERDRIVE?.connectionString ?? (config.databaseUrl as string)
}
export const dbProvider = os
.$context<{ event: H3Event }>()
.middleware(async ({ context, next }) => {
const dbUrl = getDbUrl(context.event)
const db = drizzle(dbUrl, { schema })
return next({
context: {
...context,
db,
},
})
})
Describe the bug
When deploying to Cloudflare Workers and using the cloudflare-hyperdrive-postgresql connector via useDatabase, database queries fail sporadically but frequently (almost 95% failure rate).
The symptoms are:
- Requests return a 500 Internal Server Error.
Additional context
No response
Logs
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with nitro.config.ts and the cloudflare-hyperdrive-postgresql connector used by useDatabase; compare that path with the manual Hyperdrive connection shown in the issue. Reproduce the failures on Cloudflare Workers and inspect the 500 responses, then verify that the configured connector handles requests as reliably as the manual implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cloud, postgresql, typescript
- Domain
- backend, cloud, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100