# crafael23/sistema-bibliotecario architecture

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

- Interactive diagram: https://gitdiagram.com/crafael23/sistema-bibliotecario
- Repository: https://github.com/crafael23/sistema-bibliotecario

## Overview

##### Project Type and Purpose
- This is a full-stack web application built with Next.js 13’s app router.
- It serves as a library management system (“Sistema Bibliotecario”) with two main user roles: administrators (who manage books, inventory, reports, reservations, and users) and end-users (who browse, reserve, and view their profile).
- It uses Drizzle ORM to interact with a relational database (likely PostgreSQL), Uploadthing for file uploads, and custom server-side code under `src/server`.

##### File Structure Analysis
- Top-level config and dependency files (.env, tsconfig.json, tailwind.config.ts, drizzle.config.ts).
- `src/app`: Next.js app router – comprised of separate routes for `/admin`, `/homepage`, `/usuario` and API endpoints under `src/app/api`.
- `src/app/admin`: Admin UI pages (book addition, inventory, reports, reservations, user management).
- `src/app/homepage`: Public catalog/browsing and user profile.
- `src/app/usuario`: New-user onboarding flow.
- `src/components` + `src/components/ui`: Shared UI primitives and global components.
- `src/hooks`, `src/lib`, `src/utils`: Reusable client-side utilities and hooks.
- `src/server/db`: Server-side database schema, queries, seeding, demo data.
- API routes under `src/app/api`: Uploadthing integration, user-specific fines & reservations, webhooks.

##### Key Architectural Patterns
- Layered architecture:
- Presentation layer: Next.js pages & React components (split into server vs. client components).
- Application/API layer: Next.js server actions and API routes.
- Data layer: Drizzle ORM + raw SQL queries + repository pattern in `src/server/db`.
- Next.js app-router conventions (layouts, page.tsx, loading.tsx per route).
- Shared component library follows an atomic design approach.
- Use of React Server Components (RSC) for data fetching and UI hydration.

##### Main Components
a. Client (Browser)
- Administrator UI (under `/admin`)
- Public UI (catalog & profile under `/homepage`)
b. Next.js Application (Server + API)
- App Router: route handlers & server actions
- React Server Components & Client Components
- API Routes: `/api/uploadthing`, `/api/user`, `/api/webhooks`
c. Database
- PostgreSQL (Drizzle ORM + SQL schema + queries + seeding)
d. File Storage Service
- Uploadthing (handles image/book cover uploads)
e. External Integrations (optional)
- Webhooks (incoming events)

