# rashmeet0099/fast_api_postgres architecture

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

- Interactive diagram: https://gitdiagram.com/rashmeet0099/fast_api_postgres
- Repository: https://github.com/rashmeet0099/fast_api_postgres

## Overview

##### Project Type and Purpose
- This is a backend-only, RESTful CRUD API for a bookstore.
- Built with FastAPI (async web framework), SQLAlchemy ORM for PostgreSQL, and Pydantic for request/response validation.
- Exposes OpenAPI docs (Swagger UI) and supports standard CRUD endpoints for managing “Book” records.

##### File Structure Analysis
- Top-level Python modules:
- main.py       → FastAPI app instance + route definitions
- services.py   → Business logic layer (CRUD service functions)
- models.py     → SQLAlchemy ORM models (Book table)
- schemas.py    → Pydantic request/response schemas
- db.py         → Database connection configuration and session management
- __init__.py   → Package marker
- requirements.txt → Dependency list
- No separate “routers” or “controllers” folder: routing lives in main.py.
- Single-process, monolithic backend—no microservices.

##### Key Architectural Patterns
- Layered (Onion) Architecture:
- API/Controller layer (main.py)
- Service layer (services.py)
- Data layer (models.py + db.py)
- Validation layer (schemas.py)
- Async I/O: all endpoint handlers and DB sessions use asynchronous patterns (async/await).
- Auto-generated documentation via FastAPI’s OpenAPI integration.

##### Main Components to Illustrate
A. API Client (e.g., browser, Postman, mobile app)
B. FastAPI Application
- Router endpoints in main.py
- Swagger UI (/docs)
C. Service Layer (services.py)
- CRUD functions called by endpoints
D. Data Access Layer
- SQLAlchemy models (models.py)
- Session management (db.py)
E. PostgreSQL Database (external service)

##### Component Relationships and Data Flow
1. Client sends HTTP request (GET/POST/PUT/DELETE) → FastAPI endpoint in main.py
2. Endpoint handler:
- Parses and validates input via Pydantic schemas.py
- Calls corresponding service function in services.py
3. Service function:
- Uses SQLAlchemy session from db.py
- Reads/writes ORM models (models.py) against PostgreSQL
4. Database executes SQL → results returned to SQLAlchemy → mapped to Python objects
5. Service returns domain objects → endpoint transforms to Pydantic schema → JSON response to client

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client"
        Client["API Client (Browser/Postman)"]:::external
    end

    %% API Layer
    subgraph "API Layer"
        main["main.py\n(FastAPI App)"]:::api
        swagger["Swagger UI (/docs)"]:::api
    end

    %% Validation Layer
    subgraph "Validation Layer"
        schemas["schemas.py\n(Pydantic)"]:::validation
    end

    %% Service Layer
    subgraph "Service Layer"
        services["services.py\n(Business Logic)"]:::service
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        models["models.py\n(SQLAlchemy ORM)"]:::data
        db["db.py\n(DB Session)"]:::data
    end

    %% External Database
    PostgreSQL[(PostgreSQL Database)]:::external

    %% Data Flow
    Client -->|"HTTP request\n(async call)"| main
    main -->|"validates input"| schemas
    main -->|"calls service\n(async)"| services
    main -.->|"HTTP GET /docs"| swagger
    services -->|"uses session"| db
    services -->|"reads/writes ORM"| models
    db -->|"executes SQL"| PostgreSQL
    PostgreSQL -->|"returns rows"| db
    db -->|"returns result"| services
    services -->|"returns domain objects"| main
    main -->|"respond JSON"| Client

    %% Click Events
    click main "https://github.com/rashmeet0099/fast_api_postgres/blob/main/main.py"
    click schemas "https://github.com/rashmeet0099/fast_api_postgres/blob/main/schemas.py"
    click services "https://github.com/rashmeet0099/fast_api_postgres/blob/main/services.py"
    click models "https://github.com/rashmeet0099/fast_api_postgres/blob/main/models.py"
    click db "https://github.com/rashmeet0099/fast_api_postgres/blob/main/db.py"
    click swagger "https://github.com/rashmeet0099/fast_api_postgres/blob/main/main.py"
    click Client "https://github.com/rashmeet0099/fast_api_postgres/blob/main/requirements.txt"

    %% Styles
    classDef api fill:#D6EAF8,stroke:#1B4F72,color:#1B4F72;
    classDef service fill:#D5F5E3,stroke:#145A32,color:#145A32;
    classDef data fill:#FCF3CF,stroke:#7D6608,color:#7D6608;
    classDef validation fill:#FADBD8,stroke:#6E2C00,color:#6E2C00;
    classDef external fill:#ebebeb,stroke:#333333,color:#333333;
```
