# thanhquang2004/library-server architecture

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

- Interactive diagram: https://gitdiagram.com/thanhquang2004/library-server
- Repository: https://github.com/thanhquang2004/library-server

## Overview

##### Project type and purpose
- This is a backend‐only RESTful API built with Node.js and Express that serves as the core of a Library Management System.
- It exposes endpoints for user authentication, book/catalog management, lending & reservation, fines, notifications, payments, audit logging, etc.

##### File structure
- Entry points:
– server.js: boots the HTTP server
– app.js: configures Express (middleware, routes, error handler)
- layers (MVC + services):
– routes/: maps HTTP endpoints → controllers
– controllers/: Express handlers → orchestrate request flow → call services
– services/: business logic → uses models to read/write data
– models/: ORM/ODM schemas (Mongoose/Sequelize) for DB entities
- middlewares/: cross‐cutting concerns (auth, rate limiting, logging, validation, error handling, sanitization)
- utils/: shared helpers (email sender, hashing, validation functions)

##### README for additional insights
- Confirms Node.js 18+, Express, optional choice of MongoDB / PostgreSQL / MySQL
- Env vars drive PORT, DB URL, JWT secret, etc.
- No frontend—pure API server using Git Workflow

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    Client["Client\n(Web/Mobile/API Consumer)"]:::external

    %% API Gateway
    subgraph "API Gateway"
        ServerJS["server.js"]:::api
        AppJS["app.js"]:::api
    end

    %% Middleware Layer
    subgraph "Middlewares"
        AuthM["authMiddleware.js"]:::api
        RateM["rateLimiter.js"]:::api
        LogM["logMiddleware.js"]:::api
        SanitizeM["sanitizeMiddleware.js"]:::api
        ValidateM["validateRequest.js"]:::api
        RoleM["roleMiddleware.js"]:::api
        ErrorM["errorHandler.js"]:::api
        CheckBookM["checkBookAvailability.js"]:::api
    end

    %% Routing Layer
    subgraph "Routing Layer"
        AuthR["authRoutes.js"]:::api
        AuthorR["authorRoutes.js"]:::api
        BookR["bookRoutes.js"]:::api
        BookItemR["bookItemRoutes.js"]:::api
        LendingR["lendingRoutes.js"]:::api
        FinesR["finesRoutes.js"]:::api
        CardR["libraryCardRoutes.js"]:::api
        RackR["rackRoutes.js"]:::api
        UserR["userRoutes.js"]:::api
    end

    %% Controllers
    subgraph "Controllers"
        AuthC["authController.js"]:::api
        AuthorC["authorController.js"]:::api
        BookC["bookController.js"]:::api
        BookItemC["bookItemController.js"]:::api
        LendingC["lendingController.js"]:::api
        FinesC["finesController.js"]:::api
        CardC["libraryCardController.js"]:::api
        RackC["rackController.js"]:::api
        UserC["userController.js"]:::api
    end

    %% Service Layer
    subgraph "Service Layer"
        AuthorS["authorService.js"]:::service
        BookS["bookService.js"]:::service
        BookItemS["bookItemService.js"]:::service
        FineS["fineService.js"]:::service
        CardS["libraryCardService.js"]:::service
        RackS["rackService.js"]:::service
        UserS["userService.js"]:::service
    end

    %% Data/Model Layer
    subgraph "Data/Model Layer"
        IndexM["models/index.js"]:::data
        AuthorM["models/Author.js"]:::data
        BookM["models/Book.js"]:::data
        BookItemM["models/BookItem.js"]:::data
        LendingM["models/BookLending.js"]:::data
        ReservationM["models/BookReservation.js"]:::data
        CategoryM["models/Category.js"]:::data
        FineM["models/Fine.js"]:::data
        CardM["models/LibraryCard.js"]:::data
        RackM["models/Rack.js"]:::data
        TokenM["models/ResetToken.js"]:::data
        UserM["models/User.js"]:::data
        AuditM["models/AuditLog.js"]:::data
        NotificationM["models/Notification.js"]:::data
        PaymentM["models/Payment.js"]:::data
        DB["Database\n(MongoDB/PostgreSQL/MySQL)"]:::data
        Redis["Redis (Rate Limiter)"]:::data
    end

    %% External Services
    subgraph "External Services"
        Email["Email Service"]:::external
        PaymentGW["Payment Gateway"]:::external
        NotificationService["Notification Service"]:::external
        AuditLogSink["Audit Log Sink"]:::external
    end

    %% Flow Connections
    Client -->|"HTTP(S) Request"| ServerJS
    ServerJS -->|"initialize Express"| AppJS
    AppJS -->|"use middleware pipeline"| AuthM
    AuthM --> RateM --> LogM --> SanitizeM --> ValidateM --> RoleM --> CheckBookM --> ErrorM
    ErrorM -->|"after pipeline"| AuthR
    AuthR --> AuthorR --> BookR --> BookItemR --> LendingR --> FinesR --> CardR --> RackR --> UserR
    AuthR & AuthorR & BookR & BookItemR & LendingR & FinesR & CardR & RackR & UserR --> Controllers
    Controllers --> Services
    Services --> IndexM & AuthorM & BookM & BookItemM & LendingM & ReservationM & CategoryM & FineM & CardM & RackM & TokenM & UserM & AuditM & NotificationM & PaymentM
    IndexM --> DB
    AuthorM & BookM & BookItemM & LendingM & ReservationM & CategoryM & FineM & CardM & RackM & TokenM & UserM & AuditM & NotificationM & PaymentM --> DB
    Services --> Email
    Services --> PaymentGW
    Services --> NotificationService
    Services --> AuditLogSink

    %% Click Events
    click ServerJS "https://github.com/thanhquang2004/library-server/blob/develop/server.js"
    click AppJS "https://github.com/thanhquang2004/library-server/blob/develop/app.js"
    click AuthM "https://github.com/thanhquang2004/library-server/blob/develop/middlewares/authMiddleware.js"
    click RateM "https://github.com/thanhquang2004/library-server/blob/develop/middlewares/rateLimiter.js"
    click LogM "https://github.com/thanhquang2004/library-server/blob/develop/middlewares/logMiddleware.js"
    click SanitizeM "https://github.com/thanhquang2004/library-server/blob/develop/middlewares/sanitizeMiddleware.js"
    click ValidateM "https://github.com/thanhquang2004/library-server/blob/develop/middlewares/validateRequest.js"
    click RoleM "https://github.com/thanhquang2004/library-server/blob/develop/middlewares/roleMiddleware.js"
    click ErrorM "https://github.com/thanhquang2004/library-server/blob/develop/middlewares/errorHandler.js"
    click CheckBookM "https://github.com/thanhquang2004/library-server/blob/develop/middlewares/checkBookAvailability.js"
    click AuthR "https://github.com/thanhquang2004/library-server/blob/develop/routes/authRoutes.js"
    click AuthorR "https://github.com/thanhquang2004/library-server/blob/develop/routes/authorRoutes.js"
    click BookR "https://github.com/thanhquang2004/library-server/blob/develop/routes/bookRoutes.js"
    click BookItemR "https://github.com/thanhquang2004/library-server/blob/develop/routes/bookItemRoutes.js"
    click LendingR "https://github.com/thanhquang2004/library-server/blob/develop/routes/lendingRoutes.js"
    click FinesR "https://github.com/thanhquang2004/library-server/blob/develop/routes/finesRoutes.js"
    click CardR "https://github.com/thanhquang2004/library-server/blob/develop/routes/libraryCardRoutes.js"
    click RackR "https://github.com/thanhquang2004/library-server/blob/develop/routes/rackRoutes.js"
    click UserR "https://github.com/thanhquang2004/library-server/blob/develop/routes/userRoutes.js"
    click AuthC "https://github.com/thanhquang2004/library-server/blob/develop/controllers/authController.js"
    click AuthorC "https://github.com/thanhquang2004/library-server/blob/develop/controllers/authorController.js"
    click BookC "https://github.com/thanhquang2004/library-server/blob/develop/controllers/bookController.js"
    click BookItemC "https://github.com/thanhquang2004/library-server/blob/develop/controllers/bookItemController.js"
    click LendingC "https://github.com/thanhquang2004/library-server/blob/develop/controllers/lendingController.js"
    click FinesC "https://github.com/thanhquang2004/library-server/blob/develop/controllers/finesController.js"
    click CardC "https://github.com/thanhquang2004/library-server/blob/develop/controllers/libraryCardController.js"
    click RackC "https://github.com/thanhquang2004/library-server/blob/develop/controllers/rackController.js"
    click UserC "https://github.com/thanhquang2004/library-server/blob/develop/controllers/userController.js"
    click AuthorS "https://github.com/thanhquang2004/library-server/blob/develop/services/authorService.js"
    click BookS "https://github.com/thanhquang2004/library-server/blob/develop/services/bookService.js"
    click BookItemS "https://github.com/thanhquang2004/library-server/blob/develop/services/bookItemService.js"
    click FineS "https://github.com/thanhquang2004/library-server/blob/develop/services/fineService.js"
    click CardS "https://github.com/thanhquang2004/library-server/blob/develop/services/libraryCardService.js"
    click RackS "https://github.com/thanhquang2004/library-server/blob/develop/services/rackService.js"
    click UserS "https://github.com/thanhquang2004/library-server/blob/develop/services/userService.js"
    click IndexM "https://github.com/thanhquang2004/library-server/blob/develop/models/index.js"
    click AuthorM "https://github.com/thanhquang2004/library-server/blob/develop/models/Author.js"
    click BookM "https://github.com/thanhquang2004/library-server/blob/develop/models/Book.js"
    click BookItemM "https://github.com/thanhquang2004/library-server/blob/develop/models/BookItem.js"
    click LendingM "https://github.com/thanhquang2004/library-server/blob/develop/models/BookLending.js"
    click ReservationM "https://github.com/thanhquang2004/library-server/blob/develop/models/BookReservation.js"
    click CategoryM "https://github.com/thanhquang2004/library-server/blob/develop/models/Category.js"
    click FineM "https://github.com/thanhquang2004/library-server/blob/develop/models/Fine.js"
    click CardM "https://github.com/thanhquang2004/library-server/blob/develop/models/LibraryCard.js"
    click RackM "https://github.com/thanhquang2004/library-server/blob/develop/models/Rack.js"
    click TokenM "https://github.com/thanhquang2004/library-server/blob/develop/models/ResetToken.js"
    click UserM "https://github.com/thanhquang2004/library-server/blob/develop/models/User.js"
    click AuditM "https://github.com/thanhquang2004/library-server/blob/develop/models/AuditLog.js"
    click NotificationM "https://github.com/thanhquang2004/library-server/blob/develop/models/Notification.js"
    click PaymentM "https://github.com/thanhquang2004/library-server/blob/develop/models/Payment.js"
    click Email "https://github.com/thanhquang2004/library-server/blob/develop/utils/email.js"
    click PaymentGW "https://github.com/thanhquang2004/library-server/blob/develop/services/paymentService.js"
    click NotificationService "https://github.com/thanhquang2004/library-server/blob/develop/utils/email.js"
    click AuditLogSink "https://github.com/thanhquang2004/library-server/blob/develop/models/AuditLog.js"

    %% Styles
    classDef api fill:#D0E8FF,stroke:#0066CC,color:#000;
    classDef service fill:#DFFFD0,stroke:#33AA00,color:#000;
    classDef data fill:#FFE8B0,stroke:#CC6600,color:#000;
    classDef external fill:#E0E0E0,stroke:#888888,color:#000;
```
