Skip to content

[Bug]: High failure rate (~95%) with useDatabase and Cloudflare Hyperdrive compared to manual connectionΒ #3893

@imbytecat

Description

@imbytecat

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.

  1. Configuration (nitro.config.ts):
export default defineNitroConfig({
  experimental: {
    database: true,
  },
  database: {
    default: {
      connector: 'cloudflare-hyperdrive-postgresql',
      options: {
        bindingName: 'HYPERDRIVE' 
      }
    }
  }
})
  1. The Failing Implementation (Using useDatabase): When using the built-in useDatabase with db0/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,
    },
  })
})
  1. The Working Implementation (Manual Connection): When I manually create the connection using the Hyperdrive connection string directly (bypassing useDatabase abstraction), 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:

  1. Requests return a 500 Internal Server Error.
Image Image

Additional context

No response

Logs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions