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

main
ts 45 lines 1.74 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Redacted from "effect/Redacted";
3 import type * as Scope from "effect/Scope";
4 import type { Pool, PoolOptions } from "mysql2/promise";
5
6 /**
7 * Lazily load the `mysql2/promise` driver (an optional peer dependency of
8 * alchemy) with a descriptive failure when it isn't installed. The
9 * promise flavor is the canonical surface — its `.pool` property exposes
10 * the callback pool for consumers (like Kysely) that drive that API.
11 */
12 export const importMySql = (): Promise<typeof import("mysql2/promise")> =>
13 import("mysql2/promise")
14 .then((mod) =>
15 (mod as { default?: { createPool?: unknown } }).default?.createPool !== undefined
16 ? (mod as unknown as { default: typeof import("mysql2/promise") }).default
17 : mod,
18 )
19 .catch((cause) => {
20 throw new Error(
21 "Failed to load the 'mysql2' driver. Install the optional peer dependency 'mysql2' to connect to MySQL.",
22 { cause },
23 );
24 });
25
26 /**
27 * Open a raw `mysql2/promise` pool on the current `Scope` — `pool.end()`
28 * runs when the scope closes. Pair with `makeExecutionMemo` for the
29 * one-pool-per-event shape workerd and Lambda require.
30 *
31 * `connectionLimit` defaults to 1: per-execution pools never need more
32 * than one connection.
33 */
34 export const openMySQLPool = (
35 url: Effect.Effect<Redacted.Redacted<string>>,
36 config?: Omit<PoolOptions, "uri">,
37 ): Effect.Effect<Pool, never, Scope.Scope> =>
38 Effect.gen(function* () {
39 const mysql = yield* Effect.promise(importMySql);
40 const uri = Redacted.value(yield* url);
41 return yield* Effect.acquireRelease(
42 Effect.sync(() => mysql.createPool({ uri, connectionLimit: 1, ...config })),
43 (pool) => Effect.promise(() => pool.end()),
44 );
45 });