# pomarec/sangre architecture

> Architecture diagram of the GitHub repository pomarec/sangre, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pomarec/sangre
- Repository: https://github.com/pomarec/sangre

## Overview

##### Main Components Identification

a) Data Source Layer:
- PostgreSQL Database (with logical replication enabled)
- Supabase Realtime (as the change data capture broker)

b) Core Processing Layer (Node System):
- Source Nodes (postgres_table.ts, array.ts, growing_array.ts)
- Operator Nodes (filter.ts, join_*.ts, diffed.ts, etc.)
- Node Factory (node_factory.ts)

c) API Layer:
- Express.js Integration (express.ts)
- WebSocket Endpoints

d) Client Layer:
- Client Applications (Flutter example shown)

**[Database Layer]**
- ↓
**[Change Data Capture Layer]**
- ↓
**[Core Processing Layer]**
- ↓
**[API Layer]**
- ↓
**[Client Layer]**

##### Specific Implementation Details

- The node-based architecture (from src/nodes/)
- Different types of nodes (sources, operators)
- How joins and filters are processed
- The diffing mechanism for minimal network load
- WebSocket streaming implementation

##### Important Relationships

- PostgreSQL → Supabase Realtime → Source Nodes
- Source Nodes → Operator Nodes (showing transformation flow)
- Operator Nodes → Express.js Integration
- Express.js → WebSocket Connections → Clients

##### Additional Elements

- Sample data flow for a typical query
- Error handling paths
- Scaling points

##### Implementation Notes

Use tools like:

Remember to emphasize the project's key differentiators:
- Real-time processing of relational queries
- Incremental updates via diffs
- Integration with existing backend frameworks
- Offline sync capabilities

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Data_Source_Layer
        PG[PostgreSQL]:::database
        SR[Supabase Realtime]:::cdc
    end

    subgraph Core_Processing_Layer
        NF[Node Factory]:::core
        
        subgraph Source_Nodes
            PT[PostgreSQL Table Source]:::source
            AS[Array Source]:::source
            GA[Growing Array Source]:::source
        end

        subgraph Operator_Nodes
            DF[Diff Operator]:::operator
            FT[Filter Operator]:::operator
            J1[One-to-One Join]:::operator
            JM[Many-to-Many Join]:::operator
            LG[Logger Operator]:::operator
        end

        subgraph Node_Input_Handlers
            NI1[Single Input]:::input
            NI2[Double Input]:::input
            NI3[Triple Input]:::input
            BNI[Base Input Handler]:::input
        end

        BN[Base Node]:::core
    end

    subgraph API_Layer
        EX[Express Integration]:::api
        WS[WebSocket Endpoints]:::api
    end

    subgraph Client_Layer
        CL[Client Applications]:::client
    end

    %% Connections
    PG --> SR
    SR --> PT
    NF --> Source_Nodes
    NF --> Operator_Nodes
    Source_Nodes --> Operator_Nodes
    Operator_Nodes --> API_Layer
    EX --> WS
    WS --> CL
    BN --> Source_Nodes
    BN --> Operator_Nodes
    BNI --> NI1
    BNI --> NI2
    BNI --> NI3

    %% Click Events
    click NF "https://github.com/pomarec/sangre/blob/main/src/nodes/node_factory.ts"
    click PT "https://github.com/pomarec/sangre/blob/main/src/nodes/sources/postgres_table.ts"
    click AS "https://github.com/pomarec/sangre/blob/main/src/nodes/sources/array.ts"
    click GA "https://github.com/pomarec/sangre/blob/main/src/nodes/sources/growing_array.ts"
    click DF "https://github.com/pomarec/sangre/blob/main/src/nodes/operators/diffed.ts"
    click FT "https://github.com/pomarec/sangre/blob/main/src/nodes/operators/filter.ts"
    click J1 "https://github.com/pomarec/sangre/blob/main/src/nodes/operators/join_one_to_one.ts"
    click JM "https://github.com/pomarec/sangre/blob/main/src/nodes/operators/join_many_to_many.ts"
    click LG "https://github.com/pomarec/sangre/blob/main/src/nodes/operators/logger.ts"
    click EX "https://github.com/pomarec/sangre/blob/main/src/express.ts"
    click CL "https://github.com/pomarec/sangre/tree/main/example/client/"
    click BN "https://github.com/pomarec/sangre/blob/main/src/nodes/node.ts"
    click BNI "https://github.com/pomarec/sangre/blob/main/src/nodes/node_with_input/node_with_input.ts"
    click NI1 "https://github.com/pomarec/sangre/blob/main/src/nodes/node_with_input/node_1_input.ts"
    click NI2 "https://github.com/pomarec/sangre/blob/main/src/nodes/node_with_input/node_2_input.ts"
    click NI3 "https://github.com/pomarec/sangre/blob/main/src/nodes/node_with_input/node_3_input.ts"

    %% Styles
    classDef database fill:#3498db,stroke:#2980b9,color:white
    classDef cdc fill:#9b59b6,stroke:#8e44ad,color:white
    classDef core fill:#2ecc71,stroke:#27ae60,color:white
    classDef source fill:#e74c3c,stroke:#c0392b,color:white
    classDef operator fill:#f1c40f,stroke:#f39c12,color:white
    classDef input fill:#1abc9c,stroke:#16a085,color:white
    classDef api fill:#e67e22,stroke:#d35400,color:white
    classDef client fill:#95a5a6,stroke:#7f8c8d,color:white
```
