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

main
ts 96 lines 3.99 KB
Raw
1 import type { Input, RuntimeContext } from "alchemy";
2 import type { BetterAuthOptions } from "better-auth";
3 import * as Context from "effect/Context";
4 import type * as Effect from "effect/Effect";
5 import type * as Scope from "effect/Scope";
6 import type { BetterAuthMigrationError } from "./Errors.ts";
7
8 /** The database dialect family, as Better Auth's Kysely adapter names them. */
9 export type Provider = "sqlite" | "postgres" | "mysql";
10
11 /**
12 * Everything Better Auth accepts as `options.database`: a raw driver object
13 * (pg `Pool`, mysql2 pool, `bun:sqlite` `Database`, Cloudflare `D1Database`,
14 * ...), a Kysely dialect form, or an adapter factory
15 * (`drizzleAdapter(...)`, `memoryAdapter(...)`, custom adapters).
16 */
17 export type DatabaseInput = NonNullable<BetterAuthOptions["database"]>;
18
19 /**
20 * The Kysely-resolvable subset of {@link DatabaseInput} — raw driver objects
21 * and dialect forms, excluding adapter factories.
22 *
23 * This is the only shape `migrate.connect` may yield: Better Auth's
24 * `getMigrations` supports exactly these forms and hard-`process.exit(1)`s
25 * on adapter factories, so excluding them statically keeps that landmine
26 * unreachable.
27 */
28 export type DirectDatabase = Exclude<DatabaseInput, (...args: never[]) => unknown>;
29
30 export interface DatabaseService {
31 /** Dialect family of the database this layer connects to. */
32 readonly provider: Provider;
33 /**
34 * Build the runtime `options.database` input. Evaluated once per
35 * execution (memoized on the execution Scope by {@link BetterAuth}), so
36 * disposable resources acquired with `Effect.acquireRelease` — pg/mysql
37 * pools — are closed when the event settles. That is the only legal
38 * pooling shape on workerd (sockets are IoContext-pinned).
39 *
40 * Deploy/plan evaluation never runs this effect: construction is deferred
41 * to the first `api`/`fetch`/`getSession` use inside an execution scope.
42 */
43 readonly runtime: Effect.Effect<DatabaseInput, never, RuntimeContext | Scope.Scope>;
44 /**
45 * Deploy-time migration support. `undefined` means this platform cannot
46 * auto-migrate (memory, drizzle, custom adapters): {@link BetterAuth}
47 * skips migration registration, and fails synth if `migrate: true` was
48 * explicitly requested.
49 */
50 readonly migrate?: {
51 /**
52 * Non-secret, canonically-hashable identity of the target database
53 * (resource Outputs allowed) — part of the migration Action's input, so
54 * migrations re-run when the target database is replaced.
55 *
56 * MUST NOT contain the connection string: Action input is persisted to
57 * alchemy state.
58 */
59 readonly identity: Input<Record<string, unknown>>;
60 /**
61 * Two-stage connection for the migration Action.
62 *
63 * The OUTER effect is the Action's *init* half: it runs at stack-eval
64 * time under the Action's capture context — this is where resource
65 * Outputs (`database.databaseId`, a connection-string attr) MUST be
66 * yielded so the engine records them as dependency captures and
67 * resolves them at apply. No I/O here.
68 *
69 * The INNER effect is the *apply* half: it runs inside the Action body
70 * with the resolved captures ambient, acquires the actual client/pool
71 * on the Scope (`pool.end` / `db.close` ride it), and yields a
72 * Kysely-resolvable database.
73 */
74 readonly connect: Effect.Effect<
75 Effect.Effect<DirectDatabase, BetterAuthMigrationError, Scope.Scope>,
76 never,
77 RuntimeContext
78 >;
79 };
80 }
81
82 /**
83 * The database dependency of {@link BetterAuth}.
84 *
85 * Platform layers (`Memory`, `SQLite`, `CloudflareD1`, `Postgres`, `MySQL`,
86 * `Drizzle`, ...) provide this service; `yield* BetterAuth({...})` consumes
87 * it. Provide the layer on the surrounding impl effect:
88 *
89 * ```typescript
90 * Effect.gen(function* () {
91 * const auth = yield* BetterAuth({ emailAndPassword: { enabled: true } });
92 * return { fetch: ... };
93 * }).pipe(Effect.provide(CloudflareD1(Db)))
94 * ```
95 */
96 export class Database extends Context.Service<Database, DatabaseService>()("BetterAuth.Database") {}