Skip to content

BetterAuth.Drizzle reference

Source: packages/better-auth/src/Drizzle.ts Kind: Layer · Provides: BetterAuth.Database · Peer dependencies: drizzle-orm, @better-auth/drizzle-adapter

Use an existing Drizzle database as Better Auth’s storage via better-auth’s Relations v2 drizzleAdapter.

Accepts a plain drizzle instance, or an Effect resolving to one for databases that only materialize at runtime. NOTE: alchemy’s own Drizzle.Postgres/Drizzle.D1 chainable proxies yield Effects rather than thenables and cannot back the (promise-based) adapter — pass the raw drizzle(...) instance instead.

Schema management is yours: this layer has no automatic migration support (npx auth@1.7.5 generate + your drizzle-kit flow own the tables). Spread generated authRelations after your app’s defineRelations when constructing the db. The CLI configuration must use drizzleAdapter from @better-auth/drizzle-adapter/relations-v2. Run npx auth@1.7.5 generate --config ./auth.cli.ts. Passing --adapter drizzle selects the CLI’s legacy Relations v1 generator.

import { BetterAuth } from "@alchemy.run/better-auth";
import { Drizzle } from "@alchemy.run/better-auth/Drizzle";
import { drizzle } from "drizzle-orm/node-postgres";
import * as schema from "./auth-schema.ts";
import { relations } from "./app-schema.ts";
const db = drizzle({
client: pool,
relations: { ...relations, ...schema.authRelations },
});
Effect.gen(function* () {
const auth = yield* BetterAuth({ emailAndPassword: { enabled: true } });
// ...
}).pipe(Effect.provide(Drizzle(db, { provider: "pg", schema })))