@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 563 lines 20.3 KB
Raw
1 import * as firestore from "@distilled.cloud/gcp/firestore_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
12 import type { Providers } from "../Providers.ts";
13
14 const DEFAULT_TYPE = "FIRESTORE_NATIVE";
15 const DEFAULT_EDITION = "STANDARD";
16 const DEFAULT_DELETE_PROTECTION = "DELETE_PROTECTION_DISABLED";
17 const DEFAULT_APP_ENGINE = "DISABLED";
18 const MAX_DATABASE_ID_LENGTH = 63;
19
20 export type DatabaseType = firestore.GoogleFirestoreAdminV1DatabaseTypeEnum | (string & {});
21 export type DatabaseEdition =
22 | firestore.GoogleFirestoreAdminV1DatabaseDatabaseEditionEnum
23 | (string & {});
24 export type DatabaseConcurrencyMode =
25 | firestore.GoogleFirestoreAdminV1DatabaseConcurrencyModeEnum
26 | (string & {});
27 export type DatabasePointInTimeRecoveryEnablement =
28 | firestore.GoogleFirestoreAdminV1DatabasePointInTimeRecoveryEnablementEnum
29 | (string & {});
30 export type DatabaseAppEngineIntegrationMode =
31 | firestore.GoogleFirestoreAdminV1DatabaseAppEngineIntegrationModeEnum
32 | (string & {});
33 export type DatabaseDeleteProtectionState =
34 | firestore.GoogleFirestoreAdminV1DatabaseDeleteProtectionStateEnum
35 | (string & {});
36 export type DatabaseRealtimeUpdatesMode =
37 | firestore.GoogleFirestoreAdminV1DatabaseRealtimeUpdatesModeEnum
38 | (string & {});
39 export type DatabaseDataAccessMode =
40 | firestore.GoogleFirestoreAdminV1DatabaseFirestoreDataAccessModeEnum
41 | (string & {});
42
43 export type DatabaseCmekConfig = {
44 /**
45 * Cloud KMS CryptoKey used to encrypt the database
46 * (`projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`).
47 * Must be in the same location as the database. Immutable — changing
48 * it replaces the database.
49 */
50 kmsKeyName?: string;
51 };
52
53 export type DatabaseProps = {
54 /**
55 * Database id (the `{database}` segment of
56 * `projects/{project}/databases/{database}`). If omitted, a unique name
57 * is generated from the stack, stage, and logical id. Must be 4-63
58 * characters, match `[a-z][a-z0-9-]*[a-z0-9]`, and must not look like a
59 * UUID. `"(default)"` is valid for the Standard edition default
60 * database. Immutable — changing it replaces the database.
61 */
62 databaseId?: string;
63 /**
64 * Location of the database (`us-central1`, `nam5`, `eur3`, …).
65 * Immutable — changing it replaces the database. `US-CENTRAL1` is
66 * accepted and normalized to `us-central1`.
67 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
68 */
69 location?: string;
70 /**
71 * Database type. Mode changes are only allowed when the database is
72 * empty.
73 * @default "FIRESTORE_NATIVE"
74 */
75 type?: DatabaseType;
76 /**
77 * Database edition. Immutable — changing it replaces the database.
78 * @default "STANDARD"
79 */
80 databaseEdition?: DatabaseEdition;
81 /**
82 * Default transaction concurrency mode. Defaults to `PESSIMISTIC` for
83 * Standard edition and `OPTIMISTIC` for Enterprise.
84 */
85 concurrencyMode?: DatabaseConcurrencyMode;
86 /**
87 * Point-in-time recovery. When enabled, version retention is 7 days
88 * instead of 1 hour.
89 * @default "POINT_IN_TIME_RECOVERY_DISABLED"
90 */
91 pointInTimeRecoveryEnablement?: DatabasePointInTimeRecoveryEnablement;
92 /**
93 * App Engine integration mode. `DISABLED` is the API default for
94 * databases created with the Firestore API.
95 * @default "DISABLED"
96 */
97 appEngineIntegrationMode?: DatabaseAppEngineIntegrationMode;
98 /**
99 * Delete protection. Alchemy defaults to disabled so `destroy` can
100 * delete the database.
101 * @default "DELETE_PROTECTION_DISABLED"
102 */
103 deleteProtectionState?: DatabaseDeleteProtectionState;
104 /**
105 * Default Realtime Updates mode. Immutable — changing it replaces the
106 * database.
107 */
108 realtimeUpdatesMode?: DatabaseRealtimeUpdatesMode;
109 /**
110 * Firestore API data access mode. Defaults to enabled on Standard
111 * edition and disabled on Enterprise.
112 */
113 firestoreDataAccessMode?: DatabaseDataAccessMode;
114 /**
115 * MongoDB-compatible API data access mode. Always disabled on Standard
116 * edition.
117 */
118 mongodbCompatibleDataAccessMode?: DatabaseDataAccessMode;
119 /**
120 * Customer-managed encryption. Immutable — changing it replaces the
121 * database.
122 */
123 cmekConfig?: DatabaseCmekConfig;
124 };
125
126 export type Database = Resource<
127 "GCP.Firestore.Database",
128 DatabaseProps,
129 {
130 /** Full resource name `projects/{project}/databases/{database}`. */
131 name: string;
132 /** Database id (last path segment). */
133 databaseId: string;
134 /** Project id. */
135 project: string;
136 /** Location id (`us-central1`, `nam5`, …). */
137 location: string;
138 /** Database type (`FIRESTORE_NATIVE`, `DATASTORE_MODE`). */
139 type: string;
140 /** Database edition (`STANDARD`, `ENTERPRISE`). */
141 databaseEdition: string | undefined;
142 /** Default concurrency mode. */
143 concurrencyMode: string | undefined;
144 /** Point-in-time recovery enablement. */
145 pointInTimeRecoveryEnablement: string | undefined;
146 /** App Engine integration mode. */
147 appEngineIntegrationMode: string | undefined;
148 /** Delete protection state. */
149 deleteProtectionState: string | undefined;
150 /** Realtime Updates mode. */
151 realtimeUpdatesMode: string | undefined;
152 /** Firestore API data access mode. */
153 firestoreDataAccessMode: string | undefined;
154 /** MongoDB-compatible API data access mode. */
155 mongodbCompatibleDataAccessMode: string | undefined;
156 /** CMEK key, if any. */
157 kmsKeyName: string | undefined;
158 /** System-generated UUID4. */
159 uid: string | undefined;
160 /** Datastore key prefix, if any. */
161 keyPrefix: string | undefined;
162 /** Whether this database is eligible for the free tier. */
163 freeTier: boolean | undefined;
164 /** Version retention period (e.g. `"3600s"`). */
165 versionRetentionPeriod: string | undefined;
166 /** Earliest PITR timestamp, if available. */
167 earliestVersionTime: string | undefined;
168 /** RFC3339 creation timestamp. */
169 createTime: string | undefined;
170 /** RFC3339 last-update timestamp. */
171 updateTime: string | undefined;
172 /** Server etag. */
173 etag: string | undefined;
174 },
175 never,
176 Providers
177 >;
178
179 /**
180 * A Cloud Firestore database.
181 *
182 * Firestore databases have no labels field and Alchemy writes no data
183 * into them: `read` reports a database it finds without prior state as
184 * unowned (adopt it with `--adopt`). Changing `databaseId`, `location`, `databaseEdition`, CMEK, or
185 * Realtime Updates mode replaces the database.
186 *
187 * Create, update, and delete are long-running operations — provisioning
188 * a named database typically takes tens of seconds.
189 *
190 * ### Creating a Database
191 * **Example:** Generated name
192 * ```typescript
193 * const database = yield* GCP.Firestore.Database("App", {});
194 * ```
195 *
196 * **Example:** Explicit id, location, and concurrency
197 * ```typescript
198 * const database = yield* GCP.Firestore.Database("App", {
199 * databaseId: "app-data",
200 * location: "us-central1",
201 * type: "FIRESTORE_NATIVE",
202 * concurrencyMode: "OPTIMISTIC",
203 * });
204 * ```
205 *
206 * ### Reading and Writing Documents
207 * **Example:** Upsert and read a document
208 * ```typescript
209 * const patchDocument = yield* GCP.Firestore.PatchDocument(database);
210 * yield* patchDocument({
211 * documentPath: "users/alice",
212 * body: { fields: { name: { stringValue: "Alice" } } },
213 * });
214 * const getDocument = yield* GCP.Firestore.GetDocument(database);
215 * const doc = yield* getDocument({ documentPath: "users/alice" });
216 * ```
217 *
218 * @resource
219 * @category Firestore
220 */
221 export const Database = Resource<Database>("GCP.Firestore.Database");
222
223 export class DatabaseNotResolved extends Data.TaggedError("GCP.Firestore.DatabaseNotResolved")<{
224 resourceName: string;
225 }> {}
226
227 export class DatabaseStillExists extends Data.TaggedError("GCP.Firestore.DatabaseStillExists")<{
228 resourceName: string;
229 }> {}
230
231 const lastSegment = (value: string) => {
232 const trimmed = value.replace(/\/+$/, "");
233 const parts = trimmed.split("/");
234 return parts[parts.length - 1] || trimmed;
235 };
236
237 const normalizeLocation = (location: string | undefined, fallback: string) =>
238 lastSegment(location ?? fallback).toLowerCase();
239
240 const normalizeEnum = (value: string | undefined, fallback: string) => {
241 const next = (value ?? fallback).toUpperCase();
242 return next.endsWith("_UNSPECIFIED") ? fallback : next;
243 };
244
245 const normalizeType = (value: string | undefined) => normalizeEnum(value, DEFAULT_TYPE);
246
247 const normalizeEdition = (value: string | undefined) => normalizeEnum(value, DEFAULT_EDITION);
248
249 const resourceName = (project: string, databaseId: string) =>
250 `projects/${project}/databases/${databaseId}`;
251
252 const parseName = (name: string) => {
253 const parts = name.split("/").filter((part) => part.length > 0);
254 const databasesAt = parts.lastIndexOf("databases");
255 const projectsAt = parts.lastIndexOf("projects");
256 return {
257 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
258 databaseId:
259 databasesAt >= 0 && parts[databasesAt + 1] ? parts[databasesAt + 1]! : lastSegment(name),
260 };
261 };
262
263 const toDatabaseId = (id: string, databaseId: string | undefined, existing?: string) =>
264 Effect.gen(function* () {
265 if (databaseId !== undefined) return databaseId;
266 if (existing !== undefined) return existing;
267 const generated = yield* createPhysicalName({
268 id,
269 maxLength: MAX_DATABASE_ID_LENGTH,
270 lowercase: true,
271 });
272 let named = /^[a-z]/.test(generated) ? generated : `f${generated}`;
273 named = named.replace(/-+$/g, "").slice(0, MAX_DATABASE_ID_LENGTH);
274 named = named.replace(/-+$/g, "");
275 if (named.length < 4) {
276 named = `${named}dbxx`.slice(0, MAX_DATABASE_ID_LENGTH);
277 }
278 return named;
279 });
280
281 const toAttrs = (
282 database: firestore.GoogleFirestoreAdminV1Database,
283 project: string,
284 ): Database["Attributes"] => {
285 const name = database.name ?? "";
286 const parsed = parseName(name);
287 return {
288 name,
289 databaseId: parsed.databaseId,
290 project: parsed.project || project,
291 location: normalizeLocation(database.locationId, ""),
292 type: normalizeType(database.type),
293 databaseEdition: database.databaseEdition,
294 concurrencyMode: database.concurrencyMode,
295 pointInTimeRecoveryEnablement: database.pointInTimeRecoveryEnablement,
296 appEngineIntegrationMode: database.appEngineIntegrationMode,
297 deleteProtectionState: database.deleteProtectionState,
298 realtimeUpdatesMode: database.realtimeUpdatesMode,
299 firestoreDataAccessMode: database.firestoreDataAccessMode,
300 mongodbCompatibleDataAccessMode: database.mongodbCompatibleDataAccessMode,
301 kmsKeyName: database.cmekConfig?.kmsKeyName,
302 uid: database.uid,
303 keyPrefix: database.keyPrefix,
304 freeTier: database.freeTier,
305 versionRetentionPeriod: database.versionRetentionPeriod,
306 earliestVersionTime: database.earliestVersionTime,
307 createTime: database.createTime,
308 updateTime: database.updateTime,
309 etag: database.etag,
310 };
311 };
312
313 const getByName = (name: string) =>
314 firestore.getProjectsDatabases({ name }).pipe(
315 Effect.map((database) => (database.deleteTime !== undefined ? undefined : database)),
316 Effect.catchTag("NotFound", () => Effect.undefined),
317 );
318
319 /**
320 * Wait for a Firestore admin operation; named databases provision in tens
321 * of seconds to a few minutes. With `alreadyExistsOk`, ALREADY_EXISTS
322 * (code 6) counts as success (create race).
323 */
324 const waitForOperation = (
325 operation: firestore.GoogleLongrunningOperation,
326 options?: { alreadyExistsOk?: boolean },
327 ) =>
328 waitForGcpOperation(operation, (name) => firestore.getProjectsDatabasesOperations({ name }), {
329 budget: "10 minutes",
330 }).pipe(
331 Effect.catchIf(
332 (error) =>
333 options?.alreadyExistsOk === true &&
334 error._tag === "GCP.OperationFailed" &&
335 error.code === 6,
336 () => Effect.succeed(operation),
337 ),
338 );
339
340 const waitUntilExists = (name: string) =>
341 getByName(name).pipe(
342 Effect.filterOrFail(
343 (database): database is firestore.GoogleFirestoreAdminV1Database => database !== undefined,
344 () => new DatabaseNotResolved({ resourceName: name }),
345 ),
346 Effect.retry({
347 while: (error) => error._tag === "GCP.Firestore.DatabaseNotResolved",
348 times: 8,
349 schedule: Schedule.spaced("2 seconds"),
350 }),
351 );
352
353 const waitUntilGone = (name: string) =>
354 getByName(name).pipe(
355 Effect.filterOrFail(
356 (database) => database === undefined,
357 () => new DatabaseStillExists({ resourceName: name }),
358 ),
359 Effect.retry({
360 while: (error) => error._tag === "GCP.Firestore.DatabaseStillExists",
361 times: 10,
362 schedule: Schedule.spaced("8 seconds"),
363 }),
364 Effect.asVoid,
365 );
366
367 const retryConcurrentChanges = <A, E extends { readonly _tag: string }, R>(
368 effect: Effect.Effect<A, E, R>,
369 ) =>
370 effect.pipe(
371 Effect.retry({
372 while: (error) => error._tag === "Conflict",
373 times: 8,
374 schedule: Schedule.spaced("5 seconds"),
375 }),
376 );
377
378 const enumChanged = (desired: string | undefined, observed: string | undefined) => {
379 if (desired === undefined) return false;
380 return normalizeEnum(observed, desired) !== normalizeEnum(desired, desired);
381 };
382
383 export const DatabaseProvider = () =>
384 Provider.succeed(Database, {
385 stables: [
386 "name",
387 "databaseId",
388 "project",
389 "location",
390 "uid",
391 "createTime",
392 "keyPrefix",
393 "databaseEdition",
394 ],
395
396 diff: Effect.fn(function* ({ news, olds, output }) {
397 const env = yield* GcpEnvironment.current;
398 if (!isResolved(news)) return undefined;
399
400 const previousId = olds?.databaseId ?? output?.databaseId;
401 const nextId = news.databaseId ?? previousId;
402 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
403 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
404 const previousEdition = normalizeEdition(olds?.databaseEdition ?? output?.databaseEdition);
405 const nextEdition = normalizeEdition(news.databaseEdition ?? output?.databaseEdition);
406 const previousKms = olds?.cmekConfig?.kmsKeyName ?? output?.kmsKeyName ?? "";
407 const nextKms = news.cmekConfig?.kmsKeyName ?? previousKms;
408 const previousRealtime = (
409 olds?.realtimeUpdatesMode ??
410 output?.realtimeUpdatesMode ??
411 ""
412 ).toUpperCase();
413 const nextRealtime = (
414 news.realtimeUpdatesMode ??
415 output?.realtimeUpdatesMode ??
416 previousRealtime
417 ).toUpperCase();
418
419 const replace =
420 (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
421 previousLocation !== nextLocation ||
422 previousEdition !== nextEdition ||
423 previousKms !== nextKms ||
424 (news.realtimeUpdatesMode !== undefined && previousRealtime !== nextRealtime);
425
426 if (!replace) return undefined;
427 return {
428 action: "replace" as const,
429 deleteFirst: previousId !== undefined && nextId !== undefined && nextId === previousId,
430 };
431 }),
432
433 read: Effect.fn(function* ({ id, olds, output }) {
434 const env = yield* GcpEnvironment.current;
435 const databaseId = yield* toDatabaseId(id, olds?.databaseId, output?.databaseId);
436 const name = output?.name ?? resourceName(env.project, databaseId);
437 const existing = yield* getByName(name);
438 if (existing === undefined) return undefined;
439 const attrs = toAttrs(existing, env.project);
440 // No labels field: without prior state it may not be ours.
441 return output !== undefined ? attrs : Unowned(attrs);
442 }),
443
444 reconcile: Effect.fn(function* ({ id, news, output }) {
445 const env = yield* GcpEnvironment.current;
446 const databaseId = yield* toDatabaseId(id, news.databaseId, output?.databaseId);
447 const location = normalizeLocation(news.location ?? output?.location, env.region);
448 const type = normalizeType(news.type);
449 const name = resourceName(env.project, databaseId);
450 const desiredProtection = news.deleteProtectionState ?? DEFAULT_DELETE_PROTECTION;
451
452 let current = yield* getByName(name);
453
454 if (current === undefined) {
455 const created = yield* firestore
456 .createProjectsDatabases({
457 parent: `projects/${env.project}`,
458 databaseId,
459 body: {
460 locationId: location,
461 type,
462 databaseEdition: news.databaseEdition
463 ? normalizeEdition(news.databaseEdition)
464 : undefined,
465 concurrencyMode: news.concurrencyMode,
466 pointInTimeRecoveryEnablement: news.pointInTimeRecoveryEnablement,
467 appEngineIntegrationMode: news.appEngineIntegrationMode ?? DEFAULT_APP_ENGINE,
468 deleteProtectionState: desiredProtection,
469 realtimeUpdatesMode: news.realtimeUpdatesMode,
470 firestoreDataAccessMode: news.firestoreDataAccessMode,
471 mongodbCompatibleDataAccessMode: news.mongodbCompatibleDataAccessMode,
472 cmekConfig: news.cmekConfig?.kmsKeyName
473 ? { kmsKeyName: news.cmekConfig.kmsKeyName }
474 : undefined,
475 },
476 })
477 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
478 if (created !== undefined) {
479 yield* waitForOperation(created, { alreadyExistsOk: true });
480 }
481 current = yield* waitUntilExists(name);
482 }
483
484 if (current === undefined) {
485 return yield* new DatabaseNotResolved({ resourceName: name });
486 }
487
488 const mask: string[] = [];
489 const patchBody: firestore.GoogleFirestoreAdminV1Database = {};
490
491 if (enumChanged(type, current.type)) {
492 patchBody.type = type;
493 mask.push("type");
494 }
495 if (enumChanged(news.concurrencyMode, current.concurrencyMode)) {
496 patchBody.concurrencyMode = news.concurrencyMode;
497 mask.push("concurrencyMode");
498 }
499 if (enumChanged(news.pointInTimeRecoveryEnablement, current.pointInTimeRecoveryEnablement)) {
500 patchBody.pointInTimeRecoveryEnablement = news.pointInTimeRecoveryEnablement;
501 mask.push("pointInTimeRecoveryEnablement");
502 }
503 if (enumChanged(news.appEngineIntegrationMode, current.appEngineIntegrationMode)) {
504 patchBody.appEngineIntegrationMode = news.appEngineIntegrationMode;
505 mask.push("appEngineIntegrationMode");
506 }
507 if (enumChanged(desiredProtection, current.deleteProtectionState)) {
508 patchBody.deleteProtectionState = desiredProtection;
509 mask.push("deleteProtectionState");
510 }
511 if (enumChanged(news.firestoreDataAccessMode, current.firestoreDataAccessMode)) {
512 patchBody.firestoreDataAccessMode = news.firestoreDataAccessMode;
513 mask.push("firestoreDataAccessMode");
514 }
515 if (
516 enumChanged(news.mongodbCompatibleDataAccessMode, current.mongodbCompatibleDataAccessMode)
517 ) {
518 patchBody.mongodbCompatibleDataAccessMode = news.mongodbCompatibleDataAccessMode;
519 mask.push("mongodbCompatibleDataAccessMode");
520 }
521
522 if (mask.length > 0) {
523 const patched = yield* retryConcurrentChanges(
524 firestore.patchProjectsDatabases({
525 name,
526 updateMask: mask.join(","),
527 body: patchBody,
528 }),
529 );
530 yield* waitForOperation(patched);
531 current = yield* waitUntilExists(name);
532 }
533
534 return toAttrs(current, env.project);
535 }),
536
537 delete: Effect.fn(function* ({ output }) {
538 const current = yield* getByName(output.name);
539 if (current === undefined) return;
540
541 if (
542 normalizeEnum(current.deleteProtectionState, DEFAULT_DELETE_PROTECTION) ===
543 "DELETE_PROTECTION_ENABLED"
544 ) {
545 const patched = yield* retryConcurrentChanges(
546 firestore.patchProjectsDatabases({
547 name: output.name,
548 updateMask: "deleteProtectionState",
549 body: {
550 deleteProtectionState: DEFAULT_DELETE_PROTECTION,
551 },
552 }),
553 );
554 yield* waitForOperation(patched);
555 }
556
557 yield* retryConcurrentChanges(firestore.deleteProjectsDatabases({ name: output.name })).pipe(
558 Effect.catchTag("NotFound", () => Effect.void),
559 );
560 // Soft-delete: GET reports `deleteTime` long before the LRO is done.
561 yield* waitUntilGone(output.name);
562 }),
563 });