# digidem/comapeo-core architecture

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

- Interactive diagram: https://gitdiagram.com/digidem/comapeo-core
- Repository: https://github.com/digidem/comapeo-core

## Overview

##### Main Components

a. Core Components:
- MapeoManager (central orchestrator)
- MapeoProject (project management)
- DataStore (data persistence layer)
- CoreManager (manages hypercore instances)
- BlobStore (handles binary data storage)

b. Sync Components:
- LocalDiscovery (peer discovery)
- PeerSyncController (sync orchestration)
- SyncApi (sync interface)
- CoreSyncState (sync state management)
- NamespaceSyncState (namespace sync)

c. API Layers:
- BlobApi
- IconApi
- InviteApi
- MemberApi
- TranslationApi
- FastifyController (HTTP API)

##### Architectural Patterns to Highlight

a. Data Flow:
- How data flows from API layer through to storage
- Peer-to-peer sync paths
- Demonstrate local discovery mechanisms

b. Storage Architecture:
- Hyperdrive/Hypercore (underlying p2p storage)
- BlobStore (binary data)
- DataStore (structured data)

##### Component Relationships

**[Client Applications]**
- ↕
**[FastifyController/API Layer]**
- ↕
**[MapeoManager]**
- ↙   ↓   ↘
**[Projects] [Sync] [Storage]**
- ↓       ↓       ↓
**[DataTypes] [Peers] [Blobs]**
- ↘   ↓   ↙
- [P2P Network Layer]

##### Implementation Details

- Clear labels for all components
- Version information where relevant
- Database schema relationships

##### API interfaces

Remember to include version control and migration paths in the architecture, as indicated by the presence of schema versioning files in the project structure.

This architecture should be represented as a living document that can evolve with the project, particularly given the warning in the README that the package is not yet ready for use.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client["Client Layer"]
        ClientApps[Client Applications]
    end

    subgraph API["API Layer"]
        FastifyC[FastifyController]:::api
        BlobA[BlobApi]:::api
        IconA[IconApi]:::api
        InviteA[InviteApi]:::api
        MemberA[MemberApi]:::api
        TransA[TranslationApi]:::api
    end

    subgraph Core["Core Layer"]
        MapeoM[MapeoManager]:::core
        MapeoP[MapeoProject]:::core
        CoreM[CoreManager]:::core
    end

    subgraph Sync["Sync Layer"]
        PeerSync[PeerSyncController]:::sync
        SyncA[SyncApi]:::sync
        CoreSync[CoreSyncState]:::sync
        NSSync[NamespaceSyncState]:::sync
        LocalD[LocalDiscovery]:::sync
        LocalP[LocalPeers]:::sync
    end

    subgraph Storage["Storage Layer"]
        DataS[DataStore]:::storage
        BlobS[BlobStore]:::storage
        DataT[DataType]:::storage
    end

    subgraph Security["Security Layer"]
        CoreOwn[Core Ownership]:::security
        Schema[Schema Management]:::security
    end

    %% Connections
    ClientApps --> FastifyC
    FastifyC --> |API Requests| MapeoM
    
    MapeoM --> MapeoP
    MapeoM --> CoreM
    MapeoM --> PeerSync
    
    MapeoP --> DataS
    MapeoP --> BlobS
    MapeoP --> DataT
    
    PeerSync --> SyncA
    PeerSync --> CoreSync
    PeerSync --> NSSync
    PeerSync --> LocalD
    PeerSync --> LocalP
    
    CoreM --> CoreOwn
    DataS --> Schema
    
    BlobA & IconA & InviteA & MemberA & TransA --> MapeoM

    %% Click Events
    click FastifyC "https://github.com/digidem/comapeo-core/blob/main/src/fastify-controller.js"
    click BlobA "https://github.com/digidem/comapeo-core/blob/main/src/blob-api.js"
    click IconA "https://github.com/digidem/comapeo-core/blob/main/src/icon-api.js"
    click InviteA "https://github.com/digidem/comapeo-core/blob/main/src/invite-api.js"
    click MemberA "https://github.com/digidem/comapeo-core/blob/main/src/member-api.js"
    click TransA "https://github.com/digidem/comapeo-core/blob/main/src/translation-api.js"
    click MapeoM "https://github.com/digidem/comapeo-core/blob/main/src/mapeo-manager.js"
    click MapeoP "https://github.com/digidem/comapeo-core/blob/main/src/mapeo-project.js"
    click CoreM "https://github.com/digidem/comapeo-core/blob/main/src/core-manager/index.js"
    click PeerSync "https://github.com/digidem/comapeo-core/blob/main/src/sync/peer-sync-controller.js"
    click SyncA "https://github.com/digidem/comapeo-core/blob/main/src/sync/sync-api.js"
    click CoreSync "https://github.com/digidem/comapeo-core/blob/main/src/sync/core-sync-state.js"
    click NSSync "https://github.com/digidem/comapeo-core/blob/main/src/sync/namespace-sync-state.js"
    click LocalD "https://github.com/digidem/comapeo-core/blob/main/src/discovery/local-discovery.js"
    click LocalP "https://github.com/digidem/comapeo-core/blob/main/src/local-peers.js"
    click DataS "https://github.com/digidem/comapeo-core/blob/main/src/datastore/index.js"
    click BlobS "https://github.com/digidem/comapeo-core/blob/main/src/blob-store/index.js"
    click DataT "https://github.com/digidem/comapeo-core/blob/main/src/datatype/index.js"
    click CoreOwn "https://github.com/digidem/comapeo-core/blob/main/src/core-ownership.js"
    click Schema "https://github.com/digidem/comapeo-core/tree/main/src/schema"

    %% Styling
    classDef api fill:#2196F3,stroke:#1565C0,color:white
    classDef core fill:#4CAF50,stroke:#2E7D32,color:white
    classDef sync fill:#F44336,stroke:#C62828,color:white
    classDef storage fill:#FFC107,stroke:#FFA000,color:black
    classDef security fill:#9C27B0,stroke:#6A1B9A,color:white
    
    %% Legend
    subgraph Legend
        API_L[API Components]:::api
        Core_L[Core Components]:::core
        Sync_L[Sync Components]:::sync
        Storage_L[Storage Components]:::storage
        Security_L[Security Components]:::security
    end
```
