@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 89 lines 3.56 KB
Raw
1 import { drizzleAdapter } from "@better-auth/drizzle-adapter/relations-v2";
2 import type { RuntimeContext } from "alchemy";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Scope from "effect/Scope";
6 import { Database, type DatabaseInput, type Provider } from "./Database.ts";
7
8 type DrizzleDatabase = Parameters<typeof drizzleAdapter>[0];
9 type DrizzleDatabaseEffect = Effect.Effect<DrizzleDatabase, never, RuntimeContext | Scope.Scope>;
10
11 const isDatabaseEffect = (
12 value: DrizzleDatabase | DrizzleDatabaseEffect,
13 ): value is DrizzleDatabaseEffect => Effect.isEffect(value);
14
15 export interface DrizzleLayerConfig {
16 /** The SQL dialect of the underlying drizzle database. */
17 readonly provider: "pg" | "mysql" | "sqlite";
18 /**
19 * The drizzle schema containing the Better Auth tables (generate it with
20 * `npx auth@1.7.5 generate`). When omitted, the adapter resolves tables
21 * off the db instance's registered schema.
22 */
23 readonly schema?: Record<string, unknown>;
24 /** @default false */
25 readonly usePlural?: boolean;
26 /** @default false (snake_case column names) */
27 readonly camelCase?: boolean;
28 }
29
30 /**
31 * Use an existing Drizzle database as Better Auth's storage via
32 * better-auth's Relations v2 `drizzleAdapter`.
33 *
34 * Accepts a plain drizzle instance, or an Effect resolving to one for
35 * databases that only materialize at runtime. NOTE: alchemy's own
36 * `Drizzle.Postgres`/`Drizzle.D1` chainable proxies yield Effects rather
37 * than thenables and cannot back the (promise-based) adapter — pass the
38 * raw `drizzle(...)` instance instead.
39 *
40 * Schema management is yours: this layer has no automatic migration
41 * support (`npx auth@1.7.5 generate` + your drizzle-kit flow own the
42 * tables). Spread generated `authRelations` after your app's
43 * `defineRelations` when constructing the db. The CLI configuration must
44 * use `drizzleAdapter` from `@better-auth/drizzle-adapter/relations-v2`.
45 * Run `npx auth@1.7.5 generate --config ./auth.cli.ts`.
46 * Passing `--adapter drizzle` selects the CLI's legacy Relations v1 generator.
47 *
48 * ### Bringing your own Drizzle db
49 * **Example:** Postgres drizzle instance with generated auth relations
50 * ```typescript
51 * import { BetterAuth } from "@alchemy.run/better-auth";
52 * import { Drizzle } from "@alchemy.run/better-auth/Drizzle";
53 * import { drizzle } from "drizzle-orm/node-postgres";
54 * import * as schema from "./auth-schema.ts";
55 * import { relations } from "./app-schema.ts";
56 *
57 * const db = drizzle({
58 * client: pool,
59 * relations: { ...relations, ...schema.authRelations },
60 * });
61 *
62 * Effect.gen(function* () {
63 * const auth = yield* BetterAuth({ emailAndPassword: { enabled: true } });
64 * // ...
65 * }).pipe(Effect.provide(Drizzle(db, { provider: "pg", schema })))
66 * ```
67 *
68 * @layer
69 * @provides BetterAuth.Database
70 * @peer drizzle-orm
71 * @peer @better-auth/drizzle-adapter
72 * @product Drizzle
73 */
74 export const Drizzle = (
75 db: DrizzleDatabase | DrizzleDatabaseEffect,
76 config: DrizzleLayerConfig,
77 ): Layer.Layer<Database> =>
78 Layer.sync(Database, () => ({
79 provider: (config.provider === "pg" ? "postgres" : config.provider) as Provider,
80 runtime: Effect.gen(function* () {
81 const database = isDatabaseEffect(db) ? yield* db : db;
82 return drizzleAdapter(database, {
83 provider: config.provider,
84 ...(config.schema !== undefined ? { schema: config.schema } : {}),
85 ...(config.usePlural !== undefined ? { usePlural: config.usePlural } : {}),
86 ...(config.camelCase !== undefined ? { camelCase: config.camelCase } : {}),
87 }) as DatabaseInput;
88 }),
89 }));