@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, 06:00 UTC

main
ts 463 lines 15.5 KB
Raw
1 import * as bigtable from "@distilled.cloud/gcp/bigtableadmin_v2";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 instanceNameOf,
13 listAlchemyInstances,
14 MAX_TABLE_ID_LENGTH,
15 parentOwned,
16 parseResourceName,
17 toPhysicalId,
18 waitForOperation,
19 collectPages,
20 } from "./operations.ts";
21
22 export type GcRule = {
23 /** Keep only the most recent N versions of each cell. */
24 maxNumVersions?: number;
25 /** Delete cells older than this duration (e.g. `"2592000s"`). */
26 maxAge?: string;
27 };
28
29 export type ColumnFamily = {
30 /** Garbage-collection rule for this family. */
31 gcRule?: GcRule;
32 };
33
34 export type ChangeStreamConfig = {
35 /**
36 * How long change-stream data is retained (`1d`–`7d`, e.g. `"86400s"`).
37 */
38 retentionPeriod?: string;
39 };
40
41 export type AutomatedBackupPolicy = {
42 /** How long automated backups are retained (`3d`–`90d`). */
43 retentionPeriod?: string;
44 /** Backup frequency. Only `"24h"` is supported. */
45 frequency?: string;
46 };
47
48 export type TableProps = {
49 /**
50 * Parent instance. Full name `projects/{project}/instances/{instance}`
51 * or the instance id. Immutable — changing it replaces the table.
52 */
53 instance: string;
54 /**
55 * Table id (the `{table}` segment of
56 * `.../instances/{instance}/tables/{table}`). If omitted, a unique name
57 * is generated from the stack, stage, and logical id. Must be 1-50
58 * characters. Immutable — changing it replaces the table.
59 */
60 tableId?: string;
61 /**
62 * Column families keyed by family id. Omitted on update leaves families
63 * unchanged; a provided map is reconciled (create / update / drop).
64 */
65 columnFamilies?: Record<string, ColumnFamily>;
66 /**
67 * Protect the table (and its instance) from Admin API deletes.
68 * Disabled automatically on destroy.
69 * @default false
70 */
71 deletionProtection?: boolean;
72 /**
73 * Timestamp granularity. Immutable.
74 * @default "MILLIS"
75 */
76 granularity?: bigtable.TableGranularityEnum | (string & {});
77 /**
78 * Enable a change stream on this table.
79 */
80 changeStreamConfig?: ChangeStreamConfig;
81 /**
82 * Automated backup policy. Omit to disable.
83 */
84 automatedBackupPolicy?: AutomatedBackupPolicy;
85 };
86
87 export type Table = Resource<
88 "GCP.Bigtable.Table",
89 TableProps,
90 {
91 /** Full resource name `projects/{project}/instances/{instance}/tables/{table}`. */
92 name: string;
93 /** Table id (last path segment). */
94 tableId: string;
95 /** Parent instance resource name. */
96 instance: string;
97 /** Instance id. */
98 instanceId: string;
99 /** Project id. */
100 project: string;
101 /** Column families currently configured. */
102 columnFamilies: Record<string, ColumnFamily>;
103 /** Whether Admin API deletes are blocked. */
104 deletionProtection: boolean;
105 /** Timestamp granularity. */
106 granularity: string | undefined;
107 /** Change-stream config, if enabled. */
108 changeStreamConfig: ChangeStreamConfig | undefined;
109 /** Automated backup policy, if enabled. */
110 automatedBackupPolicy: AutomatedBackupPolicy | undefined;
111 /** Restore info, if this table was restored from a backup. */
112 restoreInfo: bigtable.RestoreInfo | undefined;
113 },
114 never,
115 Providers
116 >;
117
118 /**
119 * A Cloud Bigtable table — rows keyed by row key, grouped into column
120 * families.
121 *
122 * The parent instance must already exist. Changing `tableId`,
123 * `instance`, or `granularity` replaces the table. Column families,
124 * deletion protection, change streams, and automated backups update in
125 * place. Row read/write is a data-plane API; use {@link GetTable} for
126 * admin metadata.
127 *
128 * Tables have no labels field. Alchemy treats a table as owned when its
129 * parent instance carries Alchemy labels, so `list` / `pnpm nuke:gcp`
130 * can find it.
131 *
132 * ### Creating a Table
133 * **Example:** Table with one column family
134 * ```typescript
135 * const instance = yield* GCP.Bigtable.Instance("Data", {});
136 * const table = yield* GCP.Bigtable.Table("Users", {
137 * instance: instance.name,
138 * columnFamilies: {
139 * cf: { gcRule: { maxNumVersions: 3 } },
140 * },
141 * });
142 * ```
143 *
144 * ### Reading Table Metadata
145 * **Example:** Get the bound table
146 * ```typescript
147 * const getTable = yield* GCP.Bigtable.GetTable(table);
148 * const live = yield* getTable({ view: "SCHEMA_VIEW" });
149 * ```
150 *
151 * @resource
152 * @category Bigtable
153 */
154 export const Table = Resource<Table>("GCP.Bigtable.Table");
155
156 export class TableNotResolved extends Data.TaggedError("GCP.Bigtable.TableNotResolved")<{
157 resourceName: string;
158 }> {}
159
160 export class TableStillExists extends Data.TaggedError("GCP.Bigtable.TableStillExists")<{
161 resourceName: string;
162 }> {}
163
164 const DEFAULT_GRANULARITY = "MILLIS";
165 const SCHEMA_VIEW = "SCHEMA_VIEW";
166
167 const normalizeGranularity = (value: string | undefined) => {
168 const next = (value ?? DEFAULT_GRANULARITY).toUpperCase();
169 return next === "TIMESTAMP_GRANULARITY_UNSPECIFIED" ? DEFAULT_GRANULARITY : next;
170 };
171
172 const gcKey = (rule: GcRule | bigtable.GcRule | undefined) =>
173 JSON.stringify({
174 maxNumVersions: rule?.maxNumVersions ?? 0,
175 maxAge: rule?.maxAge ?? "",
176 });
177
178 const familiesOf = (
179 families: Record<string, ColumnFamily | bigtable.ColumnFamily | undefined> | null | undefined,
180 ): Record<string, ColumnFamily> => {
181 const result: Record<string, ColumnFamily> = {};
182 for (const [id, family] of Object.entries(families ?? {})) {
183 if (family === undefined) continue;
184 result[id] = { gcRule: family.gcRule };
185 }
186 return result;
187 };
188
189 const toId = (id: string, tableId: string | undefined, existing?: string) =>
190 toPhysicalId(id, tableId, existing, MAX_TABLE_ID_LENGTH);
191
192 const toAttrs = (table: bigtable.Table, project: string) => {
193 const name = table.name ?? "";
194 const parsed = parseResourceName(name);
195 return {
196 name,
197 tableId: parsed.tableId,
198 instance: parsed.instance,
199 instanceId: parsed.instanceId,
200 project: parsed.project || project,
201 columnFamilies: familiesOf(table.columnFamilies),
202 deletionProtection: table.deletionProtection === true,
203 granularity: table.granularity,
204 changeStreamConfig: table.changeStreamConfig,
205 automatedBackupPolicy: table.automatedBackupPolicy,
206 restoreInfo: table.restoreInfo,
207 };
208 };
209
210 const getByName = (name: string) =>
211 bigtable
212 .getProjectsInstancesTables({ name, view: SCHEMA_VIEW })
213 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
214
215 const waitUntilGone = (name: string) =>
216 getByName(name).pipe(
217 Effect.flatMap((table) =>
218 table === undefined ? Effect.void : Effect.fail(new TableStillExists({ resourceName: name })),
219 ),
220 Effect.retry({
221 while: (error) => error._tag === "GCP.Bigtable.TableStillExists",
222 times: 8,
223 schedule: Schedule.spaced("2 seconds"),
224 }),
225 );
226
227 const toFamilyBody = (family: ColumnFamily): bigtable.ColumnFamily => ({
228 gcRule: family.gcRule,
229 });
230
231 const changeStreamKey = (config: ChangeStreamConfig | undefined) =>
232 JSON.stringify({ retention: config?.retentionPeriod ?? "" });
233
234 const backupKey = (policy: AutomatedBackupPolicy | undefined) =>
235 JSON.stringify({
236 retention: policy?.retentionPeriod ?? "",
237 frequency: policy?.frequency ?? "",
238 });
239
240 const familyMods = (
241 observed: Record<string, ColumnFamily>,
242 desired: Record<string, ColumnFamily>,
243 ): bigtable.Modification[] => {
244 const mods: bigtable.Modification[] = [];
245 for (const [id, family] of Object.entries(desired)) {
246 if (observed[id] === undefined) {
247 mods.push({ id, create: toFamilyBody(family) });
248 } else if (gcKey(observed[id]?.gcRule) !== gcKey(family.gcRule)) {
249 mods.push({
250 id,
251 update: toFamilyBody(family),
252 updateMask: "gc_rule",
253 });
254 }
255 }
256 for (const id of Object.keys(observed)) {
257 if (desired[id] === undefined) {
258 mods.push({ id, drop: true });
259 }
260 }
261 return mods;
262 };
263
264 export const TableProvider = () =>
265 Provider.succeed(Table, {
266 stables: ["name", "tableId", "instance", "instanceId", "project"],
267
268 diff: Effect.fn(function* ({ news, olds, output }) {
269 if (!isResolved(news)) return undefined;
270 const previousId = olds?.tableId ?? output?.tableId;
271 const nextId = news.tableId ?? previousId;
272 const previousInstance = olds?.instance ?? output?.instance;
273 const previousInstanceId = previousInstance
274 ? parseResourceName(
275 previousInstance.includes("/instances/")
276 ? previousInstance
277 : `projects/_/instances/${previousInstance}`,
278 ).instanceId
279 : output?.instanceId;
280 const nextInstanceId = parseResourceName(
281 news.instance.includes("/instances/")
282 ? news.instance
283 : `projects/_/instances/${news.instance}`,
284 ).instanceId;
285 const previousGranularity = normalizeGranularity(olds?.granularity ?? output?.granularity);
286 const nextGranularity = normalizeGranularity(news.granularity ?? output?.granularity);
287 if (
288 (previousId !== undefined && nextId !== undefined && previousId !== nextId) ||
289 (previousInstanceId !== undefined && previousInstanceId !== nextInstanceId) ||
290 (news.granularity !== undefined && previousGranularity !== nextGranularity)
291 ) {
292 return { action: "replace" as const };
293 }
294 return undefined;
295 }),
296
297 read: Effect.fn(function* ({ id, olds, output }) {
298 const env = yield* GcpEnvironment.current;
299 const tableId = yield* toId(id, olds?.tableId, output?.tableId);
300 const instanceRef = olds?.instance ?? output?.instance;
301 const name =
302 output?.name ??
303 (instanceRef ? `${instanceNameOf(env.project, instanceRef)}/tables/${tableId}` : undefined);
304 if (name === undefined) return undefined;
305 const existing = yield* getByName(name);
306 if (existing === undefined) return undefined;
307 const attrs = toAttrs(existing, env.project);
308 return (yield* parentOwned(attrs.instance)) ? attrs : Unowned(attrs);
309 }),
310
311 list: () =>
312 Effect.gen(function* () {
313 const env = yield* GcpEnvironment.current;
314 const instances = (yield* listAlchemyInstances(env.project)).filter(
315 (instance): instance is bigtable.Instance & { name: string } => !!instance.name,
316 );
317 const pages = yield* Effect.forEach(
318 instances,
319 (instance) =>
320 collectPages(
321 bigtable.listProjectsInstancesTables.pages({
322 parent: instance.name,
323 pageSize: 1000,
324 }),
325 (page) => page.tables,
326 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([] as bigtable.Table[]))),
327 { concurrency: 4 },
328 );
329 return pages.flat().map((table) => toAttrs(table, env.project));
330 }),
331
332 reconcile: Effect.fn(function* ({ id, news, output }) {
333 const env = yield* GcpEnvironment.current;
334 const tableId = yield* toId(id, news.tableId, output?.tableId);
335 const parent = instanceNameOf(env.project, news.instance);
336 const name = `${parent}/tables/${tableId}`;
337 const desiredProtection = news.deletionProtection === true;
338 const families = news.columnFamilies;
339
340 let current = yield* getByName(name);
341
342 if (current === undefined) {
343 const created = yield* bigtable
344 .createProjectsInstancesTables({
345 parent,
346 body: {
347 tableId,
348 table: {
349 granularity: news.granularity ? normalizeGranularity(news.granularity) : undefined,
350 columnFamilies:
351 families === undefined
352 ? undefined
353 : Object.fromEntries(
354 Object.entries(families).map(([familyId, family]) => [
355 familyId,
356 toFamilyBody(family),
357 ]),
358 ),
359 deletionProtection: desiredProtection,
360 changeStreamConfig: news.changeStreamConfig,
361 automatedBackupPolicy: news.automatedBackupPolicy,
362 },
363 },
364 })
365 .pipe(
366 Effect.catchTag("Conflict", (error) =>
367 getByName(name).pipe(
368 Effect.flatMap((existing) =>
369 existing ? Effect.succeed(existing) : Effect.fail(error),
370 ),
371 ),
372 ),
373 Effect.retry({
374 while: (error) => error._tag === "Conflict",
375 times: 8,
376 schedule: Schedule.spaced("2 seconds"),
377 }),
378 );
379 current = created ?? undefined;
380 }
381
382 if (current === undefined) {
383 return yield* new TableNotResolved({ resourceName: name });
384 }
385
386 if (families !== undefined) {
387 const mods = familyMods(familiesOf(current.columnFamilies), families);
388 if (mods.length > 0) {
389 if (current.deletionProtection === true) {
390 const unlocked = yield* bigtable.patchProjectsInstancesTables({
391 name,
392 updateMask: "deletion_protection",
393 body: { deletionProtection: false },
394 });
395 yield* waitForOperation(unlocked);
396 current = (yield* getByName(name)) ?? current;
397 }
398 current = yield* bigtable.modifyColumnFamiliesProjectsInstancesTables({
399 name,
400 body: { modifications: mods },
401 });
402 }
403 }
404
405 const mask: string[] = [];
406 const patchBody: bigtable.Table = {};
407 if ((current.deletionProtection === true) !== desiredProtection) {
408 patchBody.deletionProtection = desiredProtection;
409 mask.push("deletion_protection");
410 }
411 if (
412 news.changeStreamConfig !== undefined &&
413 changeStreamKey(news.changeStreamConfig) !== changeStreamKey(current.changeStreamConfig)
414 ) {
415 patchBody.changeStreamConfig = news.changeStreamConfig;
416 mask.push("change_stream_config");
417 }
418 if (
419 news.automatedBackupPolicy !== undefined &&
420 backupKey(news.automatedBackupPolicy) !== backupKey(current.automatedBackupPolicy)
421 ) {
422 patchBody.automatedBackupPolicy = news.automatedBackupPolicy;
423 mask.push("automated_backup_policy");
424 }
425
426 if (mask.length > 0) {
427 const patched = yield* bigtable.patchProjectsInstancesTables({
428 name,
429 updateMask: mask.join(","),
430 body: patchBody,
431 });
432 yield* waitForOperation(patched);
433 current = yield* getByName(name);
434 if (current === undefined) {
435 return yield* new TableNotResolved({ resourceName: name });
436 }
437 }
438
439 return toAttrs(current, env.project);
440 }),
441
442 delete: Effect.fn(function* ({ output }) {
443 const current = yield* getByName(output.name);
444 if (current === undefined) return;
445 if (current.deletionProtection === true) {
446 const patched = yield* bigtable.patchProjectsInstancesTables({
447 name: output.name,
448 updateMask: "deletion_protection",
449 body: { deletionProtection: false },
450 });
451 yield* waitForOperation(patched);
452 }
453 yield* bigtable.deleteProjectsInstancesTables({ name: output.name }).pipe(
454 Effect.catchTag("NotFound", () => Effect.void),
455 Effect.retry({
456 while: (error) => error._tag === "Conflict",
457 times: 8,
458 schedule: Schedule.spaced("2 seconds"),
459 }),
460 );
461 yield* waitUntilGone(output.name);
462 }),
463 });