| 1 | import * as Cloudflare from "@/Cloudflare/index.ts"; |
| 2 | import * as Planetscale from "@/Planetscale/index.ts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import path from "node:path"; |
| 5 | import { fileURLToPath } from "node:url"; |
| 6 | |
| 7 | /** |
| 8 | * Shared Planetscale + Cloudflare wiring used by the Hyperdrive |
| 9 | * fixture worker. A long-lived staging Postgres database (named |
| 10 | * deterministically so reruns adopt the same resource) owns a feature |
| 11 | * branch + role. The live Hyperdrive config points at the direct |
| 12 | * `role.origin`; local dev bypasses Hyperdrive, so it uses |
| 13 | * `role.pooledOrigin` to avoid one direct connection per worker request. |
| 14 | */ |
| 15 | // This module is bundled into the worker (hyperdrive-worker.ts imports it), |
| 16 | // so this also evaluates at worker startup, where the bundler leaves |
| 17 | // `import.meta.url` undefined. The fallback is never read there — resource |
| 18 | // props are only consumed at deploy time. |
| 19 | const migrationsDir = import.meta.url |
| 20 | ? path.join(fileURLToPath(import.meta.url), "..", "migrations") |
| 21 | : "."; |
| 22 | |
| 23 | export const PlanetscaleDb = Effect.gen(function* () { |
| 24 | const database = yield* Planetscale.PostgresDatabase("HyperdriveTestDb", { |
| 25 | name: "alchemy-postgres-hyperdrive", |
| 26 | region: { slug: "us-east" }, |
| 27 | clusterSize: "PS_10", |
| 28 | }); |
| 29 | |
| 30 | const branch = yield* Planetscale.PostgresBranch("HyperdriveTestBranch", { |
| 31 | database, |
| 32 | migrations: migrationsDir, |
| 33 | }); |
| 34 | |
| 35 | const role = yield* Planetscale.PostgresRole("HyperdriveTestRole", { |
| 36 | database, |
| 37 | branch, |
| 38 | inheritedRoles: ["postgres"], |
| 39 | }); |
| 40 | |
| 41 | return { database, branch, role }; |
| 42 | }); |
| 43 | |
| 44 | export const Hyperdrive = Effect.gen(function* () { |
| 45 | const { role } = yield* PlanetscaleDb; |
| 46 | return yield* Cloudflare.Hyperdrive.Connection("HyperdriveTestEdge", { |
| 47 | origin: role.origin, |
| 48 | dev: role.pooledOrigin, |
| 49 | // The test asserts read-your-writes across separate HTTP requests; |
| 50 | // Hyperdrive's query cache (60s default) would serve stale SELECTs. |
| 51 | caching: { disabled: true }, |
| 52 | }); |
| 53 | }); |