# wxn0brp/valtheradb-server architecture

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

- Interactive diagram: https://gitdiagram.com/wxn0brp/valtheradb-server
- Repository: https://github.com/wxn0brp/valtheradb-server

## Overview

##### Project Type and Purpose
- This is a full-stack file-based database server with:
- A Node.js/TypeScript backend (Express) that exposes APIs, initializes and manages a file-based DB using `@wxn0brp/db`.
- A CLI management tool (`serverMgmt`) for setup and control.
- An optional browser-based GUI built with Monaco editor and static HTML/CSS/JS.

##### File Structure
- Top-level:
- `server/` – core backend code (auth, init, query, Express routes, utilities).
- `serverMgmt/` – CLI management commands.
- `gui-script/` – source and build logic for the GUI (TypeScript, Monaco, bundler).
- `gui/` – generated static assets (HTML, CSS, JS).
- Root scripts (`app.js`, `build_docker.sh`, `dockerfile`) – entrypoints and deployment.
- Config files (`.env.example`, `suglite.json`, `tsconfig.json`).

##### README Insights
- Uses `.env` for config (toggle GUI, DB paths, secrets).
- `yarn mgmt` invokes CLI in `serverMgmt/`.
- `yarn start` launches the Express server in `server/index.ts`.
- GUI is built via `gui-script/` and served if enabled.

##### Main Components
a. CLI Management Tool
- Component: `serverMgmt` module
- Responsibilities: initialize DB, apply migrations, manage keys, control server lifecycle
- Entry: `serverMgmt/index.ts` → `mgmt.ts`

b. Backend Server
- Component: Express app (`server/index.ts` + `server/express/*`)
- Sub-components:
- Initialization (`server/init/*`)
- Authentication (`server/auth/*`)
- Query engine (`server/query/*`)
- Utilities (`server/utils/*`, `server/vars/*`)
- External library: `@wxn0brp/db` for file-based DB operations
- Exposes: REST/HTTP endpoints for client & GUI

c. File-Based Database
- Component: `.db` files on disk managed by `@wxn0brp/db`
- Managed via server’s init/`initDataBases.ts` and query functions

d. GUI Client
- Build: `gui-script/src/*` compiles to bundle under `gui/`
- Runtime: HTML/CSS/JS served by Express (`server/express/gui.ts`)
- Features: Monaco editor integration for SQL entry, import/export pages

e. Build & Deployment
- Dockerfile & `build_docker.sh` for containerization
- GitHub Actions (`.github/workflows/`) for CI/CD and documentation pages

##### Architectural Patterns and Principles
- Modular server structure: clear separation of init, auth, query, routes
- Single Express app combining API + static file serving
- CLI and server share internal modules for code reuse
- Use of environment-based feature flags (`gui=true`)

