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 hyperdrive from "@distilled.cloud/cloudflare/hyperdrive"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 8 | import type { DevOrigin } from "./Connection.ts"; |
| 9 | |
| 10 | export type ConnectionLookupProps = ( |
| 11 | | { |
| 12 | /** |
| 13 | * The Cloudflare-assigned ID of the existing Hyperdrive configuration. |
| 14 | */ |
| 15 | hyperdriveId: string; |
| 16 | } |
| 17 | | { |
| 18 | /** |
| 19 | * The name of the existing Hyperdrive configuration. |
| 20 | */ |
| 21 | name: string; |
| 22 | } |
| 23 | ) & { |
| 24 | /** |
| 25 | * Local development origin. The Cloudflare API never returns the origin |
| 26 | * password of an existing configuration, so `alchemy dev` can only |
| 27 | * emulate the binding when a `dev` origin is set. Without it, a locally |
| 28 | * running Worker that binds the lookup fails with |
| 29 | * `HyperdriveDevOriginUnsupported`. |
| 30 | */ |
| 31 | dev?: DevOrigin | Output.Output<DevOrigin>; |
| 32 | }; |
| 33 | |
| 34 | const ConnectionLookupTypeId = Symbol.for("alchemy/Cloudflare/Hyperdrive/ConnectionLookup"); |
| 35 | |
| 36 | /** |
| 37 | * The value of a {@link lookup}: a binding descriptor branded with the |
| 38 | * Hyperdrive resource `Type`, so Worker binding classification and |
| 39 | * {@link Connect} treat it exactly like a managed {@link Connection}. |
| 40 | */ |
| 41 | export interface ConnectionLookup { |
| 42 | readonly [ConnectionLookupTypeId]: true; |
| 43 | readonly Type: "Cloudflare.Hyperdrive"; |
| 44 | /** |
| 45 | * The id passed to {@link lookup}; {@link Connect} uses it as the Worker |
| 46 | * binding name. |
| 47 | */ |
| 48 | readonly LogicalId: string; |
| 49 | /** Cloudflare-assigned ID of the configuration. */ |
| 50 | readonly hyperdriveId: Output.Output<string>; |
| 51 | /** Name of the configuration. */ |
| 52 | readonly name: Output.Output<string>; |
| 53 | /** Account the configuration lives in. */ |
| 54 | readonly accountId: Output.Output<string>; |
| 55 | /** The `dev` origin passed to {@link lookup}. */ |
| 56 | readonly dev: Output.Output<DevOrigin | undefined>; |
| 57 | } |
| 58 | |
| 59 | export const isConnectionLookup = (value: unknown): value is ConnectionLookup => |
| 60 | typeof value === "object" && value !== null && ConnectionLookupTypeId in value; |
| 61 | |
| 62 | /** |
| 63 | * No Hyperdrive configuration with the looked-up name exists in the account. |
| 64 | */ |
| 65 | export class HyperdriveConfigLookupNotFound extends Data.TaggedError( |
| 66 | "HyperdriveConfigLookupNotFound", |
| 67 | )<{ |
| 68 | message: string; |
| 69 | configName: string; |
| 70 | accountId: string; |
| 71 | }> {} |
| 72 | |
| 73 | /** |
| 74 | * Look up an existing Cloudflare Hyperdrive configuration (created in the |
| 75 | * dashboard or owned by another system) without managing its lifecycle — |
| 76 | * the data-source form (what Terraform calls a data source and Pulumi an |
| 77 | * invoke). The configuration is read by `hyperdriveId` or `name` during |
| 78 | * plan/deploy and is never created, updated or deleted: there is no state |
| 79 | * row, so `destroy` and `nuke` cannot touch it. Bind it to a Worker through |
| 80 | * `env` or {@link Connect}, exactly like a {@link Connection}. |
| 81 | * |
| 82 | * The `id` names the binding inside the Worker, so it must be the same on |
| 83 | * every deploy; the `name` or `hyperdriveId` may differ per stage. |
| 84 | * |
| 85 | * ### Looking up a configuration |
| 86 | * **Example:** By ID |
| 87 | * ```typescript |
| 88 | * const db = Cloudflare.Hyperdrive.lookup("SharedDb", { |
| 89 | * hyperdriveId: "a76a99bc342644deb02c38d66082262a", |
| 90 | * }); |
| 91 | * ``` |
| 92 | * |
| 93 | * **Example:** By name |
| 94 | * ```typescript |
| 95 | * const db = Cloudflare.Hyperdrive.lookup("SharedDb", { |
| 96 | * name: "shared-postgres", |
| 97 | * }); |
| 98 | * ``` |
| 99 | * |
| 100 | * ### Binding to a Worker |
| 101 | * **Example:** Async Worker `env` |
| 102 | * ```typescript |
| 103 | * const worker = yield* Cloudflare.Worker("Worker", { |
| 104 | * main: "./src/worker.ts", |
| 105 | * env: { |
| 106 | * HD: Cloudflare.Hyperdrive.lookup("SharedDb", { name: "shared-postgres" }), |
| 107 | * }, |
| 108 | * }); |
| 109 | * ``` |
| 110 | * |
| 111 | * **Example:** Effect Worker |
| 112 | * ```typescript |
| 113 | * const hd = yield* Cloudflare.Hyperdrive.Connect( |
| 114 | * Cloudflare.Hyperdrive.lookup("SharedDb", { name: "shared-postgres" }), |
| 115 | * ); |
| 116 | * const url = yield* hd.connectionString; |
| 117 | * ``` |
| 118 | * |
| 119 | * ### Local development |
| 120 | * **Example:** Dev origin for `alchemy dev` |
| 121 | * ```typescript |
| 122 | * const db = Cloudflare.Hyperdrive.lookup("SharedDb", { |
| 123 | * name: "shared-postgres", |
| 124 | * dev: { |
| 125 | * scheme: "postgres", |
| 126 | * host: "localhost", |
| 127 | * port: 5432, |
| 128 | * database: "app", |
| 129 | * user: "postgres", |
| 130 | * password: yield* Config.redacted("DEV_DB_PASSWORD"), |
| 131 | * }, |
| 132 | * }); |
| 133 | * ``` |
| 134 | * |
| 135 | * @resource |
| 136 | * @product Hyperdrive |
| 137 | * @category Storage & Databases |
| 138 | */ |
| 139 | export const lookup = (id: string, props: ConnectionLookupProps): ConnectionLookup => { |
| 140 | const config = Output.fromEffect( |
| 141 | Effect.gen(function* () { |
| 142 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 143 | if ("hyperdriveId" in props) { |
| 144 | const config = yield* hyperdrive.getConfig({ |
| 145 | accountId, |
| 146 | hyperdriveId: props.hyperdriveId, |
| 147 | }); |
| 148 | return { hyperdriveId: config.id, name: config.name, accountId }; |
| 149 | } |
| 150 | const match = yield* hyperdrive.listConfigs.items({ accountId }).pipe( |
| 151 | Stream.filter((c) => c.name === props.name), |
| 152 | Stream.runHead, |
| 153 | Effect.map(Option.getOrUndefined), |
| 154 | ); |
| 155 | if (!match) { |
| 156 | return yield* Effect.die( |
| 157 | new HyperdriveConfigLookupNotFound({ |
| 158 | configName: props.name, |
| 159 | accountId, |
| 160 | message: `Hyperdrive configuration "${props.name}" not found in account ${accountId}`, |
| 161 | }), |
| 162 | ); |
| 163 | } |
| 164 | return { hyperdriveId: match.id, name: match.name, accountId }; |
| 165 | }).pipe(Effect.orDie), |
| 166 | ); |
| 167 | return { |
| 168 | [ConnectionLookupTypeId]: true, |
| 169 | Type: "Cloudflare.Hyperdrive", |
| 170 | LogicalId: id, |
| 171 | hyperdriveId: Output.map(config, (c) => c.hyperdriveId), |
| 172 | name: Output.map(config, (c) => c.name), |
| 173 | accountId: Output.map(config, (c) => c.accountId), |
| 174 | dev: Output.asOutput<DevOrigin | undefined>(props.dev), |
| 175 | }; |
| 176 | }; |