# igorbenav/sqlmodel-boilerplate architecture

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

- Interactive diagram: https://gitdiagram.com/igorbenav/sqlmodel-boilerplate
- Repository: https://github.com/igorbenav/sqlmodel-boilerplate

## Overview

##### Main components
- API Web Server – The core FastAPI application is defined under the “src/app” directory. It includes:
- The main entry point (src/app/main.py) that creates and configures the FastAPI app.
- API routes grouped by version (src/app/api/v1) such as login, logout, posts, users, rate limits, tasks, and tiers.
- Middleware and security (JWT authentication, refresh token handling, client-side cache middleware).
- Business Logic & Data Access – The project follows a clear separation of concerns:
- CRUD operations (in src/app/crud) that encapsulate database operations.
- Database models and SQLModel definitions (in src/app/models and src/app/core/db) that define schema and ORM behaviors.
- Pydantic validation models and exception handling (in src/app/core/schemas and src/app/core/exceptions).
- Database Layer – PostgreSQL is the relational database in use:
- Connection details and migrations (using Alembic in src/migrations) are specified.
- Caching & Rate Limiting – Redis is used in dual roles:
- Caching endpoints and client-side caching (modules in src/app/core/utils/cache and related settings in the .env).
- Rate limiter dependencies provided in the API endpoints (with corresponding models and utilities).
- Background Processing – ARQ is configured for running asynchronous background tasks:
- Worker functions and settings reside in src/app/core/worker.
- Deployment & Containerization – Docker is central:
- A Dockerfile, docker-compose.yml, and NGINX configuration (default.conf) illustrate containerization and reverse-proxy options.
- Testing – There is a separate tests directory for unit and integration tests.

##### Relationships and Interactions
- The FastAPI server makes database calls through the CRUD modules using SQLModel and async sessions (depicted between the API and PostgreSQL).
- When needed (such as caching responses or implementing rate limits), the API server interacts with Redis.
- When handling tasks (e.g., longer processes that run asynchronously), the API enqueues jobs via ARQ; the background worker (illustrated as a separate worker component) picks up these tasks.
- Optionally, include external routing by an NGINX reverse proxy as a façade in front of the API layer.

##### Architectural Patterns and Key Technologies
- That the project is fully asynchronous and relies on modern Python frameworks and libraries (FastAPI, SQLModel, Redis, ARQ).
- Client → FastAPI API server
- API server → PostgreSQL (via async DB sessions)
- API server → Redis (for caching, rate limiting)
- API server → ARQ worker (for enqueueing background tasks)
- ARQ worker → (optional) Database or external services if background tasks require further processing
- That the application runs inside Docker containers managed by Docker Compose.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Deployment["Deployment & Containerization"]:::deployment
    Client["Client"]:::external
    NGINX["NGINX (Reverse Proxy)"]:::external
    FastAPIApp["FastAPI Application"]:::app
    Database["PostgreSQL Database"]:::db
    Cache_Rate["Redis (Caching & Rate Limiting)"]:::cache
    ARQ_Worker["ARQ Worker (Background Processing)"]:::worker
    Testing["Testing"]:::external

    subgraph "FastAPI Application Modules"
        API_V1["API Routes (v1)"]:::app
        MiddlewareSec["Middleware & Security"]:::app
        BizLogic["Business Logic & Data Access"]:::app
    end

    Client -->|"HTTP"| NGINX
    NGINX -->|"proxies"| FastAPIApp
    FastAPIApp -->|"handles"| API_V1
    API_V1 -->|"applies"| MiddlewareSec
    MiddlewareSec -->|"invokes"| BizLogic

    FastAPIApp ---|"DB_calls"| Database
    FastAPIApp ---|"cache/rate_limit"| Cache_Rate
    FastAPIApp ---|"enqueues_job"| ARQ_Worker
    ARQ_Worker -->|"processes_and_accesses"| Database

    Deployment -->|"orchestrates"| NGINX
    Deployment -->|"orchestrates"| FastAPIApp
    Deployment -->|"orchestrates"| Database
    Deployment -->|"orchestrates"| Cache_Rate
    Deployment -->|"orchestrates"| ARQ_Worker

    Testing -->|"tests"| FastAPIApp

    classDef client fill:#f9c74f,stroke:#333,stroke-width:2px
    classDef app fill:#90be6d,stroke:#333,stroke-width:2px
    classDef db fill:#f94144,stroke:#333,stroke-width:2px
    classDef cache fill:#577590,stroke:#333,stroke-width:2px
    classDef worker fill:#f3722c,stroke:#333,stroke-width:2px
    classDef deployment fill:#277da1,stroke:#333,stroke-width:2px
    classDef external fill:#43aa8b,stroke:#333,stroke-width:2px

    click FastAPIApp "https://github.com/igorbenav/sqlmodel-boilerplate/blob/main/src/app/main.py"
    click API_V1 "https://github.com/igorbenav/sqlmodel-boilerplate/tree/main/src/app/api/v1"
    click MiddlewareSec "https://github.com/igorbenav/sqlmodel-boilerplate/tree/main/src/app/middleware"
    click BizLogic "https://github.com/igorbenav/sqlmodel-boilerplate/tree/main/src/app/crud"
    click Database "https://github.com/igorbenav/sqlmodel-boilerplate/blob/main/src/app/core/db/database.py"
    click Cache_Rate "https://github.com/igorbenav/sqlmodel-boilerplate/blob/main/src/app/core/utils/cache.py"
    click ARQ_Worker "https://github.com/igorbenav/sqlmodel-boilerplate/tree/main/src/app/core/worker"
    click Deployment "https://github.com/igorbenav/sqlmodel-boilerplate/tree/main/Dockerfile"
    click Testing "https://github.com/igorbenav/sqlmodel-boilerplate/tree/main/tests"
```
