| 1 | import type * as runtime from "@cloudflare/workers-types"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Redacted from "effect/Redacted"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import type { RuntimeContext } from "../../RuntimeContext.ts"; |
| 6 | import type { Connection } from "./Connection.ts"; |
| 7 | |
| 8 | /** |
| 9 | * A typed accessor for a Cloudflare Hyperdrive runtime binding inside a |
| 10 | * Worker. Provides the same shape as the raw `Hyperdrive` runtime object |
| 11 | * (connection string, host, port, user, password, database) plus a `raw` |
| 12 | * escape hatch for libraries that want direct access. |
| 13 | * |
| 14 | * **Example:** Bind Hyperdrive in a Worker |
| 15 | * ```typescript |
| 16 | * const hd = yield* Cloudflare.Hyperdrive.Connect(MyConnection); |
| 17 | * const url = yield* hd.connectionString; |
| 18 | * ``` |
| 19 | * |
| 20 | * @binding |
| 21 | * @product Hyperdrive |
| 22 | * @category Storage & Databases |
| 23 | */ |
| 24 | /** |
| 25 | * Bind a {@link Connection} to a Worker and obtain the Effect-native |
| 26 | * Hyperdrive client (connection string, host, port, …). |
| 27 | * |
| 28 | * `Connect` is a single identifier that is simultaneously the binding's Context |
| 29 | * tag, its type, and the callable — `yield* Cloudflare.Hyperdrive.Connect(conn)`. |
| 30 | * |
| 31 | * **Example:** Using Hyperdrive inside a Worker |
| 32 | * ```typescript |
| 33 | * const hd = yield* Cloudflare.Hyperdrive.Connect(MyConnection); |
| 34 | * const url = yield* hd.connectionString; |
| 35 | * ``` |
| 36 | * |
| 37 | * @binding |
| 38 | * @product Hyperdrive |
| 39 | * @category Storage & Databases |
| 40 | */ |
| 41 | export interface Connect extends Binding.Service< |
| 42 | Connect, |
| 43 | "Cloudflare.Hyperdrive.Connect", |
| 44 | (connection: Connection) => Effect.Effect<ConnectClient> |
| 45 | > {} |
| 46 | |
| 47 | export const Connect = Binding.Service<Connect>( |
| 48 | "Cloudflare.Hyperdrive.Connect", |
| 49 | ); |
| 50 | |
| 51 | export interface ConnectClient { |
| 52 | /** |
| 53 | * The raw runtime `Hyperdrive` binding. Use this when integrating with a |
| 54 | * driver that wants direct access to the Cloudflare object. |
| 55 | */ |
| 56 | raw: Effect.Effect<runtime.Hyperdrive, never, RuntimeContext>; |
| 57 | /** |
| 58 | * A valid DB connection string for use with a driver/ORM. |
| 59 | */ |
| 60 | connectionString: Effect.Effect< |
| 61 | Redacted.Redacted<string>, |
| 62 | never, |
| 63 | RuntimeContext |
| 64 | >; |
| 65 | /** |
| 66 | * Hostname valid only within the current Worker invocation. |
| 67 | */ |
| 68 | host: Effect.Effect<string, never, RuntimeContext>; |
| 69 | /** |
| 70 | * Port to pair with `host`. |
| 71 | */ |
| 72 | port: Effect.Effect<number, never, RuntimeContext>; |
| 73 | /** |
| 74 | * Database user. |
| 75 | */ |
| 76 | user: Effect.Effect<string, never, RuntimeContext>; |
| 77 | /** |
| 78 | * Randomly generated password valid only within the current Worker |
| 79 | * invocation. |
| 80 | */ |
| 81 | password: Effect.Effect<Redacted.Redacted<string>, never, RuntimeContext>; |
| 82 | /** |
| 83 | * Database name. |
| 84 | */ |
| 85 | database: Effect.Effect<string, never, RuntimeContext>; |
| 86 | } |