@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 96 lines 3.13 KB
Raw
1 import type * as bigquery from "@distilled.cloud/gcp/bigquery_v2";
2 import * as Binding from "alchemy/Binding";
3 import type { RuntimeContext } from "alchemy/RuntimeContext";
4 import * as Data from "effect/Data";
5 import type * as Effect from "effect/Effect";
6 import type { Table } from "./Table.ts";
7
8 /** A query job did not finish within the client's bounded wait. */
9 export class QueryNotComplete extends Data.TaggedError("GCP.BigQuery.QueryNotComplete")<{
10 jobId: string | undefined;
11 }> {}
12
13 export interface ListTableOptions {
14 /** Maximum rows per page. */
15 maxResults?: number;
16 /** Page token from a previous `list`. */
17 pageToken?: string;
18 }
19
20 export interface ListTableResult {
21 /** Rows decoded with the table schema (see {@link ReadTable}). */
22 rows: Record<string, unknown>[];
23 /** Pass to the next `list` call; `undefined` on the last page. */
24 nextPageToken: string | undefined;
25 }
26
27 /** Read-only client for one BigQuery table. */
28 export interface ReadTableClient {
29 /**
30 * One page of table rows (`tabledata.list`), including rows still in the
31 * streaming buffer.
32 */
33 list(
34 options?: ListTableOptions,
35 ): Effect.Effect<
36 ListTableResult,
37 bigquery.GetTablesError | bigquery.ListTabledataError,
38 RuntimeContext
39 >;
40 /**
41 * Run a GoogleSQL query and return every result row. Unqualified table
42 * names resolve against the bound table's dataset; `params` become named
43 * parameters (`@name`) typed `STRING`, `INT64`/`FLOAT64`, or `BOOL`.
44 */
45 query(
46 sql: string,
47 params?: Record<string, string | number | boolean>,
48 ): Effect.Effect<
49 Record<string, unknown>[],
50 bigquery.QueryJobsError | bigquery.GetQueryResultsJobsError | QueryNotComplete,
51 RuntimeContext
52 >;
53 }
54
55 /**
56 * Read access to a BigQuery {@link Table}: `list` rows and run `query`
57 * jobs. Grants `roles/bigquery.dataViewer` on the table and
58 * `roles/bigquery.jobUser` on the project (job creation is project-only).
59 *
60 * Rows are decoded with the schema: `INTEGER` → `number` (`bigint` beyond
61 * the safe range), `FLOAT` → `number`, `BOOLEAN` → `boolean`, `TIMESTAMP` →
62 * `Date`, `BYTES` → `Uint8Array`, `JSON` → parsed value, `RECORD` → object,
63 * `REPEATED` → array; `NUMERIC`, `DATE`, `DATETIME`, `TIME`, and `GEOGRAPHY`
64 * stay strings.
65 *
66 * ### Reading rows
67 * **Example:** Page through a table
68 * ```typescript
69 * const events = yield* GCP.BigQuery.ReadTable(table);
70 * const { rows, nextPageToken } = yield* events.list({ maxResults: 100 });
71 * // …provided with Effect.provide(GCP.BigQuery.ReadTableHttp)
72 * ```
73 *
74 * ### Querying
75 * **Example:** Parameterized query
76 * ```typescript
77 * const events = yield* GCP.BigQuery.ReadTable(table);
78 * const tableId = yield* table.tableId;
79 * return Effect.gen(function* () {
80 * const rows = yield* events.query(
81 * `SELECT id, score FROM \`${yield* tableId}\` WHERE score > @min`,
82 * { min: 10 },
83 * );
84 * });
85 * ```
86 *
87 * @binding
88 * @category BigQuery
89 */
90 export interface ReadTable extends Binding.Service<
91 ReadTable,
92 "GCP.BigQuery.ReadTable",
93 (table: Table) => Effect.Effect<ReadTableClient>
94 > {}
95
96 export const ReadTable = Binding.Service<ReadTable>("GCP.BigQuery.ReadTable");