##### Final Checklist
- All major folders/modules are represented as components.
- Technologies (`Express`, `TypeScript`, `Monaco`, `@wxn0brp/db`, `Docker`) are annotated.
- Build & deployment elements (Docker, GitHub Actions) are included at a high level.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CLI Management Tool
    subgraph "CLI Management Tool" 
        style CLI fill:#d4f7d4,stroke:#38a836
        CLI_Index["index.ts"] 
        CLI_Mgmt["mgmt.ts"]
    end

    %% Backend Server
    subgraph "Backend Server (Express App)"
        style Backend fill:#d0e7f9,stroke:#3b82f6
        ExpressIndex["index.ts"]
        subgraph "Express Routes"
            ExpressMain["express/index.ts"]
            GUI_Route["gui.ts"]
            DB_Adapter["db.ts"]
        end
        subgraph "Initialization Modules"
            Init_DB["initDataBases.ts"]
            Init_Parser["customParser.ts"]
            Init_Keys["keys.ts"]
        end
        subgraph "Authentication Modules"
            Auth_Main["auth.ts"]
            Auth_Helpers["helpers.ts"]
        end
        subgraph "Query Engine"
            Query_Main["query.ts"]
            Query_DB["db.ts"]
            Query_Func["function.ts"]
            Query_Rel["relation.ts"]
            Query_SQLFile["sqlFile.ts"]
        end
        subgraph "Utilities"
            Util_Path["path.ts"]
            Util_Perm["perm.ts"]
            Util_Export["sqlFileExport.ts"]
            Util_Import["sqlFileImport.ts"]
        end
        subgraph "Config & Types"
            Vars_Index["vars/index.ts"]
            Vars_Secret["vars/secret.ts"]
            Env_Example[".env.example"]
            Sug_Lite["sug-lite.json"]
            Types_Shared["types.ts"]
        end
    end

    %% File-Based Database
    DB["File-Based .db Store"]:::dbstyle

    %% GUI Build Pipeline
    subgraph "GUI Build Scripts"
        style Build fill:#d4f7f9,stroke:#7e3af2
        GS_Build["build.js"]
        GS_Pkg["package.json"]
        GS_TSConfig["tsconfig.json"]
        subgraph "Source"
            GS_Index["src/index.ts"]
            GS_DB["src/db.ts"]
            GS_Monaco["src/monaco.ts"]
            GS_MonoTypes["src/monaco.types.ts"]
            GS_Render["src/render.ts"]
            GS_Run["src/run.ts"]
            GS_Utils["src/utils.ts"]
        end
    end

    %% GUI Static Assets
    subgraph "Static GUI Assets"
        style GUI fill:#f4d7f9,stroke:#d6336c
        GUI_Index["index.html"]
        GUI_Login["login.html"]
        GUI_Export["sql-export.html"]
        GUI_Import["sql-import.html"]
        subgraph "Styles & Scripts"
            GUI_CSS["css/style.css"]
            GUI_CSSLogin["css/login.css"]
            GUI_LoginJS["login.js"]
        end
    end

    %% Deployment & CI/CD
    subgraph "Deployment & CI/CD"
        style Deploy fill:#e2e2e2,stroke:#6c757d
        Dockerfile["dockerfile"]
        BuildScript["build_docker.sh"]
        GH_Build[".github/workflows/build.yml"]
        GH_Pages[".github/workflows/build-pages.yml"]
    end

    %% Relationships
    CLI_Index -->|"calls"| ExpressIndex
    CLI_Mgmt -->|"controls"| ExpressIndex

    ExpressIndex -->|"uses"| ExpressMain
    ExpressMain --> GUI_Route
    ExpressMain --> DB_Adapter

    ExpressIndex -->|"initDatabases()"| Init_DB
    ExpressIndex --> Init_Parser
    ExpressIndex --> Init_Keys

    ExpressIndex --> Auth_Main
    Auth_Main --> Auth_Helpers

    ExpressIndex --> Query_Main
    Query_Main --> Query_DB
    Query_Main --> Query_Func
    Query_Main --> Query_Rel
    Query_Main --> Query_SQLFile

    ExpressIndex --> Util_Path
    ExpressIndex --> Util_Perm
    ExpressIndex --> Util_Export
    ExpressIndex --> Util_Import

    ExpressIndex --> Vars_Index
    Vars_Index --> Vars_Secret
    Vars_Index --> Env_Example
    Vars_Index --> Sug_Lite
    ExpressIndex --> Types_Shared

    ExpressIndex -->|"read/write"| DB

    GUI_Index -->|"HTTP requests"| ExpressMain
    GUI_Login --> ExpressMain
    GUI_Export --> ExpressMain
    GUI_Import --> ExpressMain

    GS_Build --> GS_Index
    GS_Index --> GS_DB
    GS_Index --> GS_Monaco
    GS_Index --> GS_MonoTypes
    GS_Index --> GS_Render
    GS_Index --> GS_Run
    GS_Index --> GS_Utils
    GS_Build --> GS_Pkg
    GS_Build --> GS_TSConfig
    GS_Build -->|"outputs"| GUI_Index
    GS_Build --> GUI_Login
    GS_Build --> GUI_Export
    GS_Build --> GUI_Import
    GS_Build --> GUI_CSS
    GS_Build --> GUI_CSSLogin
    GS_Build --> GUI_LoginJS

    GH_Build --> Dockerfile
    GH_Build --> BuildScript
    GH_Build --> GH_Pages

    %% Click Events
    click CLI_Index "https://github.com/wxn0brp/valtheradb-server/blob/master/serverMgmt/index.ts"
    click CLI_Mgmt "https://github.com/wxn0brp/valtheradb-server/blob/master/serverMgmt/mgmt.ts"
    click ExpressIndex "https://github.com/wxn0brp/valtheradb-server/blob/master/server/index.ts"
    click ExpressMain "https://github.com/wxn0brp/valtheradb-server/blob/master/server/express/index.ts"
    click GUI_Route "https://github.com/wxn0brp/valtheradb-server/blob/master/server/express/gui.ts"
    click DB_Adapter "https://github.com/wxn0brp/valtheradb-server/blob/master/server/express/db.ts"
    click Init_DB "https://github.com/wxn0brp/valtheradb-server/blob/master/server/init/initDataBases.ts"
    click Init_Parser "https://github.com/wxn0brp/valtheradb-server/blob/master/server/init/customParser.ts"
    click Init_Keys "https://github.com/wxn0brp/valtheradb-server/blob/master/server/init/keys.ts"
    click Auth_Main "https://github.com/wxn0brp/valtheradb-server/blob/master/server/auth/auth.ts"
    click Auth_Helpers "https://github.com/wxn0brp/valtheradb-server/blob/master/server/auth/helpers.ts"
    click Query_Main "https://github.com/wxn0brp/valtheradb-server/blob/master/server/query/query.ts"
    click Query_DB "https://github.com/wxn0brp/valtheradb-server/blob/master/server/query/db.ts"
    click Query_Func "https://github.com/wxn0brp/valtheradb-server/blob/master/server/query/function.ts"
    click Query_Rel "https://github.com/wxn0brp/valtheradb-server/blob/master/server/query/relation.ts"
    click Query_SQLFile "https://github.com/wxn0brp/valtheradb-server/blob/master/server/query/sqlFile.ts"
    click Util_Path "https://github.com/wxn0brp/valtheradb-server/blob/master/server/utils/path.ts"
    click Util_Perm "https://github.com/wxn0brp/valtheradb-server/blob/master/server/utils/perm.ts"
    click Util_Export "https://github.com/wxn0brp/valtheradb-server/blob/master/server/utils/sqlFileExport.ts"
    click Util_Import "https://github.com/wxn0brp/valtheradb-server/blob/master/server/utils/sqlFileImport.ts"
    click Vars_Index "https://github.com/wxn0brp/valtheradb-server/blob/master/server/vars/index.ts"
    click Vars_Secret "https://github.com/wxn0brp/valtheradb-server/blob/master/server/vars/secret.ts"
    click Env_Example "https://github.com/wxn0brp/valtheradb-server/blob/master/.env.example"
    click Sug_Lite "https://github.com/wxn0brp/valtheradb-server/blob/master/sug-lite.json"
    click Types_Shared "https://github.com/wxn0brp/valtheradb-server/blob/master/server/types.ts"
    click GS_Build "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/build.js"
    click GS_Pkg "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/package.json"
    click GS_TSConfig "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/tsconfig.json"
    click GS_Index "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/src/index.ts"
    click GS_DB "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/src/db.ts"
    click GS_Monaco "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/src/monaco.ts"
    click GS_MonoTypes "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/src/monaco.types.ts"
    click GS_Render "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/src/render.ts"
    click GS_Run "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/src/run.ts"
    click GS_Utils "https://github.com/wxn0brp/valtheradb-server/blob/master/gui-script/src/utils.ts"
    click GUI_Index "https://github.com/wxn0brp/valtheradb-server/blob/master/gui/index.html"
    click GUI_Login "https://github.com/wxn0brp/valtheradb-server/blob/master/gui/login.html"
    click GUI_Export "https://github.com/wxn0brp/valtheradb-server/blob/master/gui/sql-export.html"
    click GUI_Import "https://github.com/wxn0brp/valtheradb-server/blob/master/gui/sql-import.html"
    click GUI_CSS "https://github.com/wxn0brp/valtheradb-server/blob/master/gui/css/style.css"
    click GUI_CSSLogin "https://github.com/wxn0brp/valtheradb-server/blob/master/gui/css/login.css"
    click GUI_LoginJS "https://github.com/wxn0brp/valtheradb-server/blob/master/gui/login.js"
    click Dockerfile "https://github.com/wxn0brp/valtheradb-server/tree/master/dockerfile"
    click BuildScript "https://github.com/wxn0brp/valtheradb-server/blob/master/build_docker.sh"
    click GH_Build "https://github.com/wxn0brp/valtheradb-server/blob/master/.github/workflows/build.yml"
    click GH_Pages "https://github.com/wxn0brp/valtheradb-server/blob/master/.github/workflows/build-pages.yml"

    %% Styles
    classDef dbstyle fill:#ffe4b5,stroke:#f08000
    class CLI_Index,CLI_Mgmt fill:#d4f7d4,stroke:#38a836
    class ExpressIndex,ExpressMain,GUI_Route,DB_Adapter fill:#d0e7f9,stroke:#3b82f6
    class Init_DB,Init_Parser,Init_Keys,Auth_Main,Auth_Helpers,Query_Main,Query_DB,Query_Func,Query_Rel,Query_SQLFile,Util_Path,Util_Perm,Util_Export,Util_Import,Vars_Index,Vars_Secret,Env_Example,Sug_Lite,Types_Shared fill:#d0e7f9,stroke:#3b82f6
    class GS_Build,GS_Pkg,GS_TSConfig,GS_Index,GS_DB,GS_Monaco,GS_MonoTypes,GS_Render,GS_Run,GS_Utils fill:#d4f7f9,stroke:#7e3af2
    class GUI_Index,GUI_Login,GUI_Export,GUI_Import,GUI_CSS,GUI_CSSLogin,GUI_LoginJS fill:#f4d7f9,stroke:#d6336c
    class Dockerfile,BuildScript,GH_Build,GH_Pages fill:#e2e2e2,stroke:#6c757d
```
