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
| 1 | import * as LibsqlClient from "@effect/sql-libsql/LibsqlClient"; |
| 2 | import { makeExecutionMemo } from "alchemy/Runtime/ExecutionMemo"; |
| 3 | import { proxyChain } from "alchemy/Util/proxy-chain"; |
| 4 | import * as Context from "effect/Context"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import type * as Redacted from "effect/Redacted"; |
| 8 | import * as Sql from "effect/sql/SqlClient"; |
| 9 | |
| 10 | /** |
| 11 | * Where to reach a libSQL database: its URL and (for remote databases) an |
| 12 | * auth token, as plain values or Effects of them. Structurally matches the |
| 13 | * client returned by `Turso.Connect(db)`, so it can be passed straight in. |
| 14 | */ |
| 15 | export interface LibSQLSource<E = never, R = never> { |
| 16 | readonly url: string | Effect.Effect<string, E, R>; |
| 17 | readonly authToken?: Redacted.Redacted<string> | Effect.Effect<Redacted.Redacted<string>, E, R>; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Options forwarded to `@effect/sql-libsql`'s `LibsqlClient` (everything |
| 22 | * except the connection, which comes from the {@link LibSQLSource}). |
| 23 | */ |
| 24 | export type LibSQLConfig = Omit< |
| 25 | Extract<LibsqlClient.LibsqlClientConfig, { readonly url: unknown }>, |
| 26 | "url" | "authToken" |
| 27 | >; |
| 28 | |
| 29 | const resolve = <A, E, R>(value: A | Effect.Effect<A, E, R>) => |
| 30 | Effect.isEffect(value) ? value : Effect.succeed(value); |
| 31 | |
| 32 | /** |
| 33 | * Resolve a {@link LibSQLSource} and {@link LibSQLConfig} into the |
| 34 | * `LibsqlClientConfig` handed to `@effect/sql-libsql`. |
| 35 | */ |
| 36 | export const resolveLibSQLConfig = Effect.fn(function* <E = never, R = never>( |
| 37 | source: LibSQLSource<E, R>, |
| 38 | config?: LibSQLConfig, |
| 39 | ) { |
| 40 | const url = yield* resolve(source.url); |
| 41 | const authToken = source.authToken === undefined ? undefined : yield* resolve(source.authToken); |
| 42 | return { ...config, url, authToken } satisfies LibsqlClient.LibsqlClientConfig; |
| 43 | }); |
| 44 | |
| 45 | /** |
| 46 | * Open an `@effect/sql-libsql` client over a SQLite database through |
| 47 | * libSQL — a Turso database, any other libSQL server, or a local |
| 48 | * `file:` database. |
| 49 | * |
| 50 | * Accepts the client returned by `Turso.Connect(db)` (or any |
| 51 | * `{ url, authToken }`) and returns a `LibsqlClient` (which implements the |
| 52 | * generic `SqlClient` interface) wrapped in a chainable Proxy, so it can be |
| 53 | * resolved once at init and used from any handler: |
| 54 | * |
| 55 | * ```typescript |
| 56 | * import * as SQL from "@alchemy.run/sql/SQLite"; |
| 57 | * |
| 58 | * const conn = yield* Turso.Connect(Db); |
| 59 | * const sql = yield* SQL.LibSQL(conn); |
| 60 | * |
| 61 | * fetch: Effect.gen(function* () { |
| 62 | * const users = yield* sql`SELECT * FROM users`; |
| 63 | * }); |
| 64 | * ``` |
| 65 | * |
| 66 | * The client is built lazily on the first query and memoized on the |
| 67 | * current execution's `Scope` (via {@link makeExecutionMemo}), so it's |
| 68 | * created at most once per execution — a Worker `fetch`/`queue`/`scheduled` |
| 69 | * event, a Durable Object call, a Workflow run, or a Lambda invocation — and |
| 70 | * closed when the event settles. Resolving the URL and token is likewise |
| 71 | * deferred, so deploy / plan-time invocations never connect. |
| 72 | * |
| 73 | * @binding |
| 74 | */ |
| 75 | export const LibSQL = <E = never, R = never>(source: LibSQLSource<E, R>, config?: LibSQLConfig) => |
| 76 | Effect.map( |
| 77 | makeExecutionMemo( |
| 78 | Effect.gen(function* () { |
| 79 | const ctx = yield* Layer.build( |
| 80 | LibsqlClient.layer(yield* resolveLibSQLConfig(source, config)), |
| 81 | ); |
| 82 | return Context.get(ctx, LibsqlClient.LibsqlClient); |
| 83 | }), |
| 84 | ), |
| 85 | (client) => proxyChain<LibsqlClient.LibsqlClient>(client), |
| 86 | ); |
| 87 | |
| 88 | /** |
| 89 | * Provide an `@effect/sql-libsql` client as the `LibsqlClient` and generic |
| 90 | * `SqlClient` services, so cloud-agnostic services written against |
| 91 | * `SqlClient.SqlClient` (or drizzle's `effect-libsql` driver, which depends |
| 92 | * on `LibsqlClient`) run on a libSQL database: |
| 93 | * |
| 94 | * ```typescript |
| 95 | * const conn = yield* Turso.Connect(Db); |
| 96 | * const app = yield* makeApp.pipe(Effect.provide(SQL.LibSQLLayer(conn))); |
| 97 | * ``` |
| 98 | * |
| 99 | * The layer itself builds synchronously at init; the underlying client is |
| 100 | * created lazily per execution (see {@link LibSQL}). |
| 101 | */ |
| 102 | export const LibSQLLayer = <E = never, R = never>( |
| 103 | source: LibSQLSource<E, R>, |
| 104 | config?: LibSQLConfig, |
| 105 | ) => |
| 106 | // Derive SqlClient from the single LibsqlClient build so both tags share |
| 107 | // one per-execution client. |
| 108 | Layer.effect(Sql.SqlClient, LibsqlClient.LibsqlClient).pipe( |
| 109 | Layer.provideMerge(Layer.effect(LibsqlClient.LibsqlClient, LibSQL(source, config))), |
| 110 | ); |