# am17jx/book_review architecture

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

- Interactive diagram: https://gitdiagram.com/am17jx/book_review
- Repository: https://github.com/am17jx/book_review

## Overview

##### Project Type and Purpose
- This is a single-service, monolithic REST API for managing books, users, comments, and tags.
- Built on Node.js/Express and backed by a PostgreSQL database.
- Provides JWT authentication, role-based access control, and robust error handling.

##### File Structure
- Top-level entry:
- server.js – loads environment, starts HTTP server
- app.js – configures Express app, global middleware, and error handlers
- config/ – database connection (config/DataBase.js)
- controllers/ – business logic for each resource (books, users, comments, tags, auth)
- routes/ – Express routers mapping HTTP endpoints to controller functions
- utils/ – AppError (custom error class) and catchAsync (async wrapper)
- package.json – dependencies (Express, pg, JWT, bcrypt, helmet, rate-limit, xss-clean)

##### README for Insights
- Core domains: Users, Books, Comments, Tags
- Security stack: JWT auth, bcrypt hashing, helmet, rate limiting, XSS sanitization
- DB schema: users, books, comments, tags, book_tag junction table
- Access rules: owner-only updates, admin-only deletes, protected routes

##### Main Components
a. Clients
- Web or Mobile clients (Postman, front-end apps)
b. API Layer
- Express App (app.js)
- Global middleware: helmet, rate-limit, xss-clean, JSON body parser
- Auth middleware (JWT verification, role checks)
c. Routing Layer
- /api/users, /api/books, /api/comment, /api/tag routers
d. Controller Layer (controllers/*.js)
- authController, userController, bookController, commentController, tagController
- Use catchAsync to wrap async functions, throw AppError on failures
e. Data Access / Model Layer
- config/DataBase.js → pg client or pool connecting to PostgreSQL
- Direct SQL queries or ORM calls (if any)
f. Database
- PostgreSQL instance with five tables: users, books, comments, tags, book_tag
g. Utilities
- AppError for standardized error responses
- catchAsync for error propagation
h. External Libraries/Services
- jsonwebtoken (JWT generation/validation)
- bcryptjs (password hashing)
- dotenv (env vars)

##### Relationships & Data Flows
- Client → HTTP Request → Express Middleware → Router → Controller → Database → Controller → JSON Response → Client
- Authenticated routes use JWT middleware before routing.
- Role-checks (owner vs. admin) happen in controller or middleware immediately after auth.
- Controllers call DB via DataBase.js; results flow back upstream.
- Errors thrown by controllers propagate through Express’s centralized error handler.

##### Architectural Patterns & Principles
- Layered (Controller → Data Access → Database)
- Modular (“separation of concerns” for routes, controllers, config, utils)
- Middleware pipeline for cross-cutting concerns (security headers, rate limiting, XSS)
- Centralized error handling with a custom error class and async wrapper

## Diagram (Mermaid)

```mermaid
flowchart TD
    Server["server.js"]:::bootstrap
    App["Express App<br/>(app.js)"]:::api
    subgraph "Security & Parsing"
        Helmet["Helmet"]:::middleware
        RateLimiter["Rate Limiter"]:::middleware
        XSS["XSS Clean"]:::middleware
        BodyParser["JSON Body Parser"]:::middleware
    end
    AuthMiddleware["Auth Middleware<br/>(JWT)"]:::middleware

    subgraph "Routing"
        UserRoutes["User Routes<br/>(routes/userRoutes.js)"]:::routes
        BookRoutes["Book Routes<br/>(routes/bookrotes.js)"]:::routes
        CommentRoutes["Comment Routes<br/>(routes/commentRoutes.js)"]:::routes
        TagRoutes["Tag Routes<br/>(routes/tagroutes.js)"]:::routes
    end

    subgraph "Business Logic"
        AuthController["Auth Controller<br/>(controllers/authController.js)"]:::controller
        UserController["User Controller<br/>(controllers/userController.js)"]:::controller
        BookController["Book Controller<br/>(controllers/Bookcontrollers.js)"]:::controller
        CommentController["Comment Controller<br/>(controllers/commentController.js)"]:::controller
        TagController["Tag Controller<br/>(controllers/tagController.js)"]:::controller
    end

    subgraph "Data Access Layer"
        DBConfig["DataBase.js<br/>(config/DataBase.js)"]:::datalayer
    end

    DB["PostgreSQL"]:::db

    subgraph "Utilities"
        AppError["AppError<br/>(utils/AppError.js)"]:::utility
        CatchAsync["catchAsync<br/>(utils/catchAsync.js)"]:::utility
    end

    subgraph "External Libraries"
        JWT["jsonwebtoken"]:::external
        Bcrypt["bcryptjs"]:::external
        Dotenv["dotenv"]:::external
        Package["package.json"]:::external
    end

    Client["Web/Mobile Client"]:::client

    Client -->|"HTTP Request"| App
    Server -->|"requires app.js"| App

    App -->|"helmet()"| Helmet
    Helmet -->|"rateLimit()"| RateLimiter
    RateLimiter -->|"xssClean()"| XSS
    XSS -->|"bodyParser.json()"| BodyParser
    BodyParser -->|"JWT.verify"| AuthMiddleware

    AuthMiddleware -->|"route request"| UserRoutes
    AuthMiddleware --> UserRoutes
    AuthMiddleware --> BookRoutes
    AuthMiddleware --> CommentRoutes
    AuthMiddleware --> TagRoutes

    UserRoutes -->|"handle /api/users"| UserController
    BookRoutes -->|"handle /api/books"| BookController
    CommentRoutes -->|"handle /api/comments"| CommentController
    TagRoutes -->|"handle /api/tags"| TagController

    AuthController -->|uses| CatchAsync
    UserController -->|uses| CatchAsync
    BookController -->|uses| CatchAsync
    CommentController -->|uses| CatchAsync
    TagController -->|uses| CatchAsync

    AuthController -->|throws| AppError
    UserController -->|throws| AppError
    BookController -->|throws| AppError
    CommentController -->|throws| AppError
    TagController -->|throws| AppError

    AuthController -->|SQL queries| DBConfig
    UserController -->|SQL queries| DBConfig
    BookController -->|SQL queries| DBConfig
    CommentController -->|SQL queries| DBConfig
    TagController -->|SQL queries| DBConfig

    DBConfig -->|"connects to"| DB

    AuthController -->|"JWT gen/verify"| JWT
    UserController -->|"password hash"| Bcrypt
    App -->|"loads env"| Dotenv

    classDef client fill:#f9f,stroke:#333,stroke-width:1px;
    classDef api fill:#bbf,stroke:#333,stroke-width:1px;
    classDef middleware fill:#cdf,stroke:#333,stroke-width:1px;
    classDef routes fill:#dcd,stroke:#333,stroke-width:1px;
    classDef controller fill:#bfb,stroke:#333,stroke-width:1px;
    classDef datalayer fill:#fb8,stroke:#333,stroke-width:1px;
    classDef db fill:#f90,stroke:#333,stroke-width:1px;
    classDef utility fill:#afa,stroke:#333,stroke-width:1px;
    classDef external fill:#eee,stroke:#333,stroke-width:1px;
    classDef bootstrap fill:#bbf,stroke:#333,stroke-width:1px;

    click Server "https://github.com/am17jx/book_review/blob/main/server.js"
    click App "https://github.com/am17jx/book_review/blob/main/app.js"
    click DBConfig "https://github.com/am17jx/book_review/blob/main/config/DataBase.js"
    click AuthController "https://github.com/am17jx/book_review/blob/main/controllers/authController.js"
    click UserController "https://github.com/am17jx/book_review/blob/main/controllers/userController.js"
    click BookController "https://github.com/am17jx/book_review/blob/main/controllers/Bookcontrollers.js"
    click CommentController "https://github.com/am17jx/book_review/blob/main/controllers/commentController.js"
    click TagController "https://github.com/am17jx/book_review/blob/main/controllers/tagController.js"
    click UserRoutes "https://github.com/am17jx/book_review/blob/main/routes/userRoutes.js"
    click BookRoutes "https://github.com/am17jx/book_review/blob/main/routes/bookrotes.js"
    click CommentRoutes "https://github.com/am17jx/book_review/blob/main/routes/commentRoutes.js"
    click TagRoutes "https://github.com/am17jx/book_review/blob/main/routes/tagroutes.js"
    click AppError "https://github.com/am17jx/book_review/blob/main/utils/AppError.js"
    click CatchAsync "https://github.com/am17jx/book_review/blob/main/utils/catchAsync.js"
    click Package "https://github.com/am17jx/book_review/blob/main/package.json"
```
