# streamingfast/substreams-sink-sql architecture

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

- Interactive diagram: https://gitdiagram.com/streamingfast/substreams-sink-sql
- Repository: https://github.com/streamingfast/substreams-sink-sql

## Overview

##### Main Components Identification

a. Input Layer:
- Substreams Module (WASM)
- Authentication Token Handler
- Network Endpoint Handler

b. Core Processing:
- Command Layer (cmd/substreams-sink-sql)
- Bundler Component
- Sinker Component
- State Management

c. Database Layer:
- Database Dialect System
- PostgreSQL Support
- ClickHouse Support

d. CSV Processing Pipeline (for high throughput):
- CSV Generator
- CSV Injector

**[Input Layer]**

- ▼
**[Command Layer]**

- ┬─────────────────┐
- ▼               ▼                 ▼
**[Bundler]    [State Manager]    [CSV Pipeline]**

- ┬───────┘                 │
- ▼                         │
- [Sink Layer]                   │

- ┬──────────┘
- ▼
- [Database Layer]

##### Component Details

For each component, include these specific elements:

a. Input Layer:
- Substreams WASM module input
- Authentication token flow
- Network endpoint configuration
- Protocol buffer definitions (from proto/ directory)

b. Command Layer:
- Main commands: setup, run, generate-csv, inject-csv
- Configuration flow
- DSN parsing and validation

c. Processing Components:
- Bundler: Show data bundling and encoding process
- State Manager: Display cursor management and state tracking
- Sinker: Show database change processing
- CSV Pipeline: Show the alternative high-throughput path

d. Database Layer:
- Dialect abstraction
- Supported databases (PostgreSQL, ClickHouse)
- Connection management
- Schema management

##### Additional Details

Important aspects:

- Version information
- Configuration file references
- Schema file locations
- Protocol buffer message types
- Database connection parameters
- Buffer size configurations for CSV processing

##### Specific Implementation Notes

- The modular nature of the database dialect system
- The dual-path processing (direct vs. CSV-based)
- The cursor-based state management
- The configuration flexibility through DSN
- The protocol buffer integration

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Input
        WASM[Substreams WASM Module]
        AUTH[Authentication Token]
        PROTO["Protocol Buffers"]:::file
        NET[Network Endpoint]
    end

    subgraph Command
        CMD["Command Layer"]:::file
        CONFIG[Configuration Handler]
        DSN["DSN Parser"]:::file
    end

    subgraph Processing
        BUNDLE["Bundler"]:::file
        STATE["State Manager"]:::file
        STATEFILE["State Persistence"]:::file
        BUFFER["Buffer Manager"]:::file
        STATS_B["Bundler Stats"]:::file
        STATS_S["Sinker Stats"]:::file
    end

    subgraph CSV
        CSV_GEN["CSV Generator"]:::file
        CSV_INJ["CSV Injector"]:::file
    end

    subgraph Database
        DB_MAIN["Database Interface"]:::file
        DIALECT["Dialect System"]:::file
        CONN["Connection Manager"]:::file
        OPS["Database Operations"]:::file
        PG["PostgreSQL Support"]:::file
        CH["ClickHouse Support"]:::file
    end

    WASM --> CMD
    AUTH --> CMD
    NET --> CMD
    PROTO --> CMD

    CMD --> CONFIG
    CONFIG --> DSN
    DSN --> BUNDLE

    BUNDLE --> STATE
    STATE --> STATEFILE
    BUNDLE --> BUFFER
    BUNDLE --> STATS_B

    STATE --> CSV_GEN
    CSV_GEN --> CSV_INJ
    CSV_INJ --> DB_MAIN

    STATE --> DB_MAIN
    DB_MAIN --> DIALECT
    DIALECT --> PG
    DIALECT --> CH
    DB_MAIN --> CONN
    DB_MAIN --> OPS
    STATE --> STATS_S

    classDef file fill:#f9f,stroke:#333,stroke-width:2px;
    classDef default fill:#fff,stroke:#333,stroke-width:2px;

    click PROTO "https://github.com/streamingfast/substreams-sink-sql/blob/develop/proto/sf/substreams/sink/sql/v1/services.proto"
    click CMD "https://github.com/streamingfast/substreams-sink-sql/blob/develop/cmd/substreams-sink-sql/main.go"
    click BUNDLE "https://github.com/streamingfast/substreams-sink-sql/blob/develop/bundler/bundler.go"
    click STATE "https://github.com/streamingfast/substreams-sink-sql/blob/develop/state/interface.go"
    click STATEFILE "https://github.com/streamingfast/substreams-sink-sql/blob/develop/state/file.go"
    click BUFFER "https://github.com/streamingfast/substreams-sink-sql/blob/develop/bundler/writer/buffered.go"
    click STATS_B "https://github.com/streamingfast/substreams-sink-sql/blob/develop/bundler/stats.go"
    click STATS_S "https://github.com/streamingfast/substreams-sink-sql/blob/develop/sinker/stats.go"
    click CSV_GEN "https://github.com/streamingfast/substreams-sink-sql/blob/develop/cmd/substreams-sink-sql/generate_csv.go"
    click CSV_INJ "https://github.com/streamingfast/substreams-sink-sql/blob/develop/cmd/substreams-sink-sql/inject_csv.go"
    click DB_MAIN "https://github.com/streamingfast/substreams-sink-sql/blob/develop/db/db.go"
    click DIALECT "https://github.com/streamingfast/substreams-sink-sql/blob/develop/db/dialect.go"
    click CONN "https://github.com/streamingfast/substreams-sink-sql/blob/develop/db/dsn.go"
    click OPS "https://github.com/streamingfast/substreams-sink-sql/blob/develop/db/operations.go"
    click PG "https://github.com/streamingfast/substreams-sink-sql/blob/develop/db/dialect_postgres.go"
    click CH "https://github.com/streamingfast/substreams-sink-sql/blob/develop/db/dialect_clickhouse.go"
```
