# adrianhajdin/university-library-jsm architecture

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

- Interactive diagram: https://gitdiagram.com/adrianhajdin/university-library-jsm
- Repository: https://github.com/adrianhajdin/university-library-jsm

## Overview

##### Project Type and Purpose
- This is a full‑stack web application: “University Library Management System with Admin Panel.”
- Built in Next.js 13 (App Router) + TypeScript. It serves both public users (browse, borrow books) and admins (manage books/users, view analytics), with built‑in authentication, automated email workflows, media uploads, caching, and rate‑limiting.

##### File Structure
- app/
- (root): public‑facing pages (home, library, book details, profile)
- (auth): sign‑in/sign‑up flows
- admin: admin dashboard, user/book/borrow‑records pages
- api: Next.js API Routes (auth, imagekit, workflows)
- components/: shared UI controls
- lib/: business logic (actions, validations, workflows, rate‑limit)
- database/: Drizzle ORM setup, schema, migrations, seed scripts
- config files: next.config.ts, tailwind.config.ts, middleware.ts
- hooks/, styles/, constants/, types.d.ts

##### README for Insights
- Tech Stack: Next.js, PostgreSQL (Neon), Upstash Redis & QStash, ImageKit, Resend Emails, Drizzle ORM, Tailwind CSS, NextAuth.
- Features: authentication, book discovery, borrowing workflows (reminders & receipts), admin user/role management, analytics, caching, DDoS protection.

##### Main Components & Interactions
a. Client (Browser)
- Renders React pages (public & admin) via Next.js App Router.
- Calls API Endpoints for data & actions.
b. Next.js Server
- Frontend SSR/SSG and API Routes:
- /api/auth → NextAuth (session, JWT)
- /api/imagekit → media upload proxy
- /api/workflows/onboarding → QStash‑triggered jobs
- Middleware for rate‑limiting & DDoS protection.
- Business logic in lib/actions and lib/admin/actions.
- Drizzle ORM client for DB operations.
c. Persistence
- PostgreSQL (Neon) for core data (users, books, borrow records).
- Upstash Redis for caching, rate-limit counters, and QStash (message queue) for scheduling.
d. External Services
- ImageKit for media storage & optimization.
- Resend for sending emails (onboarding, reminders, PDF receipts).
- QStash for scheduled workflows (automated emails).

Interaction Flow:
1. User → Next.js UI (SSR/CSR)
2. UI → Next.js API (HTTP)
3. API → PostgreSQL (Drizzle ORM) and/or Redis
4. API → ImageKit / Resend / QStash
5. QStash → Next.js API → Resend for scheduled emails

##### Architectural Patterns & Principles
- Monolithic Next.js “front + back” co‑located in one codebase.
- Clear separation of route scopes: public, auth, admin.
- CQRS style: lib/actions for commands, Drizzle ORM queries for reads.
- Event‑driven workflows via QStash.
- Modular design: components, hooks, lib, database.

