Skip to content

[bug] hub:db does not work with better-auth + hyperdrive setup (for postgresql) #706

@miguelrk

Description

@miguelrk

TLDR: how can we setup betterAuth() so that it works on cloudflare using hyperdrive with hub:db? Any attempt I've made does not work. This used to work before with a custom useDB() implementation, but does not work anymore with hub:db.

Context:
I am deploying a NuxtHub application to Cloudflare Workers using PostgreSQL (PlanetScale) via Cloudflare Hyperdrive. I am trying to use hub:db directly with Better Auth.

The issue is that hub:db does not appear to detect the Hyperdrive binding at runtime in the Worker. Instead, it seems to silently fall back to the default NuxtHub database (Cloudflare D1/SQLite).

Because Better Auth is configured with provider: 'pg', it generates PostgreSQL syntax (e.g., $1 placeholders, LATERAL JOIN, json_agg), which crashes immediately when executed against the fallback SQLite database.

The Stack:

  • NuxtHub (latest)
  • Database: PlanetScale (Postgres) via Cloudflare Hyperdrive
  • Auth: Better Auth (better-auth)
  • Deployment: Cloudflare Workers

Relevant Links:

Configuration:

  1. nuxt.config.ts:

    hub: {
      db: {
        dialect: 'postgresql',
        driver: 'postgres-js',
        connection: {
          connectionString: process.env.DATABASE_URL
        }
      }
    }
  2. wrangler.jsonc:

    "hyperdrive": [
      {
        "binding": "POSTGRES",
        "id": "...",
        "localConnectionString": "postgresql://..."
      }
    ]
  3. auth.ts (Better Auth config):

    import { db, schema } from 'hub:db' 
    
    export const serverAuth = () => {
      return betterAuth({
        database: drizzleAdapter(db, { 
          schema,
          provider: 'pg', // Configured for Postgres
          usePlural: true 
        }),
        // ...
      })
    }

The Error:
When attempting to fetch a session or user, Better Auth throws the following error. Note the PostgreSQL syntax ($1, json_agg, lateral) which causes the crash, maybe because hub:db is actually connected to D1 (SQLite)?

# SERVER_ERROR:  Error: Failed query: select "users"."id", "users"."name", "users"."email", "users"."emailVerified", "users"."image", "users"."createdAt", "users"."updatedAt", "users"."role", "users"."settingsColorMode", "users"."settingsLocale", "users_members"."data" as "members" from "users" "users" left join lateral (select coalesce(json_agg(json_build_array("users_members"."id", "users_members"."organizationId", "users_members"."role")), '[]'::json) as "data" from "members" "users_members" where "users_members"."userId" = "users"."id") "users_members" on true where "users"."id" = $1 limit $2 params: k4z1cvbmc45a,1

In general, any help or pointers would be greatly appreciated, I have been stuck in this for 1 day already.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions