@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, 04:45 UTC

main
ts 74 lines 3.12 KB
Raw
1 import type { LibSQLConfig, LibSQLSource } from "@alchemy.run/sql/SQLite";
2 // `@effect/sql-libsql` (and @libsql/client underneath) are optional peers —
3 // value imports are deferred to first use so `@alchemy.run/drizzle` resolves
4 // without them installed.
5 import type * as LibsqlClient from "@effect/sql-libsql/LibsqlClient";
6 import { makeExecutionMemo } from "alchemy/Runtime/ExecutionMemo";
7 import { proxyChain } from "alchemy/Util/proxy-chain";
8 import type { AnyRelations, EmptyRelations } from "drizzle-orm";
9 import type { EffectLibsqlDatabase } from "drizzle-orm/effect-libsql";
10 import type { EffectDrizzleSQLiteConfig } from "drizzle-orm/sqlite-core/effect/utils";
11 import * as Effect from "effect/Effect";
12 import * as Layer from "effect/Layer";
13
14 /**
15 * Open a Drizzle database over a libSQL database (e.g. Turso) using the
16 * `drizzle-orm/effect-libsql` integration.
17 *
18 * Accepts the client returned by `Turso.Connect(db)` — or any
19 * `{ url, authToken }` — and returns a chainable Proxy over
20 * `EffectLibsqlDatabase` (via `proxyChain`): every property read records a
21 * step, every call records args, and the chain is replayed against the
22 * resolved drizzle db when it's finally yielded as an Effect. Callers don't
23 * need a separate `yield* conn` step:
24 *
25 * ```typescript
26 * const conn = yield* Turso.Connect(Db);
27 * const db = yield* Drizzle.LibSQL(conn, { relations });
28 *
29 * fetch: Effect.gen(function* () {
30 * const rows = yield* db.select().from(users);
31 * });
32 * ```
33 *
34 * The client is built lazily on the first query and memoized on the
35 * current execution's `Scope` (via {@link makeExecutionMemo}), so it is
36 * created at most once per execution — a Worker `fetch`/`queue`/`scheduled`
37 * event, a Durable Object call, a Workflow run, or a Lambda invocation —
38 * and closed when that execution settles. Resolving the URL and token is
39 * likewise deferred, so deploy / plan-time invocations never connect.
40 *
41 * `@effect/sql-libsql` client options (e.g. `intMode`) are passed via
42 * `config.client`.
43 *
44 * @binding
45 */
46 export const LibSQL = <TRelations extends AnyRelations = EmptyRelations, E = never, R = never>(
47 source: LibSQLSource<E, R>,
48 config?: EffectDrizzleSQLiteConfig<TRelations> & {
49 /** Overrides for the underlying `@effect/sql-libsql` client. */
50 readonly client?: LibSQLConfig;
51 },
52 ) =>
53 Effect.map(
54 makeExecutionMemo(
55 Effect.gen(function* () {
56 const [LibsqlClient, LibsqlDrizzle, { resolveLibSQLConfig }] = yield* Effect.promise(() =>
57 Promise.all([
58 import("@effect/sql-libsql/LibsqlClient"),
59 import("drizzle-orm/effect-libsql"),
60 import("@alchemy.run/sql/SQLite"),
61 ]),
62 );
63 const { client, ...drizzleConfig } = config ?? {};
64 const ctx = yield* Layer.build(
65 LibsqlClient.layer(yield* resolveLibSQLConfig(source, client)),
66 );
67 return yield* LibsqlDrizzle.makeWithDefaults(
68 drizzleConfig as EffectDrizzleSQLiteConfig<TRelations>,
69 ).pipe(Effect.provideContext(ctx));
70 }),
71 ),
72 (db) =>
73 proxyChain<EffectLibsqlDatabase<TRelations> & { $client: LibsqlClient.LibsqlClient }>(db),
74 );