Can't use Cloudflare D1 database with Hono backend on Cloudflare Workers

Due to the fact that the env context is only exposed to incoming requests, It can't seem to be possible to create a config for better-auth on Hono using the D1 Database from Cloudflare Workers.

This example of D1 with better-auth is for the OpenNext new implementation for Workers, I'm wondering if there's a way to set it up also on Hono.

I've been able to make it work from SolidStart with a similar configuration but I'm stuck on Hono, and I'd prefer to have the server backend of better-auth on a separate Hono Worker without relying on CF Functions from the full-stack app.

import { betterAuth } from "better-auth";
import { anonymous } from "better-auth/plugins";
import { Kysely } from "kysely";
import { D1Dialect } from "kysely-d1";
// Can't get a reference of D1 from here since it's outside the incoming request scope

export const auth = betterAuth({
  database: {
    db: new Kysely({
      dialect: new D1Dialect({
        database: process.env.DB as unknown as D1Database, // this results in database: undefined
      }),
    }),
    type: "sqlite",
  },
  emailAndPassword: {
    enabled: true,
  },
  plugins: [anonymous()],
});
Was this page helpful?