##### Essential Elements
- Route group boundaries within the Next.js container.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer" 
        direction TB
        Browser["Browser (React UI)"]:::frontend
    end

    %% Application Layer
    subgraph "Next.js Server" 
        direction TB

        subgraph "Page Routes" 
            direction TB
            Public["Public Pages"]:::frontend
            AuthPages["Auth Pages"]:::frontend
            AdminPages["Admin Panel"]:::frontend
        end

        subgraph "API Routes" 
            direction TB
            AuthAPI["/api/auth"]:::backend
            ImageKitAPI["/api/imagekit"]:::backend
            WorkflowAPI["/api/workflows/onboarding"]:::backend
        end

        Middleware["Middleware & Rate-limit"]:::backend
        BusinessLogic["Business Logic (lib/)"]:::backend
        ORMClient["Drizzle ORM Client"]:::backend
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        Postgres["PostgreSQL (Neon)"]:::db
        Redis["Upstash Redis"]:::db
    end

    %% External Services
    subgraph "External Services"
        direction TB
        ImageKitExt["ImageKit"]:::ext
        Resend["Resend (Email)"]:::ext
        QStash["QStash"]:::ext
    end

    %% Connections
    Browser -->|"SSR/CSR"| Public
    Browser -->|"SSR/CSR"| AuthPages
    Browser -->|"SSR/CSR"| AdminPages

    Public -->|"HTTP REST"| AuthAPI
    AuthPages -->|"HTTP REST"| AuthAPI
    AdminPages -->|"HTTP REST"| AuthAPI

    Browser -->|"HTTP REST"| ImageKitAPI
    Browser -->|"HTTP REST"| WorkflowAPI

    AuthAPI -->|"ORM (Drizzle)"| Postgres
    ImageKitAPI -->|"Proxy"| ImageKitExt
    ImageKitAPI -->|"ORM (Drizzle)"| Postgres
    WorkflowAPI -->|"Redis/QStash"| Redis
    WorkflowAPI -->|"Send"| Resend

    QStash -->|"Webhook"| WorkflowAPI

    AuthAPI -->|"Cache & Rate-limit"| Redis
    Middleware -->|"Enforce"| AuthAPI
    Middleware -->|"Enforce"| ImageKitAPI
    Middleware -->|"Enforce"| WorkflowAPI

    BusinessLogic --> AuthAPI
    BusinessLogic --> ImageKitAPI
    BusinessLogic --> WorkflowAPI
    ORMClient --> Postgres

    %% Click Events - Page Routes
    click Public "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/(root)/page.tsx"
    click Public "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/(root)/books/page.tsx"
    click Public "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/(root)/books/[id]/page.tsx"
    click Public "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/(root)/my-profile/page.tsx"

    click AuthPages "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/(auth)/sign-in/page.tsx"
    click AuthPages "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/(auth)/sign-up/page.tsx"

    click AdminPages "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/admin/layout.tsx"
    click AdminPages "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/admin/page.tsx"
    click AdminPages "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/admin/books/page.tsx"
    click AdminPages "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/admin/books/new/page.tsx"

    %% Click Events - API Routes
    click AuthAPI "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/api/auth/[...nextauth]/route.ts"
    click ImageKitAPI "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/api/imagekit/route.ts"
    click WorkflowAPI "https://github.com/adrianhajdin/university-library-jsm/blob/main/app/api/workflows/onboarding/route.ts"

    %% Click Events - Business Logic & Database
    click BusinessLogic "https://github.com/adrianhajdin/university-library-jsm/blob/main/lib/actions/auth.ts"
    click BusinessLogic "https://github.com/adrianhajdin/university-library-jsm/blob/main/lib/actions/book.ts"
    click BusinessLogic "https://github.com/adrianhajdin/university-library-jsm/blob/main/lib/admin/actions/book.ts"
    click BusinessLogic "https://github.com/adrianhajdin/university-library-jsm/blob/main/lib/workflow.ts"
    click BusinessLogic "https://github.com/adrianhajdin/university-library-jsm/blob/main/lib/ratelimit.ts"
    click BusinessLogic "https://github.com/adrianhajdin/university-library-jsm/blob/main/lib/validations.ts"

    click ORMClient "https://github.com/adrianhajdin/university-library-jsm/blob/main/database/drizzle.ts"
    click Postgres "https://github.com/adrianhajdin/university-library-jsm/blob/main/database/schema.ts"
    click Postgres "https://github.com/adrianhajdin/university-library-jsm/blob/main/database/seed.ts"
    click Redis "https://github.com/adrianhajdin/university-library-jsm/blob/main/database/redis.ts"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef backend  fill:#E6FFED,stroke:#28a745,color:#28a745;
    classDef db       fill:#FFF5E5,stroke:#FFA500,color:#FFA500;
    classDef ext      fill:#F3E8FF,stroke:#8A2BE2,color:#8A2BE2;
```
