@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 80 lines 2.7 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Effect from "effect/Effect";
3 import { serveProbes } from "../../bindingHost.ts";
4
5 /**
6 * Datastore-mode database the bindings are granted on (project roles under
7 * an IAM Condition matching only this database).
8 */
9 export const Tasks = GCP.Firestore.Database("Tasks", {
10 type: "DATASTORE_MODE",
11 });
12
13 /** Entity the Commit probe upserts. */
14 export const COMMITTED = { kind: "Task", name: "probe-commit" } as const;
15 /** Entity the test seeds out of band for the Lookup / RunQuery probes. */
16 export const SEEDED = { kind: "Seeded", name: "probe-seeded" } as const;
17
18 /** The Datastore API names the default database `""`, not `(default)`. */
19 export const apiDatabaseId = (databaseId: string) => (databaseId === "(default)" ? "" : databaseId);
20
21 /**
22 * Effect-native Cloud Run service exercising every Datastore binding as its
23 * own runtime service account. Deployed from {@link ../Bindings.test.ts}.
24 */
25 export default class DatastoreBindingsHost extends GCP.Function<DatastoreBindingsHost>()(
26 "DatastoreBindingsHost",
27 { main: import.meta.url, invokerIamDisabled: true },
28 Effect.gen(function* () {
29 const database = yield* Tasks;
30 const project = yield* database.project;
31 const databaseId = yield* database.databaseId;
32 const commit = yield* GCP.Datastore.Commit(Tasks);
33 const lookup = yield* GCP.Datastore.Lookup(Tasks);
34 const runQuery = yield* GCP.Datastore.RunQuery(Tasks);
35
36 const keyOf = (path: { kind: string; name: string }) =>
37 Effect.gen(function* () {
38 return {
39 partitionId: {
40 projectId: yield* project,
41 databaseId: apiDatabaseId(yield* databaseId),
42 },
43 path: [path],
44 };
45 });
46
47 return {
48 fetch: serveProbes({
49 commit: Effect.gen(function* () {
50 return yield* commit({
51 body: {
52 mode: "NON_TRANSACTIONAL",
53 mutations: [
54 {
55 upsert: {
56 key: yield* keyOf(COMMITTED),
57 properties: { title: { stringValue: "committed" } },
58 },
59 },
60 ],
61 },
62 });
63 }),
64 lookup: Effect.gen(function* () {
65 return yield* lookup({ body: { keys: [yield* keyOf(SEEDED)] } });
66 }),
67 runQuery: Effect.gen(function* () {
68 const { partitionId } = yield* keyOf(SEEDED);
69 return yield* runQuery({
70 body: { partitionId, query: { kind: [{ name: SEEDED.kind }] } },
71 });
72 }),
73 }),
74 };
75 }).pipe(
76 Effect.provide(GCP.Datastore.CommitHttp),
77 Effect.provide(GCP.Datastore.LookupHttp),
78 Effect.provide(GCP.Datastore.RunQueryHttp),
79 ),
80 ) {}