# zadam/trilium architecture

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

- Interactive diagram: https://gitdiagram.com/zadam/trilium
- Repository: https://github.com/zadam/trilium

## Overview

##### Main Components

A. Frontend Layer:
- Desktop Application (Electron-based)
- Web Application
- Mobile Frontend
- Key components in src/public/app:
- Widgets (note editing, tree view, etc.)
- Services (search, sync, etc.)
- Components (app context, tab management)

B. Backend Layer:
- Core Services (src/services):
- Management
- Search Engine
- Encryption Services
- Synchronization
- Authentication
- ETAPI (REST API)

C. Data Layer:
- SQLite Database
- File Storage (for attachments/blobs)
- Migration System

- Client Layer                                           |

- Desktop   |  |      Web      |  |    Mobile     |  |     Web Clipper     |        |
- Application |  |   Interface    |  |   Interface   |  |                      |        |

- Application Layer                                       |

- Note Editor   |  | Tree Manager  |  | Search Engine  |  |  Attribute System   |      |
- (CKEditor)    |  | (FancyTree)   |  |               |  |                      |      |

- Encryption    |  |     Sync      |  |    Scripts     |  |    Share System     |      |
- System       |  |    Service    |  |    Engine      |  |                      |      |

- Data Layer                                             |

- SQLite      |  |    Blobs      |  |   Revisions   |                               |
- Database     |  |    Storage    |  |    Storage    |                               |

##### Key Interactions to Highlight

- Bidirectional sync between clients and server
- Encryption flow for protected notes
- Search request flow
- Revision history system
- ETAPI interactions
- File attachment handling

##### Technical Details to Note

- Built on Node.js
- Uses Electron for desktop
- CKEditor for WYSIWYG
- CodeMirror for code editing
- WebSocket for real-time updates
- SQLite for data storage

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef frontend fill:#2196F3,stroke:#1565C0,color:white
    classDef backend fill:#4CAF50,stroke:#2E7D32,color:white
    classDef data fill:#FFC107,stroke:#FFA000,color:black
    classDef security fill:#F44336,stroke:#C62828,color:white
    classDef integration fill:#9C27B0,stroke:#6A1B9A,color:white

    %% Client Layer
    subgraph ClientLayer
        Desktop["Desktop Application"]
        Web["Web Interface"]
        Mobile["Mobile Frontend"]
        WebClipper["Web Clipper"]
    end

    %% Application Layer
    subgraph AppLayer
        %% UI Components
        NoteEditor["Note Editor (CKEditor)"]
        TreeManager["Tree Manager"]
        SearchUI["Search Interface"]
        TabManager["Tab Management"]

        %% Core Services
        NoteService["Note Management"]
        SearchEngine["Search Engine"]
        EncryptionSystem["Encryption System"]
        SyncService["Sync Service"]
        ScriptEngine["Script Engine"]
        AttributeSystem["Attribute System"]
        TreeService["Tree Service"]
        AuthService["Authentication"]
        ETAPI["ETAPI (REST API)"]
        BackupSystem["Backup System"]
    end

    %% Data Layer
    subgraph DataLayer
        SQLite[(SQLite Database)]
        BlobStorage[(Blob Storage)]
        RevisionStorage[(Revision Storage)]
        Migrations["Migration System"]
    end

    %% Relationships
    Desktop --> NoteEditor
    Desktop --> TreeManager
    Desktop --> SearchUI
    Desktop --> TabManager
    Web --> NoteEditor
    Web --> TreeManager
    Web --> SearchUI
    Mobile --> NoteEditor
    Mobile --> TreeManager

    NoteEditor --> NoteService
    TreeManager --> TreeService
    SearchUI --> SearchEngine
    
    NoteService --> SQLite
    NoteService --> BlobStorage
    NoteService --> RevisionStorage
    
    SyncService --> NoteService
    SyncService --> EncryptionSystem
    
    AuthService --> SQLite
    ETAPI --> NoteService
    ETAPI --> AuthService
    
    SearchEngine --> SQLite
    AttributeSystem --> SQLite
    ScriptEngine --> NoteService
    BackupSystem --> SQLite
    BackupSystem --> BlobStorage
    
    Migrations --> SQLite

    %% Component Styles
    class Desktop,Web,Mobile,WebClipper,NoteEditor,TreeManager,SearchUI,TabManager frontend
    class NoteService,SearchEngine,SyncService,ScriptEngine,AttributeSystem,TreeService,AuthService,ETAPI,BackupSystem backend
    class SQLite,BlobStorage,RevisionStorage,Migrations data
    class EncryptionSystem security

    %% Click Events
    click Desktop "https://github.com/zadam/trilium/blob/master/src/public/app/desktop.js"
    click Mobile "https://github.com/zadam/trilium/blob/master/src/public/app/mobile.js"
    click Web "https://github.com/zadam/trilium/blob/master/src/views/desktop.ejs"
    click NoteEditor "https://github.com/zadam/trilium/tree/master/src/public/app/widgets/type_widgets/"
    click TreeManager "https://github.com/zadam/trilium/blob/master/src/public/app/widgets/note_tree.js"
    click SearchUI "https://github.com/zadam/trilium/blob/master/src/public/app/widgets/quick_search.js"
    click TabManager "https://github.com/zadam/trilium/blob/master/src/public/app/components/tab_manager.js"
    click NoteService "https://github.com/zadam/trilium/blob/master/src/services/notes.js"
    click SearchEngine "https://github.com/zadam/trilium/tree/master/src/services/search/"
    click EncryptionSystem "https://github.com/zadam/trilium/tree/master/src/services/encryption/"
    click SyncService "https://github.com/zadam/trilium/blob/master/src/services/sync.js"
    click AuthService "https://github.com/zadam/trilium/blob/master/src/services/auth.js"
    click ETAPI "https://github.com/zadam/trilium/tree/master/src/etapi/"
    click SQLite "https://github.com/zadam/trilium/blob/master/src/services/sql.js"
    click BlobStorage "https://github.com/zadam/trilium/blob/master/src/services/blob.js"
    click Migrations "https://github.com/zadam/trilium/tree/master/db/migrations/"
    click TreeService "https://github.com/zadam/trilium/blob/master/src/services/tree.js"
    click AttributeSystem "https://github.com/zadam/trilium/blob/master/src/services/attributes.js"
    click ScriptEngine "https://github.com/zadam/trilium/blob/master/src/services/script.js"
    click BackupSystem "https://github.com/zadam/trilium/blob/master/src/services/backup.js"
```
