The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Binding from "alchemy/Binding"; |
| 2 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import type * as Redacted from "effect/Redacted"; |
| 6 | import type { Database } from "./Database.ts"; |
| 7 | import { DatabaseToken, type TokenAuthorization } from "./DatabaseToken.ts"; |
| 8 | |
| 9 | /** |
| 10 | * libSQL connection accessors for one Turso database. Resolving them does |
| 11 | * not open a connection; pass the client to `SQL.LibSQL` or |
| 12 | * `Drizzle.LibSQL`, or read `url` / `authToken` for `@libsql/client`. |
| 13 | */ |
| 14 | export interface ConnectClient { |
| 15 | /** libSQL URL of the database (`libsql://<hostname>`). */ |
| 16 | url: Effect.Effect<string, never, RuntimeContext>; |
| 17 | /** SQL auth token for the database. */ |
| 18 | authToken: Effect.Effect<Redacted.Redacted<string>, never, RuntimeContext>; |
| 19 | } |
| 20 | |
| 21 | export interface ConnectOptions { |
| 22 | /** |
| 23 | * Access level of the token minted for this host. |
| 24 | * @default "full-access" |
| 25 | */ |
| 26 | authorization?: TokenAuthorization; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Connect to a Turso database from a Worker, Lambda, or other host. The |
| 31 | * host gets the database's URL and its own {@link DatabaseToken}. Your |
| 32 | * Turso API token stays on your machine. |
| 33 | * |
| 34 | * ### Querying a Database |
| 35 | * **Example:** Effect SQL over a Turso database |
| 36 | * ```typescript |
| 37 | * Effect.gen(function* () { |
| 38 | * const conn = yield* Turso.Connect(Db); |
| 39 | * const sql = yield* SQL.LibSQL(conn); |
| 40 | * return { |
| 41 | * fetch: Effect.gen(function* () { |
| 42 | * return yield* HttpServerResponse.json(yield* sql`SELECT 1 AS value`); |
| 43 | * }), |
| 44 | * }; |
| 45 | * }).pipe(Effect.provide(Turso.ConnectHttp)); |
| 46 | * ``` |
| 47 | * |
| 48 | * **Example:** Drizzle over a Turso database |
| 49 | * ```typescript |
| 50 | * const conn = yield* Turso.Connect(Db); |
| 51 | * const db = yield* Drizzle.LibSQL(conn, { relations }); |
| 52 | * ``` |
| 53 | * |
| 54 | * **Example:** AWS Lambda (install the native libSQL client) |
| 55 | * ```typescript |
| 56 | * export default class Api extends Lambda.Function<Api>()( |
| 57 | * "Api", |
| 58 | * { main: import.meta.url, build: { install: ["@libsql/client"] } }, |
| 59 | * Effect.gen(function* () { |
| 60 | * const sql = yield* SQL.LibSQL(yield* Turso.Connect(Db)); |
| 61 | * // ... |
| 62 | * }).pipe(Effect.provide(Turso.ConnectHttp)), |
| 63 | * ) {} |
| 64 | * ``` |
| 65 | * |
| 66 | * **Example:** Read-only access |
| 67 | * ```typescript |
| 68 | * const conn = yield* Turso.Connect(Db, { authorization: "read-only" }); |
| 69 | * ``` |
| 70 | * |
| 71 | * @binding |
| 72 | * @product Database |
| 73 | */ |
| 74 | export interface Connect extends Binding.Service< |
| 75 | Connect, |
| 76 | "Turso.Connect", |
| 77 | (database: Database, options?: ConnectOptions) => Effect.Effect<ConnectClient> |
| 78 | > {} |
| 79 | |
| 80 | export const Connect = Binding.Service<Connect>("Turso.Connect"); |
| 81 | |
| 82 | /** |
| 83 | * Host-independent implementation: the URL and a per-host token are bound |
| 84 | * as environment values, so it works on any Platform host. |
| 85 | */ |
| 86 | export const ConnectHttp = Layer.effect( |
| 87 | Connect, |
| 88 | Effect.gen(function* () { |
| 89 | const Token = yield* DatabaseToken; |
| 90 | return Effect.fn(function* (database: Database, options?: ConnectOptions) { |
| 91 | const host = yield* Binding.Host; |
| 92 | const authorization = options?.authorization ?? "full-access"; |
| 93 | const token = yield* Token( |
| 94 | `${host?.LogicalId ?? ""}${database.LogicalId}${authorization === "read-only" ? "ReadOnly" : ""}Token`, |
| 95 | { database: database.name, authorization }, |
| 96 | ); |
| 97 | return { |
| 98 | url: yield* database.url, |
| 99 | authToken: yield* token.token, |
| 100 | } satisfies ConnectClient; |
| 101 | }); |
| 102 | }), |
| 103 | ); |