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

main
ts 88 lines 2.91 KB
Raw
1 import type * as firestore from "@distilled.cloud/gcp/firestore_v1";
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 { Database } from "./Database.ts";
7 import type { DocumentSnapshot } from "./ReadDatabase.ts";
8
9 /** `create` found a document already at `path`. */
10 export class DocumentAlreadyExists extends Data.TaggedError("GCP.Firestore.DocumentAlreadyExists")<{
11 path: string;
12 message: string;
13 }> {}
14
15 /** Write-only client for one Firestore database. */
16 export interface WriteDatabaseClient {
17 /**
18 * Create or overwrite the document at `path` (`"users/alice"`) with
19 * exactly `fields` (plain JavaScript, encoded to Firestore `Value`s).
20 */
21 set(
22 path: string,
23 fields: Record<string, unknown>,
24 ): Effect.Effect<
25 DocumentSnapshot,
26 firestore.PatchProjectsDatabasesDocumentsError,
27 RuntimeContext
28 >;
29 /**
30 * Update only the top-level keys of `fields` on an existing document
31 * (the update mask is `Object.keys(fields)`). Fails with `NotFound` when
32 * the document does not exist.
33 */
34 update(
35 path: string,
36 fields: Record<string, unknown>,
37 ): Effect.Effect<
38 DocumentSnapshot,
39 firestore.PatchProjectsDatabasesDocumentsError,
40 RuntimeContext
41 >;
42 /** Delete the document at `path`. Deleting a missing document succeeds. */
43 delete(
44 path: string,
45 ): Effect.Effect<void, firestore.DeleteProjectsDatabasesDocumentsError, RuntimeContext>;
46 /**
47 * Create the document at `path`; fails with
48 * {@link DocumentAlreadyExists} when it already exists.
49 */
50 create(
51 path: string,
52 fields: Record<string, unknown>,
53 ): Effect.Effect<
54 DocumentSnapshot,
55 | DocumentAlreadyExists
56 | Exclude<firestore.CreateDocumentProjectsDatabasesDocumentsError, { _tag: "Conflict" }>,
57 RuntimeContext
58 >;
59 }
60
61 /**
62 * Write access to a Firestore {@link Database}: `set`, `update`, `delete`,
63 * `create`. Grants `roles/datastore.user` on the project under an IAM
64 * Condition naming this database (Firestore databases have no
65 * resource-level IAM policy).
66 *
67 * ### Writing documents
68 * **Example:** Create, update, and delete
69 * ```typescript
70 * const db = yield* GCP.Firestore.WriteDatabase(database);
71 * yield* db.create("users/alice", { name: "Alice", visits: 0 }).pipe(
72 * Effect.catchTag("GCP.Firestore.DocumentAlreadyExists", () => Effect.void),
73 * );
74 * yield* db.update("users/alice", { visits: 1, lastSeen: new Date() });
75 * yield* db.delete("users/bob");
76 * // …provided with Effect.provide(GCP.Firestore.WriteDatabaseHttp)
77 * ```
78 *
79 * @binding
80 * @category Firestore
81 */
82 export interface WriteDatabase extends Binding.Service<
83 WriteDatabase,
84 "GCP.Firestore.WriteDatabase",
85 (database: Database) => Effect.Effect<WriteDatabaseClient>
86 > {}
87
88 export const WriteDatabase = Binding.Service<WriteDatabase>("GCP.Firestore.WriteDatabase");