# duthanhduoc/nextjs-free architecture

> Architecture diagram of the GitHub repository duthanhduoc/nextjs-free, 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/duthanhduoc/nextjs-free
- Repository: https://github.com/duthanhduoc/nextjs-free

## Overview

##### Main Components

a. Frontend Client:
- Built with Next.js and organized under the client directory.
- Contains the Next.js “app” directory with various pages for authentication (login, register, logout), product operations (list, add, edit, view), and shared UI components.
- Handles API requests (see folder “src/apiRequests”) and static assets (public/images).

b. Backend Server:
- Located under the server directory and built with Fastify.
- Contains a set of controllers (e.g., account.controller, auth.controller, product.controller) and routes (e.g., auth.route, product.route) which define RESTful endpoints.
- Includes plugins for common functionalities such as authentication, error handling, and request validation.
- Uses a middleware architecture (noting use of hooks in auth.hooks.ts).

c. Database:
- Managed via Prisma (found in the server/prisma folder) with a schema that supports development (evidenced by dev.db and schema.prisma).
- Acts as the persistent storage for user credentials, product information, and any other business data.

##### Relationships and Interactions

a. The user interacts with the frontend client (Next.js application).
b. The client makes HTTP requests (using the API requests layer in client/src/apiRequests) to the backend server’s RESTful endpoints.
c. The backend server processes these requests by invoking the appropriate controllers, applying authentication, validation (via schema validations in both client and server), and business logic.
d. Backend controllers interact with the database via Prisma, performing CRUD operations on user profiles, product listings, etc.
e. Responses and data flows travel back from the server to the client, where the UI is updated accordingly.

##### Incorporate Architectural Patterns and Technologies

a. Client-Side Routing and Server-side Rendering:
- Next.js handles both client and server rendering (there are even API endpoints under client/src/app/api).
b. MVC-like Pattern on the Backend:
- Controllers manage the business logic.
- Routes define endpoints.
- Database access is mediated via an ORM (Prisma).
c. Authentication and Middleware:
- Both client and server maintain schema validations and authentication flows.
- The server includes plugins and hooks for authentication and error-handling.
d. Build & Deployment:
- There are ecosystem configuration files (ecosystem.config.js, nodemon.json) and configuration files (e.g., .eslintrc, tailwind.config.ts) to streamline development and deployment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Client Block
    subgraph "Frontend Client (Next.js)"
        F1["Pages & API Endpoints"]:::frontend
        F2["Authentication Pages"]:::frontend
        F3["Product Pages"]:::frontend
        F4["API Request Modules"]:::frontend
        F5["Static Assets"]:::frontend
        F6["UI Components (Core)"]:::frontend
    end

    %% Backend Server Block
    subgraph "Backend Server (Fastify)"
        B1["Controllers"]:::backend
        B2["Routes"]:::backend
        B3["Middleware/Hooks"]:::backend
        B4["Plugins"]:::backend
    end

    %% Database Block
    subgraph "Database (Prisma Managed)"
        D1["Prisma Schema & Database"]:::database
    end

    %% Connections between Frontend and Backend (Client to API)
    F1 -->|"HTTP_API_calls"| B2
    F2 -->|"HTTP_API_calls"| B2
    F3 -->|"HTTP_API_calls"| B2
    F4 -->|"HTTP_API_calls"| B2

    %% Connection from Backend to Database
    B1 -->|"CRUD_operations"| D1

    %% Return Flow from Backend to Frontend (Response)
    B2 -->|"Response_data"| F1

    %% Click Events for Frontend Client Components
    click F1 "https://github.com/duthanhduoc/nextjs-free/tree/main/client/src/app"
    click F2 "https://github.com/duthanhduoc/nextjs-free/tree/main/client/src/app/(auth)"
    click F3 "https://github.com/duthanhduoc/nextjs-free/tree/main/client/src/app/products"
    click F4 "https://github.com/duthanhduoc/nextjs-free/tree/main/client/src/apiRequests"
    click F5 "https://github.com/duthanhduoc/nextjs-free/tree/main/client/public/images"
    click F6 "https://github.com/duthanhduoc/nextjs-free/tree/main/client/src/components"

    %% Additional click event for UI Components (Alternate UI Components path)
    subgraph "Additional UI Components"
        F7["UI Components (Alternate)"]:::frontend
    end
    click F7 "https://github.com/duthanhduoc/nextjs-free/tree/main/client/src/components/ui"

    %% Click Events for Backend Server Components
    click B1 "https://github.com/duthanhduoc/nextjs-free/tree/main/server/src/controllers"
    click B2 "https://github.com/duthanhduoc/nextjs-free/tree/main/server/src/routes"
    click B3 "https://github.com/duthanhduoc/nextjs-free/tree/main/server/src/hooks"
    click B4 "https://github.com/duthanhduoc/nextjs-free/tree/main/server/src/plugins"

    %% Click Event for Database
    click D1 "https://github.com/duthanhduoc/nextjs-free/tree/main/server/prisma"

    %% Styling Definitions
    classDef frontend fill:#1E90FF,stroke:#000,stroke-width:2px,color:#fff;
    classDef backend fill:#32CD32,stroke:#000,stroke-width:2px,color:#fff;
    classDef database fill:#808080,stroke:#000,stroke-width:2px,color:#fff;
```
