# cheerfulstoic/ecto_watch architecture

> Architecture diagram of the GitHub repository cheerfulstoic/ecto_watch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cheerfulstoic/ecto_watch
- Repository: https://github.com/cheerfulstoic/ecto_watch

## Overview

##### Main Components

a) Core Components:
- PostgreSQL Database (with triggers)
- EctoWatch Library Components
- Phoenix PubSub System
- Client Application Components

b) Key Process Components:
- WatcherSupervisor
- WatcherServer (multiple instances)
- Ecto Repo
- Subscribers (LiveView, GenServer, etc.)

**┌─ Client Application ─────────────────────────┐**

- ┌─ Subscribers ──┐                         │
- LiveView    │                         │
- GenServer   │                         │
- Channels    │                         │
- ┘                          │
- ▲                                │

- ┌─ Phoenix.PubSub ─┐                     │
- ┘                      │
- ▲                                │

- ┌─ EctoWatch ──────────────────────────┐ │

- ┌─ WatcherSupervisor ─┐            │ │

- ┌─ WatcherServer ─┐            │ │
- (per schema) │            │ │
- ┘            │ │
- ┘ │
- ┘ │

- ┼────────────────────────────────┘

**┌───────────▼────────────────┐**
- PostgreSQL Database    │
- ┌─────────────────────┐  │
- Notification        │  │
- Triggers           │  │
- ┘  │
- ┘

##### Data Flow Indicators
1. Database triggers → WatcherServer
2. WatcherServer → Phoenix.PubSub
3. Phoenix.PubSub → Subscribers
4. Client Application → Database (for mutations)

##### Important Details

a) Configuration Components:
- WatcherOptions configuration
- Supported operations (inserted/updated/deleted)
- Subscription patterns (by ID, by association)

b) Process Architecture:
- Supervisor tree structure
- Multiple WatcherServer instances (one per schema/operation pair)

##### Additional Notes

- Notes about the trigger creation process
- Standardized message format
- Subscription patterns (full schema vs. individual record)

##### Technical Details to Highlight

- PostgreSQL LISTEN/NOTIFY mechanism
- Ecto integration points
- PubSub topic structure
- Message payload format

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Application"]
        direction TB
        subgraph Subscribers["Subscribers"]
            direction TB
            LV[LiveView]:::subscriber
            GS[GenServer]:::subscriber
            CH[Channels]:::subscriber
        end
        
        PubSub[Phoenix.PubSub]:::pubsub
        
        subgraph EctoWatch["EctoWatch Library"]
            direction TB
            MainLib["Main Library"]:::core
            click MainLib "https://github.com/cheerfulstoic/ecto_watch/blob/main/lib/ecto_watch.ex"
            
            subgraph Supervisor["Watcher Management"]
                direction TB
                WS["WatcherSupervisor"]:::supervisor
                click WS "https://github.com/cheerfulstoic/ecto_watch/blob/main/lib/ecto_watch/watcher_supervisor.ex"
                
                Server["WatcherServer<br/>(per schema)"]:::server
                click Server "https://github.com/cheerfulstoic/ecto_watch/blob/main/lib/ecto_watch/watcher_server.ex"
                
                Config["Configuration<br/>System"]:::config
                click Config "https://github.com/cheerfulstoic/ecto_watch/tree/main/lib/ecto_watch/options/"
                
                WatchOpt["Watcher<br/>Options"]:::config
                click WatchOpt "https://github.com/cheerfulstoic/ecto_watch/blob/main/lib/ecto_watch/options/watcher_options.ex"
                
                TriggerVal["Trigger<br/>Validator"]:::validator
                click TriggerVal "https://github.com/cheerfulstoic/ecto_watch/blob/main/lib/ecto_watch/watcher_trigger_validator.ex"
                
                Helpers["Helper<br/>Utilities"]:::util
                click Helpers "https://github.com/cheerfulstoic/ecto_watch/blob/main/lib/ecto_watch/helpers.ex"
                
                OptHandler["Options<br/>Handler"]:::config
                click OptHandler "https://github.com/cheerfulstoic/ecto_watch/blob/main/lib/ecto_watch/options.ex"
            end
        end
    end
    
    subgraph DB["PostgreSQL Database"]
        direction TB
        Triggers["Notification<br/>Triggers"]:::db
        Data["Database<br/>Data"]:::db
    end
    
    %% Connections
    Triggers --> Server
    Server --> PubSub
    PubSub --> LV
    PubSub --> GS
    PubSub --> CH
    
    WS --> Server
    Server --> WatchOpt
    Server --> TriggerVal
    TriggerVal --> Triggers
    
    %% Styling
    classDef core fill:#a8e6cf,stroke:#3b6978
    classDef supervisor fill:#dcedc1,stroke:#3b6978
    classDef server fill:#ffd3b6,stroke:#3b6978
    classDef config fill:#ffaaa5,stroke:#3b6978
    classDef validator fill:#ff8b94,stroke:#3b6978
    classDef util fill:#98ddca,stroke:#3b6978
    classDef db fill:#ff9a8b,stroke:#8e4162
    classDef pubsub fill:#d4a5a5,stroke:#3b6978
    classDef subscriber fill:#9a8194,stroke:#3b6978
    
    %% Legend
    subgraph Legend
        L1[Core Components]:::core
        L2[Supervisor]:::supervisor
        L3[Server]:::server
        L4[Configuration]:::config
        L5[Validator]:::validator
        L6[Utilities]:::util
        L7[Database]:::db
        L8[PubSub]:::pubsub
        L9[Subscribers]:::subscriber
    end
```
