| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { serveProbes } from "../../bindingHost.ts"; |
| 4 | |
| 5 | /** Dataset the Query binding reads (roles/bigquery.dataViewer on it). */ |
| 6 | export const Analytics = GCP.BigQuery.Dataset("Analytics", { |
| 7 | location: "US-CENTRAL1", |
| 8 | forceDestroy: true, |
| 9 | }); |
| 10 | |
| 11 | const table = (id: string, tableId: string) => |
| 12 | Effect.gen(function* () { |
| 13 | const dataset = yield* Analytics; |
| 14 | return yield* GCP.BigQuery.Table(id, { |
| 15 | datasetId: dataset.datasetId, |
| 16 | tableId, |
| 17 | schema: [{ name: "id", type: "STRING" }], |
| 18 | }); |
| 19 | }); |
| 20 | |
| 21 | /** Seeded by the test; read by Query through the dataset grant. */ |
| 22 | export const Queried = table("Queried", "queried"); |
| 23 | /** Seeded by the test; read by ListTabledata. */ |
| 24 | export const Listed = table("Listed", "listed"); |
| 25 | /** Written by InsertAll. */ |
| 26 | export const Inserted = table("Inserted", "inserted"); |
| 27 | /** Seeded by the test; read by ReadTable. */ |
| 28 | export const ReadRows = table("ReadRows", "read_rows"); |
| 29 | /** Written by WriteTable. */ |
| 30 | export const Written = table("Written", "written"); |
| 31 | /** Written and read back by ReadWriteTable. */ |
| 32 | export const ReadWrite = table("ReadWrite", "read_write"); |
| 33 | |
| 34 | /** |
| 35 | * Effect-native Cloud Run service exercising every BigQuery binding as its |
| 36 | * own runtime service account. Deployed from {@link ../Bindings.test.ts}. |
| 37 | */ |
| 38 | export default class BigQueryBindingsHost extends GCP.Function<BigQueryBindingsHost>()( |
| 39 | "BigQueryBindingsHost", |
| 40 | { main: import.meta.url, invokerIamDisabled: true }, |
| 41 | Effect.gen(function* () { |
| 42 | const query = yield* GCP.BigQuery.Query(Analytics); |
| 43 | const insertAll = yield* GCP.BigQuery.InsertAll(Inserted); |
| 44 | const listRows = yield* GCP.BigQuery.ListTabledata(Listed); |
| 45 | const read = yield* GCP.BigQuery.ReadTable(ReadRows); |
| 46 | const write = yield* GCP.BigQuery.WriteTable(Written); |
| 47 | const readWrite = yield* GCP.BigQuery.ReadWriteTable(ReadWrite); |
| 48 | |
| 49 | return { |
| 50 | fetch: serveProbes({ |
| 51 | query: query({ query: "SELECT id FROM queried" }), |
| 52 | insertAll: insertAll({ |
| 53 | body: { |
| 54 | rows: [{ insertId: "insert-all", json: { id: "insert-all" } }], |
| 55 | }, |
| 56 | }), |
| 57 | listTabledata: listRows({ maxResults: 10 }), |
| 58 | readTable: Effect.gen(function* () { |
| 59 | const page = yield* read.list({ maxResults: 10 }); |
| 60 | const queried = yield* read.query( |
| 61 | "SELECT id FROM read_rows WHERE id = @id", |
| 62 | { id: "seed" }, |
| 63 | ); |
| 64 | return { listed: page.rows, queried }; |
| 65 | }), |
| 66 | writeTable: write.insert([{ id: "written" }], { |
| 67 | insertIds: ["written"], |
| 68 | }), |
| 69 | readWriteTable: Effect.gen(function* () { |
| 70 | yield* readWrite.insert([{ id: "read-write" }], { |
| 71 | insertIds: ["read-write"], |
| 72 | }); |
| 73 | return yield* readWrite.query("SELECT id FROM read_write"); |
| 74 | }), |
| 75 | }), |
| 76 | }; |
| 77 | }).pipe( |
| 78 | Effect.provide(GCP.BigQuery.QueryHttp), |
| 79 | Effect.provide(GCP.BigQuery.InsertAllHttp), |
| 80 | Effect.provide(GCP.BigQuery.ListTabledataHttp), |
| 81 | Effect.provide(GCP.BigQuery.ReadTableHttp), |
| 82 | Effect.provide(GCP.BigQuery.WriteTableHttp), |
| 83 | Effect.provide(GCP.BigQuery.ReadWriteTableHttp), |
| 84 | ), |
| 85 | ) {} |