@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
mdx 89 lines 2.64 KB
Raw
1 ---
2 title: Eventarc
3 description: React to Firestore changes, Cloud Storage events, and audit-logged Google API calls with GCP.Eventarc.consumeEvents.
4 ---
5
6 Eventarc delivers events from Google services as CloudEvents: a
7 Firestore document was created, an object was written, an API was
8 called. `consumeEvents` creates the trigger and runs your handler for
9 each event.
10
11 ## React to new Firestore documents
12
13 ```typescript
14 // src/Auditor.ts
15 import * as GCP from "@alchemy.run/gcp";
16 import * as Effect from "effect/Effect";
17 import * as HttpServerResponse from "effect/http/HttpServerResponse";
18 import { Shop } from "./resources.ts";
19
20 export default class Auditor extends GCP.Function<Auditor>()(
21 "Auditor",
22 { main: import.meta.url },
23 Effect.gen(function* () {
24 const shop = yield* Shop;
25 const db = yield* GCP.Firestore.WriteDatabase(shop);
26
27 yield* GCP.Eventarc.consumeEvents(
28 "OrderCreated",
29 {
30 location: "us-central1",
31 eventFilters: [
32 { attribute: "type", value: "google.cloud.firestore.document.v1.created" },
33 { attribute: "database", value: shop.databaseId },
34 { attribute: "document", value: "orders/{id}", operator: "match-path-pattern" },
35 ],
36 },
37 (event) =>
38 db.set(`audit/${event.id}`, { document: event.attributes.document ?? "" }).pipe(
39 Effect.orDie,
40 ),
41 );
42
43 return { fetch: Effect.succeed(HttpServerResponse.text("ok")) };
44 }).pipe(
45 Effect.provide(GCP.Firestore.WriteDatabaseHttp),
46 Effect.provide(GCP.Run.EventarcEventSource),
47 ),
48 ) {}
49 ```
50
51 `eventFilters` choose which events you get. `event.attributes` names
52 the document; `event.data` holds the change itself.
53
54 ## Match the database's location
55
56 Firestore only sends events to triggers in the database's own
57 location. Pin both:
58
59 ```typescript
60 // src/resources.ts
61 export const Shop = GCP.Firestore.Database("Shop", {
62 location: "us-central1",
63 type: "FIRESTORE_NATIVE",
64 });
65 ```
66
67 ## The service stays private
68
69 The trigger signs each delivery with the service's own identity, and
70 Alchemy grants that identity what it needs to receive events. Nobody
71 else can call the event path.
72
73 ## Handle repeats
74
75 Eventarc delivers at least once. Use `set` keyed by the event, or
76 `create` and ignore `DocumentAlreadyExists`, so a second delivery
77 changes nothing.
78
79 ## Example
80
81 [`examples/gcp-eventarc-firestore`](https://github.com/alchemy-run/alchemy/tree/main/examples/gcp-eventarc-firestore)
82 writes an audit entry for every new order.
83
84 ## Where next
85
86 - [Firestore](/gcp/data/firestore) — the database that emits the
87 events.
88 - [Eventarc reference](/providers/gcp/eventarc#trigger) — triggers,
89 channels, and pipelines.