##### Relationships & Data Flows
- Client → Next.js pages (SSR/SSG/Client)
- Server actions & API routes → Drizzle ORM → PostgreSQL
- Upload requests → Uploadthing service → storage bucket
- Server receives webhooks → Next.js API → internal processing → Database
- Admin UI requests reservation/report data → Next.js server actions → DB queries

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Layer 1: Clients
  subgraph "Layer 1: Clients"
    AdminUI["Administrator UI"]:::ui
    PublicUI["Public UI"]:::ui
    UserOnboarding["Usuario Onboarding UI"]:::ui
  end

  %% Layer 2: Next.js Application
  subgraph "Layer 2: Next.js Application"
    Router["App Router\n(layout.tsx, page.tsx)"]:::server

    subgraph "Presentation Layer"
      AdminPages["Admin Pages\n(src/app/admin)"]:::server
      HomepagePages["Homepage Pages\n(src/app/homepage)"]:::server
      UsuarioPages["Usuario Pages\n(src/app/usuario)"]:::server
    end

    subgraph "Server Actions & Routes"
      AddBook["Add Book Action"]:::server
      Inventory["Inventory Action"]:::server
      Reports["Reports Action"]:::server
      ReservationsAction["Reservations Action"]:::server
      UserMgmt["User Management Action"]:::server
      BookDetails["Book Details Page"]:::server
      Profile["Profile Page"]:::server
    end

    subgraph "API Layer"
      UploadAPI["Uploadthing API\n(/api/uploadthing)"]:::server
      UserAPI["User API\n(/api/user)"]:::server
      WebhookAPI["Webhooks API\n(/api/webhooks)"]:::server
    end

    Drizzle["Drizzle ORM Layer"]:::db
  end

  %% Layer 3: Data Stores & Services
  subgraph "Layer 3: Data Stores & Services"
    Database["PostgreSQL Database"]:::db
    Storage["Uploadthing Storage"]:::service
  end

  %% Shared Resources
  subgraph "Shared Resources"
    SharedUI["Shared UI Components"]:::ui
    SharedUIUI["UI Primitives (components/ui)"]:::ui
    Hooks["Client Hooks (src/hooks)"]:::utility
    LibHooks["Lib Hooks (src/lib/hooks)"]:::utility
    Lib["Utilities (src/lib)"]:::utility
    Utils["Utils (src/utils)"]:::utility
    UploadUtil["Uploadthing Utility"]:::service
  end

  %% Config & Styles
  subgraph "Config & Styles"
    DrizzleConfig["drizzle.config.ts"]:::config
    TailwindConfig["tailwind.config.ts"]:::config
    GlobalCSS["Global CSS\n(src/styles/globals.css)"]:::config
    AppGlobals["App Globals CSS\n(src/app/globals.css)"]:::config
    TSConfig["tsconfig.json"]:::config
  end

  %% Connections: Clients to Router
  AdminUI -->|navigate| Router
  PublicUI -->|navigate| Router
  UserOnboarding -->|navigate| Router

  %% Router to Presentation
  Router --> AdminPages
  Router --> HomepagePages
  Router --> UsuarioPages

  %% Presentation to Server Actions
  AdminPages --> AddBook
  AdminPages --> Inventory
  AdminPages --> Reports
  AdminPages --> ReservationsAction
  AdminPages --> UserMgmt
  HomepagePages --> BookDetails
  HomepagePages --> Profile

  %% Server Actions & API to Drizzle
  AddBook -->|"insert/update"| Drizzle
  Inventory -->|"query"| Drizzle
  Reports -->|"reportQueries"| Drizzle
  ReservationsAction -->|"manageReservations"| Drizzle
  UserMgmt -->|"manageUsers"| Drizzle
  BookDetails -->|"getBook"| Drizzle
  Profile -->|"getProfile"| Drizzle
  UploadAPI -->|"getUploadUrl"| Storage
  UploadAPI -->|"recordUpload"| Drizzle
  UserAPI -->|"getFinesReservations"| Drizzle
  WebhookAPI -->|"processWebhook"| Drizzle

  %% Drizzle to Database
  Drizzle -->|"SQL Queries"| Database

  %% Shared resources usage
  AdminPages --> SharedUI
  HomepagePages --> SharedUI
  UsuarioPages --> SharedUI
  SharedUI --> SharedUIUI
  SharedUI --> Hooks
  SharedUI --> Lib
  Hooks --> LibHooks
  Lib --> Utils
  UploadAPI --> UploadUtil

  %% Config dependencies
  Drizzle --> DrizzleConfig
  SharedUIUI --> TailwindConfig
  AdminPages --> GlobalCSS
  Router --> AppGlobals
  Router --> TSConfig

  %% Click Events
  click AdminUI "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/admin"
  click PublicUI "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/homepage"
  click UserOnboarding "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/usuario"
  click Router "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/app/layout.tsx"
  click AdminPages "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/admin"
  click AddBook "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/admin/agregar-libro"
  click Inventory "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/admin/inventario"
  click Reports "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/admin/reportes"
  click ReservationsAction "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/admin/reservas"
  click UserMgmt "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/admin/usuarios"
  click HomepagePages "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/homepage"
  click BookDetails "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/homepage/[libroId]"
  click Profile "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/homepage/perfil"
  click UploadAPI "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/api/uploadthing"
  click UserAPI "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/api/user"
  click WebhookAPI "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/app/api/webhooks"
  click SharedUI "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/components"
  click SharedUIUI "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/components/ui"
  click Hooks "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/hooks"
  click LibHooks "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/lib/hooks"
  click Lib "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/lib"
  click Utils "https://github.com/crafael23/sistema-bibliotecario/tree/main/src/utils"
  click UploadUtil "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/utils/uploadthing.ts"
  click DrizzleConfig "https://github.com/crafael23/sistema-bibliotecario/blob/main/drizzle.config.ts"
  click TailwindConfig "https://github.com/crafael23/sistema-bibliotecario/blob/main/tailwind.config.ts"
  click GlobalCSS "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/styles/globals.css"
  click AppGlobals "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/app/globals.css"
  click TSConfig "https://github.com/crafael23/sistema-bibliotecario/blob/main/tsconfig.json"
  click Schema "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/server/db/schema.ts"
  click Queries "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/server/db/queries.ts"
  click ReportQueries "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/server/db/report-queries.ts"
  click DBIndex "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/server/db/index.ts"
  click Seeding "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/server/db/seeding.ts"
  click DemoData "https://github.com/crafael23/sistema-bibliotecario/blob/main/src/server/db/demo-data.ts"

  %% Styles
  classDef ui fill:#cfe2ff,stroke:#084298,color:#084298
  classDef server fill:#d1e7dd,stroke:#0f5132,color:#0f5132
  classDef db fill:#fff3cd,stroke:#664d03,color:#664d03
  classDef service fill:#fde2e4,stroke:#b02a37,color:#b02a37
  classDef utility fill:#e2e3e5,stroke:#6c757d,color:#6c757d
  classDef config fill:#f8d7da,stroke:#842029,color:#842029
```
