| 1 | import * as spanner from "@distilled.cloud/gcp/spanner_v1"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import type { Database } from "./Database.ts"; |
| 5 | import { GetDdl, type GetDdlRequest } from "./GetDdl.ts"; |
| 6 | import { bindGcpHost } from "../Host.ts"; |
| 7 | import { grantFor } from "../HttpBinding.ts"; |
| 8 | |
| 9 | /** |
| 10 | * HTTP implementation of {@link GetDdl}. |
| 11 | * |
| 12 | * @layer |
| 13 | * @provides GCP.Spanner.GetDdl |
| 14 | */ |
| 15 | export const GetDdlHttp = Layer.effect( |
| 16 | GetDdl, |
| 17 | Effect.gen(function* () { |
| 18 | const getDdlProjectsInstancesDatabases = |
| 19 | yield* spanner.getDdlProjectsInstancesDatabases; |
| 20 | return Effect.fn(function* (database: Database) { |
| 21 | yield* bindGcpHost({ |
| 22 | tag: "GCP.Spanner.GetDdl", |
| 23 | resource: database, |
| 24 | iam: [ |
| 25 | grantFor( |
| 26 | { role: "roles/spanner.databaseReader", on: "spanner.database" }, |
| 27 | database.name, |
| 28 | ), |
| 29 | ], |
| 30 | }); |
| 31 | const name = yield* database.name; |
| 32 | return Effect.fn(`GCP.Spanner.GetDdl(${database.LogicalId})`)(function* ( |
| 33 | request?: GetDdlRequest, |
| 34 | ) { |
| 35 | return yield* getDdlProjectsInstancesDatabases({ |
| 36 | ...request, |
| 37 | database: yield* name, |
| 38 | }); |
| 39 | }); |
| 40 | }); |
| 41 | }), |
| 42 | ); |