Effect CommunityEC
Effect Community7mo ago
4 replies
Matt

Integrating `better-auth` with `@effect/sql-drizzle` without initializing separate database conne...

I'm using better-auth with drizzle. However, to configure better-auth, you need to do the following and pass in the db client:
export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: "pg",
  }),
});


This is then used to construct the auth routes, and for retrieving the sessions, etc.
import { auth } from "@acme/auth/auth";
import { toNextJsHandler } from "@acme/auth/lib";

export const { POST, GET } = toNextJsHandler(auth);

// ...

const session = await auth.api.getSession({
  headers: await headers(),
});


The problem I face is that the entirety of my application is using the @effect/sql-drizzle package which handles the database client construction, etc.
const DrizzleLive = PgDrizzleLayer.pipe(Layer.provide(/* config */));
export const DatabaseLive = Layer.mergeAll(DrizzleLive, PgLive);


I don't really want to initialise separate database connections for the two (Effect and non-Effect). However, I'm unsure if it's possible to access the database client via yield* PgDrizzle. Alternatively, should I be looking to wrap the better-auth package with Effect, make the auth routes effectful, and then use the @effect/sql-drizzle package to access the database client and construct better-auth?
Was this page helpful?