# sisovin/lynx-notes-app architecture

> Architecture diagram of the GitHub repository sisovin/lynx-notes-app, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sisovin/lynx-notes-app
- Repository: https://github.com/sisovin/lynx-notes-app

## Overview

##### Main components
- Frontend Components: Graphical UI/UX elements including LoginForm, SignupForm, NoteForm, and NoteList. These reside in the "src/components" directory.
- API/Controller Layer: The “src/controllers” directory holds the logic for authentication and notes management (authController.ts & notesController.ts), which process requests coming from the frontend.
- Routing Layer: Routes defined in "src/routes" (authRoutes.ts and noteRoutes.ts) that map incoming HTTP requests to the appropriate controller actions.
- Middleware: Found in "src/middleware" (authMiddleware.ts) to handle cross-cutting concerns like authentication.
- Models: Business logic layer in "src/models" (noteModel.ts and userModel.ts) that interact with the SQLite database, representing the domain data.
- Utility Layer: Includes files for cryptographic operations (argon2Utils.ts), JWT handling (jwtUtils.ts), and the Redis client (redisClient.ts) under "src/utils".
- Database: SQLite database (initialized via a script in "src/utils/initDatabase.ts").
- External Services: Redis is used as a caching service.

##### Relationships and interactions
- The Frontend sends HTTP requests (sign-up, login, note CRUD operations) to the backend’s API endpoints.
- Routing directs these requests to the appropriate controllers.
- Controllers then utilize middleware (e.g., authMiddleware.ts for verifying JWT) and interact with models to process the request (read/write data from SQLite).
- Utility functions are used during these operations for authentication (Argon2 hashing, JWT generation/verification) and caching (Redis operations).
- The server is started with Bun and listens on the configured port (as indicated by the environment variable PORT).

##### Architectural patterns and design principles
- Layered Architecture: Clear separation between presentation (frontend), application logic (controllers and middleware), domain (models), and data access (SQLite database).
- MVC-Like Pattern: Routes initiate controller actions, middleware enforces security, and models encapsulate the data schema and database interactions.
- Service and Utility Separation: Additional functionality like caching (Redis) and secure hashing/token generation are abstracted out into utilities.

##### Relevant technologies
- Bun as the runtime for the server.
- TypeScript as the main programming language.
- Tailwind CSS for styling.
- SQLite as the persistence layer.
- Redis for caching.
- External libraries for Argon2 and JWT processing.

- Clear labels: Name each component (e.g., "Frontend UI", "Auth Controller", "Notes Controller", "Auth Middleware", "User Model", "Note Model", "SQLite Database", "Redis Cache", "Utility Functions").

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Frontend Layer
    subgraph "Frontend Layer"
        LoginForm["LoginForm (tsx)"]:::frontend
        SignupForm["SignupForm (tsx)"]:::frontend
        NoteForm["NoteForm (tsx)"]:::frontend
        NoteList["NoteList (tsx)"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        BunRuntime["Bun Runtime"]:::backend

        subgraph "Routing Layer"
            AuthRoutes["Auth Routes"]:::backend
            NoteRoutes["Note Routes"]:::backend
        end

        subgraph "Controller Layer"
            AuthController["Auth Controller"]:::backend
            NotesController["Notes Controller"]:::backend
        end

        Middleware["Auth Middleware"]:::backend

        subgraph "Models"
            UserModel["User Model"]:::backend
            NoteModel["Note Model"]:::backend
        end

        subgraph "Utilities"
            Argon2Utils["Argon2 Hashing"]:::utility
            JWTUtils["JWT Operations"]:::utility
            RedisClient["Redis Client"]:::utility
        end

        SQLiteDB["SQLite Database"]:::database
    end

    %% Connections: Frontend -> Routing via Bun Runtime
    BunRuntime -->|"initializes"| AuthRoutes
    BunRuntime -->|"initializes"| NoteRoutes

    LoginForm -->|"sends-login-request"| AuthRoutes
    SignupForm -->|"sends-signup-request"| AuthRoutes
    NoteForm -->|"sends-note-CRUD"| NoteRoutes
    NoteList -->|"retrieves-notes"| NoteRoutes

    %% Routing to Middleware and Controllers
    AuthRoutes -->|"passes-through"| Middleware
    NoteRoutes -->|"passes-through"| Middleware

    Middleware -->|"delegates-to"| AuthController
    Middleware -->|"delegates-to"| NotesController

    %% Controllers call Models and Utilities
    AuthController -->|"manages-user"| UserModel
    NotesController -->|"manages-note"| NoteModel

    AuthController -->|"uses-hashing"| Argon2Utils
    AuthController -->|"handles-JWT"| JWTUtils
    NotesController -->|"uses-caching"| RedisClient

    %% Models interact with Database
    UserModel -->|"queries"| SQLiteDB
    NoteModel -->|"queries"| SQLiteDB

    %% Styling classes
    classDef frontend fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef backend fill:#ffa07a,stroke:#000,stroke-width:2px;
    classDef utility fill:#98fb98,stroke:#000,stroke-width:2px;
    classDef database fill:#fdfd96,stroke:#000,stroke-width:2px;

    %% Click Events
    click LoginForm "https://github.com/sisovin/lynx-notes-app/tree/main/src/components"
    click SignupForm "https://github.com/sisovin/lynx-notes-app/tree/main/src/components"
    click NoteForm "https://github.com/sisovin/lynx-notes-app/tree/main/src/components"
    click NoteList "https://github.com/sisovin/lynx-notes-app/tree/main/src/components"
    
    click AuthController "https://github.com/sisovin/lynx-notes-app/blob/main/src/controllers/authController.ts"
    click NotesController "https://github.com/sisovin/lynx-notes-app/blob/main/src/controllers/notesController.ts"
    
    click AuthRoutes "https://github.com/sisovin/lynx-notes-app/blob/main/src/routes/authRoutes.ts"
    click NoteRoutes "https://github.com/sisovin/lynx-notes-app/blob/main/src/routes/noteRoutes.ts"
    
    click Middleware "https://github.com/sisovin/lynx-notes-app/blob/main/src/middleware/authMiddleware.ts"
    
    click UserModel "https://github.com/sisovin/lynx-notes-app/blob/main/src/models/userModel.ts"
    click NoteModel "https://github.com/sisovin/lynx-notes-app/blob/main/src/models/noteModel.ts"
    
    click Argon2Utils "https://github.com/sisovin/lynx-notes-app/blob/main/src/utils/argon2Utils.ts"
    click JWTUtils "https://github.com/sisovin/lynx-notes-app/blob/main/src/utils/jwtUtils.ts"
    click RedisClient "https://github.com/sisovin/lynx-notes-app/blob/main/src/utils/redisClient.ts"
```
