nitrojs / nitrojs/nitro

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

Open
#3,893 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug v3
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.

  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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.