# sysadminmike/couch-to-postgres architecture

> Architecture diagram of the GitHub repository sysadminmike/couch-to-postgres, 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/sysadminmike/couch-to-postgres
- Repository: https://github.com/sysadminmike/couch-to-postgres

## Overview

##### Main Components

a. Core Components:
- CouchDB Instance
- PostgreSQL Instance
- Node.js Synchronization Service
- HTTP Communication Layer (pgsql-http extension)

b. Data Flow Components:
- Changes Feed Stream
- Checkpoint Management
- Trigger System
- Bulk Operations Handler

**Top Layer: Client Interaction Layer**
- PostgreSQL Client Queries
- CouchDB Client Requests
- HTTP REST Endpoints

**Middle Layer: Synchronization Layer**
- Node.js Daemon
- Changes Feed Processor
- Checkpoint Manager
- Trigger Functions

**Bottom Layer: Storage Layer**
- PostgreSQL Tables (docs + checkpoints)
- CouchDB Documents
- JSON/JSONB Data Structures

##### Key Relationships to Highlight

a. Data Flow Paths:
- CouchDB → Changes Feed → Node.js → PostgreSQL
- PostgreSQL → Triggers → HTTP Extension → CouchDB
- Bulk Operations Path
- Checkpoint Management Flow

b. State Management:
- Since Checkpoints Table
- Document Versioning (_rev handling)
- Synchronization Status

##### Technical Details

Database Schemas:
**PostgreSQL Tables:**
- since_checkpoints (pgtable, since, enabled)
- document_tables (id, doc, from_pg)

**CouchDB:**
- Documents
- Design Documents
- Views

##### Implementation Notes

Annotations for:
- Configuration options
- Performance considerations
- Bulk operation thresholds
- Checkpoint intervals
- Error handling strategies

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef postgresql fill:#336791,color:white,stroke:#336791
    classDef couchdb fill:#E42528,color:white,stroke:#E42528
    classDef nodejs fill:#68A063,color:white,stroke:#68A063
    classDef http fill:#7F7F7F,color:white,stroke:#7F7F7F
    classDef config fill:#4A90E2,color:white,stroke:#4A90E2
    classDef monitor fill:#FFB900,color:black,stroke:#FFB900

    %% Legend
    subgraph Legend
        L1[PostgreSQL Components]:::postgresql
        L2[CouchDB Components]:::couchdb
        L3[Node.js Components]:::nodejs
        L4[HTTP Components]:::http
        L5[Config Components]:::config
        L6[Monitoring]:::monitor
    end

    %% Client Layer
    subgraph "Client Interaction Layer"
        PGClient[PostgreSQL Client Queries]:::postgresql
        CDBClient[CouchDB Client Requests]:::couchdb
        REST["HTTP REST Endpoints"]:::http
    end

    %% Synchronization Layer
    subgraph "Synchronization Layer"
        NodeDaemon["Node.js Daemon"]:::nodejs
        ChangesFeed["Changes Feed Processor"]:::nodejs
        CheckpointMgr["Checkpoint Manager"]:::nodejs
        TriggerFn["Trigger Functions"]:::postgresql
        Config["Configuration"]:::config
        Monitor["Monitoring System"]:::monitor
    end

    %% Storage Layer
    subgraph "Storage Layer"
        PostgreSQL[("PostgreSQL DB")]:::postgresql
        CouchDB[("CouchDB")]:::couchdb
    end

    %% Database Tables/Documents
    subgraph "PostgreSQL Tables"
        SinceCheckpoints["since_checkpoints"]:::postgresql
        DocTables["document_tables"]:::postgresql
    end

    subgraph "CouchDB Documents"
        Docs["Documents"]:::couchdb
        DesignDocs["Design Documents"]:::couchdb
    end

    %% Relationships
    CouchDB --> ChangesFeed
    ChangesFeed --> NodeDaemon
    NodeDaemon --> PostgreSQL
    PostgreSQL --> TriggerFn
    TriggerFn --> REST
    REST --> CouchDB
    NodeDaemon --> CheckpointMgr
    CheckpointMgr --> SinceCheckpoints
    Config --> NodeDaemon
    Monitor --> PostgreSQL
    Monitor --> CouchDB

    %% Click events for component mapping
    click NodeDaemon "https://github.com/sysadminmike/couch-to-postgres/blob/master/bin/daemon.js"
    click Config "https://github.com/sysadminmike/couch-to-postgres/blob/master/config/daemon.js.example"
    click Monitor "https://github.com/sysadminmike/couch-to-postgres/tree/master/nagios-check_couch_postgres_count"

    %% Additional technical details
    style Legend fill:#f9f9f9,stroke:#666,stroke-width:2px
    style PostgreSQL fill:#336791,stroke:#336791,stroke-width:2px
    style CouchDB fill:#E42528,stroke:#E42528,stroke-width:2px
```
