How rashmeet0099/fast_api_postgres works
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