-
-
Notifications
You must be signed in to change notification settings - Fork 770
Open
Labels
Description
Environment
Operating System: macOS / Windows - locally, but issue is on Cloudflare Workers
Bun Version: v1.3.5
Nitro Version: [email protected]
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
franklin-tinacoderabbitai