| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import * as Redacted from "effect/Redacted"; |
| 4 | import * as Binding from "../Binding.ts"; |
| 5 | import * as Output from "../Output.ts"; |
| 6 | import { unpackEnvValue, type RuntimeContext } from "../RuntimeContext.ts"; |
| 7 | import { |
| 8 | ConnectMySQL, |
| 9 | MySQLUrlMissing, |
| 10 | connectEnvKeys, |
| 11 | type ConnectMySQLClient, |
| 12 | } from "./ConnectMySQL.ts"; |
| 13 | import { isRailwayHost } from "./MountVolume.ts"; |
| 14 | import { |
| 15 | MYSQL_URL_SECRET, |
| 16 | MYSQL_PUBLIC_URL_SECRET, |
| 17 | type MySQL, |
| 18 | } from "./MySQL.ts"; |
| 19 | |
| 20 | const runtimeOutput = <A>( |
| 21 | key: string, |
| 22 | output: Output.Output<A>, |
| 23 | ): Effect.Effect<A, never, RuntimeContext> => |
| 24 | output.bind(key).pipe(Effect.flatMap((effect) => effect)); |
| 25 | |
| 26 | const asRedactedUrl = ( |
| 27 | value: string, |
| 28 | name: string, |
| 29 | ): Effect.Effect<Redacted.Redacted<string>, MySQLUrlMissing> => |
| 30 | value.length > 0 |
| 31 | ? Effect.succeed(Redacted.make(value)) |
| 32 | : Effect.fail(new MySQLUrlMissing({ name })); |
| 33 | |
| 34 | const fromProcessEnv = (key: string): string => { |
| 35 | const unpacked = unpackEnvValue<unknown>(process.env[key]); |
| 36 | if (typeof unpacked === "string") return unpacked; |
| 37 | if (Redacted.isRedacted(unpacked)) { |
| 38 | const inner = Redacted.value(unpacked); |
| 39 | return typeof inner === "string" ? inner : ""; |
| 40 | } |
| 41 | return ""; |
| 42 | }; |
| 43 | |
| 44 | const firstUrl = ( |
| 45 | values: ReadonlyArray<string>, |
| 46 | name: string, |
| 47 | ): Effect.Effect<Redacted.Redacted<string>, MySQLUrlMissing> => { |
| 48 | const found = values.find((value) => value.length > 0); |
| 49 | return found !== undefined |
| 50 | ? asRedactedUrl(found, name) |
| 51 | : Effect.fail(new MySQLUrlMissing({ name })); |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * Implementation of {@link ConnectMySQL}. Provide it on the |
| 56 | * {@link Service} Effect. |
| 57 | * |
| 58 | * At deploy time this packs the private connection URI onto the host |
| 59 | * (`RAILWAY_MYSQL_*`, `MYSQL_URL`). At runtime the client reads |
| 60 | * `process.env`. |
| 61 | * |
| 62 | * |
| 63 | * ### Provide the layer |
| 64 | * **Example:** On a Service |
| 65 | * ```typescript |
| 66 | * Effect.gen(function* () { |
| 67 | * const conn = yield* Railway.ConnectMySQL(Db); |
| 68 | * const db = yield* Drizzle.MySQL(conn.connectionString); |
| 69 | * }).pipe(Effect.provide(Railway.ConnectMySQLHttp)) |
| 70 | * ``` |
| 71 | * |
| 72 | * @layer |
| 73 | * @product MySQL |
| 74 | * @provides Railway.ConnectMySQL |
| 75 | */ |
| 76 | export const ConnectMySQLHttp = Layer.effect( |
| 77 | ConnectMySQL, |
| 78 | Effect.succeed( |
| 79 | Effect.fn(function* (mysql: MySQL) { |
| 80 | const keys = connectEnvKeys(mysql); |
| 81 | const name = mysql.LogicalId; |
| 82 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 83 | const host = yield* Binding.Host; |
| 84 | if (isRailwayHost(host)) { |
| 85 | yield* host.bind`${mysql}`({ |
| 86 | env: { |
| 87 | [keys.pooled]: mysql.connectionUri, |
| 88 | [keys.direct]: mysql.connectionUri, |
| 89 | [MYSQL_URL_SECRET]: mysql.connectionUri, |
| 90 | }, |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | const fromEnv = (preferDirect: boolean) => |
| 96 | firstUrl( |
| 97 | preferDirect |
| 98 | ? [ |
| 99 | fromProcessEnv(keys.direct), |
| 100 | fromProcessEnv(MYSQL_URL_SECRET), |
| 101 | fromProcessEnv(keys.pooled), |
| 102 | fromProcessEnv(MYSQL_PUBLIC_URL_SECRET), |
| 103 | ] |
| 104 | : [ |
| 105 | fromProcessEnv(keys.pooled), |
| 106 | fromProcessEnv(MYSQL_URL_SECRET), |
| 107 | fromProcessEnv(keys.direct), |
| 108 | fromProcessEnv(MYSQL_PUBLIC_URL_SECRET), |
| 109 | ], |
| 110 | name, |
| 111 | ); |
| 112 | |
| 113 | if (globalThis.__ALCHEMY_RUNTIME__) { |
| 114 | return { |
| 115 | connectionString: fromEnv(false), |
| 116 | directConnectionString: fromEnv(true), |
| 117 | } satisfies ConnectMySQLClient; |
| 118 | } |
| 119 | |
| 120 | const pooled = runtimeOutput(keys.pooled, mysql.connectionUri); |
| 121 | const direct = runtimeOutput(keys.direct, mysql.connectionUri); |
| 122 | |
| 123 | return { |
| 124 | connectionString: Effect.gen(function* () { |
| 125 | const packed = yield* pooled; |
| 126 | const unpacked = yield* direct; |
| 127 | return yield* firstUrl( |
| 128 | [ |
| 129 | typeof packed === "string" ? packed : "", |
| 130 | fromProcessEnv(keys.pooled), |
| 131 | fromProcessEnv(MYSQL_URL_SECRET), |
| 132 | typeof unpacked === "string" ? unpacked : "", |
| 133 | fromProcessEnv(keys.direct), |
| 134 | fromProcessEnv(MYSQL_PUBLIC_URL_SECRET), |
| 135 | ], |
| 136 | name, |
| 137 | ); |
| 138 | }), |
| 139 | directConnectionString: Effect.gen(function* () { |
| 140 | const unpacked = yield* direct; |
| 141 | const packed = yield* pooled; |
| 142 | return yield* firstUrl( |
| 143 | [ |
| 144 | typeof unpacked === "string" ? unpacked : "", |
| 145 | fromProcessEnv(keys.direct), |
| 146 | fromProcessEnv(MYSQL_URL_SECRET), |
| 147 | typeof packed === "string" ? packed : "", |
| 148 | fromProcessEnv(keys.pooled), |
| 149 | fromProcessEnv(MYSQL_PUBLIC_URL_SECRET), |
| 150 | ], |
| 151 | name, |
| 152 | ); |
| 153 | }), |
| 154 | } satisfies ConnectMySQLClient; |
| 155 | }), |
| 156 | ), |
| 157 | ); |