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

main
ts 93 lines 3.14 KB
Raw
1 import {
2 resolveConnectionSource,
3 staticConnectionSource,
4 type ConnectionSource,
5 } from "@alchemy.run/sql/ConnectionSource";
6 import { openMySQLPool } from "@alchemy.run/sql/MySQLDriver";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9 import type * as Redacted from "effect/Redacted";
10 import type * as Scope from "effect/Scope";
11 import type { PoolOptions } from "mysql2/promise";
12 import { Database, type DatabaseService, type DirectDatabase } from "./Database.ts";
13 import { makeMigrateSupport, type SqlLayerOptions } from "./Postgres.ts";
14
15 export interface MySQLOptions extends SqlLayerOptions {
16 readonly pool?: Omit<PoolOptions, "uri">;
17 }
18
19 /**
20 * Build the MySQL {@link DatabaseService}. Better Auth's Kysely path
21 * expects the `mysql2/promise` pool: it passes the pool itself as the
22 * MysqlDialect config, and the promise pool's `.pool` property is the
23 * callback pool Kysely drives.
24 *
25 * @internal
26 */
27 export const makeMySQLService = (
28 source: ConnectionSource,
29 options: MySQLOptions | undefined,
30 ): Effect.Effect<DatabaseService> =>
31 Effect.gen(function* () {
32 const urlEffect = yield* resolveConnectionSource(source);
33 const migrateSource = staticConnectionSource(source, options?.migrate);
34 const open = (url: Effect.Effect<Redacted.Redacted<string>>) =>
35 openMySQLPool(url, options?.pool) as unknown as Effect.Effect<
36 DirectDatabase,
37 never,
38 Scope.Scope
39 >;
40
41 return {
42 provider: "mysql",
43 runtime: open(urlEffect) as DatabaseService["runtime"],
44 ...(migrateSource === undefined
45 ? {}
46 : {
47 migrate: makeMigrateSupport(
48 migrateSource,
49 open,
50 "Failed to load `mysql2` — install it to migrate a MySQL-backed BetterAuth",
51 ),
52 }),
53 } satisfies DatabaseService;
54 }) as Effect.Effect<DatabaseService>;
55
56 /**
57 * Generic TCP MySQL database layer for Better Auth, from a connection
58 * string.
59 *
60 * Works with PlanetScale MySQL, Cloudflare Hyperdrive MySQL origins, AWS
61 * RDS MySQL, or any literal URL. One `mysql2` pool per execution, closed
62 * when the event settles.
63 *
64 *
65 * ### Connecting to PlanetScale MySQL
66 * PlanetScale requires TLS — pass it in the URL's `ssl` query parameter
67 * (mysql2 parses it as JSON).
68 * **Example:** PlanetScale MySQL with TLS
69 * ```typescript
70 * import { BetterAuth } from "@alchemy.run/better-auth";
71 * import { MySQL } from "@alchemy.run/better-auth/MySQL";
72 *
73 * const password = yield* Planetscale.MySQLPassword("auth-db", {
74 * database,
75 * role: "admin",
76 * });
77 * const url =
78 * `mysql://${password.username}:...@${password.host}/${database.name}` +
79 * `?ssl=${encodeURIComponent('{"rejectUnauthorized":true}')}`;
80 *
81 * Effect.gen(function* () {
82 * const auth = yield* BetterAuth({ emailAndPassword: { enabled: true } });
83 * return { fetch: ... };
84 * }).pipe(Effect.provide(MySQL(url)))
85 * ```
86 *
87 * @layer
88 * @provides BetterAuth.Database
89 * @peer mysql2
90 * @product MySQL
91 */
92 export const MySQL = (url: ConnectionSource, options?: MySQLOptions): Layer.Layer<Database> =>
93 Layer.effect(Database, makeMySQLService(url, options)) as Layer.Layer<Database>;