# paulbraganza12/cloud-firestore-triggers architecture

> Architecture diagram of the GitHub repository paulbraganza12/cloud-firestore-triggers, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/paulbraganza12/cloud-firestore-triggers
- Repository: https://github.com/paulbraganza12/cloud-firestore-triggers

## Overview

##### Project type and purpose
- This is an event-driven, serverless microservice built in TypeScript on Google Cloud.
- It listens for Firestore document writes (onDocumentWritten), serializes the event into a custom Protobuf/JSON envelope, and publishes it as a `UsersDocumentWritten` message to a Pub/Sub topic named `user-written`.
- Locally, it runs Firestore and Pub/Sub emulators via Docker Compose; in production, it is deployed as a 2nd-gen Cloud Function via Cloud Build.

##### File structure
- Top level
- Configuration files: `.env-example`, `.gcloudignore`, `cloudbuild.yaml`, `docker-compose.yml`, `package.json`, `tsconfig.json`
- `firebase-docker/`: Dockerfile + emulator config for Firestore and Storage rules
- `protos/`: Protobuf schemas, compiled JS/TS, and helper scripts (`update.sh`)
- `src/`
- `index.ts`: Cloud Function entrypoint that registers the Firestore trigger
- `config.ts`: environment and topic configuration
- `pubsub/`: code that constructs and sends Pub/Sub messages (`user-firestore.ts`)
- `utils/`: pure functions for serializing and parsing event data
- `data.proto`: your event envelope schema
- `google/...`: imported standard Google protobuf definitions
- `__tests__/`: unit tests for parsing, serialization, and end-to-end behavior

##### README for additional insights
- Message envelope structure (`json` + `attributes`)
- Tech stack: Docker for emulator, Vitest for testing, Node/TypeScript, Cloud Functions v2, Pub/Sub, Firestore triggers

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Production Environment
    subgraph "Production Environment"
        FirestoreProd["Cloud Firestore"]:::cloud
        CloudFunction["Cloud Function<br/>(index.ts)"]:::cloud
        PubSubTopic["Pub/Sub Topic<br/(`user-written`)"]:::cloud
        CloudBuild["Cloud Build<br/(`cloudbuild.yaml`)"]:::infra
    end

    %% Local Development
    subgraph "Local Development (Docker Compose)"
        DockerCompose["Docker Compose<br/(`docker-compose.yml`)"]:::local
        FirestoreEmu["Firestore Emulator"]:::local
        PubSubEmu["Pub/Sub Emulator"]:::local
    end

    %% Application Code Modules
    subgraph "Application Code" 
        Index["index.ts<br/Trigger Registration"]:::code
        Config["config.ts<br/Env & Topics"]:::code
        PubSubModule["user-firestore.ts<br/Publish Module"]:::code
        DataProto["data.proto<br/Envelope Schema"]:::code
        Protos["protos/"]:::code
        GoogleProtos["src/google/"]:::code
        SerializeUtil["serialize-data.ts"]:::code
        ParseUtil["parse-event-data.ts"]:::code
        Tests["__tests__/"]:::code
    end

    %% Production Flow
    FirestoreProd -->|"onDocumentWritten"| CloudFunction
    CloudFunction -->|"load data.proto"| DataProto
    CloudFunction -->|"parse event"| ParseUtil
    CloudFunction -->|"serialize envelope"| SerializeUtil
    CloudFunction -->|"publish message"| PubSubTopic
    CloudBuild -->|"deploy"| CloudFunction

    %% Local Flow
    DockerCompose --> FirestoreEmu
    DockerCompose --> PubSubEmu
    FirestoreEmu -->|"onDocumentWritten"| CloudFunction
    CloudFunction --> PubSubEmu

    %% Code Relationships
    CloudFunction --> Index
    Index --> Config
    Index --> PubSubModule
    PubSubModule --> SerializeUtil
    PubSubModule --> ParseUtil
    PubSubModule --> Protos
    Protos --> DataProto
    Protos --> GoogleProtos
    SerializeUtil --> DataProto
    ParseUtil --> DataProto
    Index --> Tests

    %% Click Events
    click DockerCompose "https://github.com/paulbraganza12/cloud-firestore-triggers/blob/main/docker-compose.yml"
    click CloudBuild "https://github.com/paulbraganza12/cloud-firestore-triggers/blob/main/cloudbuild.yaml"
    click Index "https://github.com/paulbraganza12/cloud-firestore-triggers/blob/main/src/index.ts"
    click Config "https://github.com/paulbraganza12/cloud-firestore-triggers/blob/main/src/config.ts"
    click PubSubModule "https://github.com/paulbraganza12/cloud-firestore-triggers/blob/main/src/pubsub/user-firestore.ts"
    click DataProto "https://github.com/paulbraganza12/cloud-firestore-triggers/blob/main/src/data.proto"
    click Protos "https://github.com/paulbraganza12/cloud-firestore-triggers/tree/main/protos/"
    click GoogleProtos "https://github.com/paulbraganza12/cloud-firestore-triggers/tree/main/src/google/"
    click SerializeUtil "https://github.com/paulbraganza12/cloud-firestore-triggers/blob/main/src/utils/serialize-data.ts"
    click ParseUtil "https://github.com/paulbraganza12/cloud-firestore-triggers/blob/main/src/utils/parse-event-data.ts"
    click Tests "https://github.com/paulbraganza12/cloud-firestore-triggers/tree/main/src/__tests__/"

    %% Styles
    classDef cloud fill:#cce5ff,stroke:#3399ff,color:#004080;
    classDef code fill:#e6ffe6,stroke:#33cc33,color:#006600;
    classDef local fill:#fff4e6,stroke:#ff9933,color:#804000;
    classDef infra fill:#f2e6ff,stroke:#9933ff,color:#4d004d;
```
