# tanwarabhinandan/digigyan architecture

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

- Interactive diagram: https://gitdiagram.com/tanwarabhinandan/digigyan
- Repository: https://github.com/tanwarabhinandan/digigyan

## Overview

##### Main Components
- API Server: Represent the core of the system as the Express application started in server.ts and loaded via src/app.ts.
- Book Module (includes bookController.ts, bookModel.ts, bookRouter.ts, bookTypes.ts)
- User Module (includes userController.ts, userModel.ts, userRouter.ts, userTypes.ts)
- Middleware: Create elements for global mechanisms like authentication (authenticate.ts) and global error handling (globalErrorHandler.ts). Illustrate that these are applied across API calls.
- Configuration & External Services:
- Database: Represent the MongoDB and show its connection details from src/config/db.ts.
- Other config settings: Mention use of environment variables (dotenv) for configuring port, JWT secret, etc.

##### Relationships & Data Flow
- Client/API Consumer: Start with an external consumer (e.g., Postman or frontend client) that interacts with the API endpoints.
- Routing: From the client, requests go to the Express server which then routes to appropriate modules (Books and Users) based on the URL.
- Middleware Flow: Indicate that requests pass through middleware components (first the authentication middleware then error handling if something goes wrong) before reaching the controllers.
- Data Persistence: Show interactions between the controllers and the MongoDB database (from src/config/db.ts), indicating read/write dependencies.

##### Architectural Patterns & Design Principles
- Modular Structure: Emphasize segregation of concerns by splitting functionality into different modules (books, users, config, middlewares).
- MVC-like Structure: Indicate that the controllers handle the API logic, models manage data interaction, and routers serve as the entry points.
- Middleware Usage: Note that authentication (e.g., JWT based) and error handling middleware are applied globally for robust request processing.
- Integration with External Services: Use separate elements to represent external dependencies like Cloudinary and MongoDB, showcasing how the API leverages these technologies.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global nodes
    Client["Client/API Consumer"]:::client
    API["Express App (server.ts & app.ts)"]:::api
    Auth["Authentication Middleware"]:::middleware
    Err["Global Error Handling Middleware"]:::middleware
    Cloud["Cloudinary (Image Upload)"]:::external
    DB["MongoDB Database"]:::db

    %% Client to API Gateway
    Client -->|"HTTPRequest"| API
    API -->|"authenticate"| Auth
    API -->|"errorCatch"| Err

    %% Books Module Subgraph
    subgraph "Books Module"
        BR["Book Router"]:::module
        BT["Book Types"]:::module
        BC["Book Controller"]:::module
        BM["Book Model"]:::module
        
        BR -->|"handles"| BC
        BR -->|"defines"| BT
        BC -->|"logic"| BM
        BM -->|"CRUD"| DB
        BC -->|"uploads"| Cloud
    end

    %% Users Module Subgraph
    subgraph "Users Module"
        UR["User Router"]:::module
        UT["User Types"]:::module
        UC["User Controller"]:::module
        UM["User Model"]:::module
        
        UR -->|"handles"| UC
        UR -->|"defines"| UT
        UC -->|"logic"| UM
        UM -->|"CRUD"| DB
    end

    %% Routing from Authentication Middleware to Modules
    Auth -->|"routes"| BR
    Auth -->|"routes"| UR

    %% Click Events - API Server
    click API "https://github.com/tanwarabhinandan/digigyan/blob/master/server.ts"
    click API "https://github.com/tanwarabhinandan/digigyan/blob/master/src/app.ts"
    
    %% Click Events - Books Module
    click BR "https://github.com/tanwarabhinandan/digigyan/blob/master/src/book/bookRouter.ts"
    click BC "https://github.com/tanwarabhinandan/digigyan/blob/master/src/book/bookController.ts"
    click BM "https://github.com/tanwarabhinandan/digigyan/blob/master/src/book/bookModel.ts"
    click BT "https://github.com/tanwarabhinandan/digigyan/blob/master/src/book/bookTypes.ts"
    
    %% Click Events - Users Module
    click UR "https://github.com/tanwarabhinandan/digigyan/blob/master/src/user/userRouter.ts"
    click UC "https://github.com/tanwarabhinandan/digigyan/blob/master/src/user/userController.ts"
    click UM "https://github.com/tanwarabhinandan/digigyan/blob/master/src/user/userModel.ts"
    click UT "https://github.com/tanwarabhinandan/digigyan/blob/master/src/user/userTypes.ts"
    
    %% Click Events - Middlewares
    click Auth "https://github.com/tanwarabhinandan/digigyan/blob/master/src/middlewares/authenticate.ts"
    click Err "https://github.com/tanwarabhinandan/digigyan/blob/master/src/middlewares/globalErrorHandler.ts"
    
    %% Click Events - External Services
    click Cloud "https://github.com/tanwarabhinandan/digigyan/blob/master/src/config/cloudinary.ts"
    click DB "https://github.com/tanwarabhinandan/digigyan/blob/master/src/config/db.ts"

    %% Styles
    classDef client fill:#FFCDD2,stroke:#B71C1C,stroke-width:2px;
    classDef api fill:#BBDEFB,stroke:#0D47A1,stroke-width:2px;
    classDef middleware fill:#FFF9C4,stroke:#F57F17,stroke-width:2px;
    classDef module fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef external fill:#D1C4E9,stroke:#512DA8,stroke-width:2px;
    classDef db fill:#FFE0B2,stroke:#E65100,stroke-width:2px;
```
