This project demonstrates a clean architecture approach by swapping an ephemeral, in-memory data store for a persistent PostgreSQL database running in a Docker container.
The core achievement of this setup is that the application layer, routes, and business logic remain completely unchanged during the storage swap—proving the effectiveness of the repository pattern.
- Backend Framework: Python 3.11 with FastAPI & Uvicorn
- Database: PostgreSQL 15 (Alpine Linux distribution)
- Database Driver:
psycopg2-binary - Orchestration: Docker & Docker Compose
.env
By leveraging a strict repository interface pattern (UserRepository), our application interacts with data via a predefined contract (save and get_all).
When transitioning from the old InMemoryUserRepository to the new PostgresUserRepository, zero lines of code were altered inside our HTTP routes or domain logic. The database transition was completed entirely by switching one initialization line in main.py:
# ❌ OLD STORE:
# repo = InMemoryUserRepository()
# NEW STORE:
DATABASE_URL = os.getenv("DATABASE_URL")
repo = PostgresUserRepository(DATABASE_URL)