| 1 | import type * as bigquery from "@distilled.cloud/gcp/bigquery_v2"; |
| 2 | import * as Data from "effect/Data"; |
| 3 | import type * as Effect from "effect/Effect"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import type { RuntimeContext } from "../../RuntimeContext.ts"; |
| 6 | import type { Table } from "./Table.ts"; |
| 7 | |
| 8 | /** Per-row errors from a `tabledata.insertAll` request. */ |
| 9 | export class InsertRowsFailed extends Data.TaggedError( |
| 10 | "GCP.BigQuery.InsertRowsFailed", |
| 11 | )<{ |
| 12 | insertErrors: ReadonlyArray<{ |
| 13 | /** Index of the rejected row in the request. */ |
| 14 | index: number | undefined; |
| 15 | errors: ReadonlyArray<bigquery.ErrorProto>; |
| 16 | }>; |
| 17 | }> {} |
| 18 | |
| 19 | export interface InsertRowsOptions { |
| 20 | /** |
| 21 | * Per-row `insertId`s for best-effort de-duplication; must match `rows` |
| 22 | * in length. |
| 23 | */ |
| 24 | insertIds?: string[]; |
| 25 | } |
| 26 | |
| 27 | /** Write-only client for one BigQuery table. */ |
| 28 | export interface WriteTableClient { |
| 29 | /** |
| 30 | * Stream rows into the table (`tabledata.insertAll`). Values are plain |
| 31 | * JavaScript (`Date` → timestamp, `Uint8Array` → bytes, `bigint` → |
| 32 | * integer string). Fails with {@link InsertRowsFailed} when any row is |
| 33 | * rejected. |
| 34 | */ |
| 35 | insert( |
| 36 | rows: ReadonlyArray<Record<string, unknown>>, |
| 37 | options?: InsertRowsOptions, |
| 38 | ): Effect.Effect< |
| 39 | void, |
| 40 | bigquery.InsertAllTabledataError | InsertRowsFailed, |
| 41 | RuntimeContext |
| 42 | >; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Write access to a BigQuery {@link Table}: streaming `insert`. Grants |
| 47 | * `roles/bigquery.dataEditor` on the table only. |
| 48 | * |
| 49 | * ### Writing rows |
| 50 | * **Example:** Stream rows with de-duplication ids |
| 51 | * ```typescript |
| 52 | * const events = yield* GCP.BigQuery.WriteTable(table); |
| 53 | * yield* events.insert( |
| 54 | * [{ id: "a", score: 1, at: new Date() }], |
| 55 | * { insertIds: ["a"] }, |
| 56 | * ); |
| 57 | * // …provided with Effect.provide(GCP.BigQuery.WriteTableHttp) |
| 58 | * ``` |
| 59 | * |
| 60 | * @binding |
| 61 | * @category BigQuery |
| 62 | */ |
| 63 | export interface WriteTable extends Binding.Service< |
| 64 | WriteTable, |
| 65 | "GCP.BigQuery.WriteTable", |
| 66 | (table: Table) => Effect.Effect<WriteTableClient> |
| 67 | > {} |
| 68 | |
| 69 | export const WriteTable = Binding.Service<WriteTable>( |
| 70 | "GCP.BigQuery.WriteTable", |
| 71 | ); |