{"id":43851897,"url":"https://github.com/cgund98/fastapi-postgres-template","last_synced_at":"2026-02-06T07:18:36.340Z","repository":{"id":333185663,"uuid":"1128153242","full_name":"cgund98/fastapi-postgres-template","owner":"cgund98","description":"Opinionated Python backend template using FastAPI, Postgres, domain-centric architecture, event-driven design, and modern tooling.","archived":false,"fork":false,"pushed_at":"2026-01-17T20:07:36.000Z","size":178,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-01-18T06:52:08.244Z","etag":null,"topics":["async-python","backend","domain-driven-design","events","fastapi","postgresql","python","rest-api","template"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cgund98.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-01-05T08:10:14.000Z","updated_at":"2026-01-17T20:07:37.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/cgund98/fastapi-postgres-template","commit_stats":null,"previous_names":["cgund98/fastapi-postgres-template"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/cgund98/fastapi-postgres-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgund98%2Ffastapi-postgres-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgund98%2Ffastapi-postgres-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgund98%2Ffastapi-postgres-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgund98%2Ffastapi-postgres-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cgund98","download_url":"https://codeload.github.com/cgund98/fastapi-postgres-template/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgund98%2Ffastapi-postgres-template/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29154090,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-06T07:15:46.158Z","status":"ssl_error","status_checked_at":"2026-02-06T07:15:26.443Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async-python","backend","domain-driven-design","events","fastapi","postgresql","python","rest-api","template"],"created_at":"2026-02-06T07:18:35.577Z","updated_at":"2026-02-06T07:18:36.332Z","avatar_url":"https://github.com/cgund98.png","language":"Python","readme":"# 🚀 FastAPI PostgreSQL Template\n\n\u003cdiv align=\"center\"\u003e\n\n**A production-ready FastAPI backend template demonstrating efficient organization and modern design patterns**\n\n[![Python](https://img.shields.io/badge/Python-3.12+-blue.svg)](https://www.python.org/)\n[![FastAPI](https://img.shields.io/badge/FastAPI-0.115+-green.svg)](https://fastapi.tiangolo.com/)\n[![PostgreSQL](https://img.shields.io/badge/PostgreSQL-15+-blue.svg)](https://www.postgresql.org/)\n[![Poetry](https://img.shields.io/badge/Poetry-Latest-orange.svg)](https://python-poetry.org/)\n[![License](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)\n\n*Built with ❤️ using Domain-Driven Design and Event-Driven Architecture*\n\n[Features](#-features) • [Quick Start](#-getting-started) • [Architecture](#-architecture) • [Example](#-example-business-case) • [Development Guide](DEVELOPMENT.md) • [Architecture Details](ARCHITECTURE.md)\n\n\u003c/div\u003e\n\n## 👥 Who Is This For?\n\nThis template is designed for **backend engineers** building:\n\n- RESTful APIs with clean separation between HTTP handlers, business logic, and data access\n- Event-driven microservices with async message processing (SQS/SNS)\n- Type-safe codebases with comprehensive type hints and fast unit tests\n- Production-ready systems with proper transaction management and error handling\n\n## ✨ Features\n\n### 🏗️ Architecture \u0026 Design Patterns\n\n- **3-Tier Architecture**: Clear separation between presentation, domain, and infrastructure layers\n- **Domain-Driven Design**: Business logic encapsulated in domain services with rich domain models\n- **Repository Pattern**: Data access abstraction enabling easy testing and database swapping\n- **Unit of Work**: Application-level transaction management for consistent data operations\n- **Event-Driven Architecture**: Decoupled communication via domain events and message queues\n- **Dependency Injection**: Loose coupling through constructor injection, enabling testability\n\n### 🛠️ Technology Choices\n\n- **FastAPI**: Modern, high-performance web framework with automatic API documentation\n- **PostgreSQL**: SQLModel ORM for type-safe database operations with domain/ORM separation\n- **Async/Await**: Fully asynchronous Python for optimal I/O-bound performance\n- **Type Safety**: Comprehensive type hints with mypy for compile-time error detection\n\n### 📊 Observability \u0026 Operations\n\n- **Structured Logging**: JSON-formatted logs with structlog for easy parsing and analysis\n- **Health Checks**: Built-in health check endpoints that test database connectivity\n\n### 👨‍💻 Developer Experience\n\n- **Modern Tooling**: Ruff for fast linting/formatting, mypy for type checking\n- **Comprehensive Testing**: Unit tests with mocked dependencies for fast, reliable test suites\n- **Docker Support**: Containerized API and worker services for consistent deployments\n- **Infrastructure as Code**: AWS CDK for reproducible cloud infrastructure\n- **CI/CD Ready**: GitHub Actions pipeline configuration included\n\n## 🏛️ Architecture\n\nThis template follows a **3-tier architecture** with clear separation of concerns:\n\n### Presentation Layer (`app/presentation/`)\n- **API Routes**: FastAPI route handlers\n- **Schemas**: Pydantic models for request/response validation\n\n### Domain Layer (`app/domain/`)\n- **Models**: Domain entities with business logic\n- **Services**: Domain-specific business logic with integrated transaction management\n- **Repositories**: Data access interfaces using SQLModel ORM\n  - ORM models defined in `repo/sql.py` files alongside repository implementations\n  - Repositories convert between ORM models and domain models\n- **Commands**: Command objects for operations (`commands.py`)\n- **Events**: Domain events for event-driven communication\n- **Consumers**: Event handlers for processing domain events\n\n### Infrastructure Layer (`app/infrastructure/`)\n- **Database**: SQLAlchemy async connection pooling and transaction management\n- **Messaging**: Event publishing (SNS) and consumption (SQS)\n- **Tasks**: Async task execution helpers\n\n### Observability (`app/observability/`)\n- **Logging**: Structured logging with structlog\n\n## 📁 Project Structure\n\n```\nfastapi-postgres-template/\n├── entry/\n│   ├── api/\n│   │   └── main.py                 # FastAPI server entrypoint\n│   └── worker/\n│       └── main.py                 # Async event consumer entrypoint\n│\n├── app/\n│   ├── config/\n│   │   └── settings.py             # Environment/config variables\n│   │\n│   ├── domain/\n│   │   ├── user/                   # User domain\n│   │   │   ├── model.py            # Domain models (pure Pydantic)\n│   │   │   ├── commands.py         # Command objects\n│   │   │   ├── repo/               # Repository implementations\n│   │   │   │   ├── base.py         # Repository interface\n│   │   │   │   └── sql.py         # SQLModel ORM + ORM models\n│   │   │   ├── events/\n│   │   │   ├── service.py\n│   │   │   └── consumers/\n│   │   │\n│   │   └── billing/invoice/        # Invoice domain\n│   │\n│   ├── presentation/\n│   │   ├── billing/                # Billing routes and schemas\n│   │   └── user/                   # User routes and schemas\n│   │\n│   ├── infrastructure/\n│   │   ├── db/                     # Transaction manager interface\n│   │   ├── sql/                    # SQLModel connection pool and transaction manager\n│   │   ├── messaging/              # Event publishing/consumption\n│   │   └── tasks/                  # Task execution\n│   │\n│   └── observability/              # Logging\n│\n├── tests/\n│   └── unit/                       # Unit tests with mocked dependencies\n│       └── domain/                 \n│\n├── resources/\n│   ├── db/migrations/              # Database migrations\n│   ├── docker/                     # Dockerfiles (shared image for API/worker)\n│   ├── scripts/                    # Utility scripts\n│   └── infra/cdk/                  # AWS CDK infrastructure\n│\n├── Makefile\n├── pyproject.toml\n└── README.md\n```\n\n## 🚀 Quick Start\n\n### Prerequisites\n\n- **Docker** and **Docker Compose** (required)\n- **AWS CLI** (optional, for infrastructure deployment)\n\n\u003e 💡 **Note**: This project uses a **workspace container** with all development tools pre-installed. See [DEVELOPMENT.md](DEVELOPMENT.md) for detailed setup instructions.\n\n### Running the Example\n\n1. **Clone and start services:**\n```bash\ngit clone \u003crepository-url\u003e\ncd fastapi-postgres-template\nmake workspace-build\nmake workspace-up\nmake poetry-install\n```\n\n2. **Set up database and LocalStack:**\n```bash\ndocker compose up -d postgres localstack\nmake localstack-setup\nmake migrate\n```\n\n3. **Run the API server:**\n```bash\nmake run-api\n\n# In a seperate terminal, start the consumer\nmake run-worker\n```\n\n4. **Visit the API documentation:**\n   - Swagger UI: http://localhost:8000/docs\n   - ReDoc: http://localhost:8000/redoc\n\n5. **Try the example endpoints:**\n   - Create a user: `POST /users` with `{\"email\": \"user@example.com\", \"name\": \"John Doe\", \"age\": 30}`\n   - Create an invoice: `POST /invoices` with `{\"user_id\": \"\u003cuser_id\u003e\", \"amount\": 100.00}`\n   - Request payment: `POST /invoices/{invoice_id}/request-payment`\n\n\u003e 📖 For detailed development instructions, Makefile commands, and workflows, see [DEVELOPMENT.md](DEVELOPMENT.md).\n\n### 🔧 SQLModel ORM Pattern\n\nRepositories use **SQLModel ORM** for type-safe database operations. This provides:\n\n- **Type Safety**: ORM models with full type hints and IDE support\n- **Domain Separation**: Pure Pydantic domain models separate from ORM concerns\n- **Simplicity**: Minimal boilerplate with automatic mapping\n- **Query Building**: Use SQLModel's query API for type-safe queries\n\n**Example ORM model and repository:**\n```python\n# In app/domain/user/repo/sql.py\nclass UserORM(SQLModel, table=True):\n    \"\"\"User ORM model for database persistence.\"\"\"\n    __tablename__ = \"users\"\n    id: UUID = Field(primary_key=True)\n    email: str = Field(unique=True, index=True)\n    name: str\n    age: int | None = None\n    created_at: datetime\n    updated_at: datetime\n\nclass UserRepository(BaseUserRepository[SQLContext]):\n    @staticmethod\n    def _orm_to_domain(orm_user: UserORM) -\u003e User:\n        \"\"\"Convert ORM model to domain model.\"\"\"\n        return User(\n            id=orm_user.id,\n            email=orm_user.email,\n            name=orm_user.name,\n            age=orm_user.age,\n            created_at=orm_user.created_at,\n            updated_at=orm_user.updated_at,\n        )\n    \n    async def get_by_id(self, context: SQLContext, user_id: UUID) -\u003e User | None:\n        orm_user = await context.session.get(UserORM, user_id)\n        return self._orm_to_domain(orm_user) if orm_user else None\n```\n\n## 💼 Example Business Case\n\nThis template implements a **User Management and Billing System** to demonstrate real-world patterns:\n\n\u003e 💡 **Note**: This example showcases how multiple domains interact through events and service orchestration.\n\n### 👤 User Domain\n- **User Registration**: Create users with email uniqueness validation\n- **User Updates**: Update user information (e.g., name changes)\n- **User Deletion**: Cascade deletion of associated invoices when a user is removed\n- **Event Publishing**: Emits `UserCreatedEvent` and `UserUpdatedEvent` for downstream processing\n\n### 💰 Invoice/Billing Domain\n- **Invoice Creation**: Create invoices linked to users with amount tracking\n- **Payment Processing**: Mark invoices as paid with business rule validation (prevents double-payment)\n- **Payment Requests**: Request payment for invoices, triggering async processing\n- **Event-Driven Workflow**: \n  - When an invoice is created, a `InvoiceCreatedEvent` is published\n  - A `InvoicePaymentRequestedEvent` triggers a worker to automatically process payment\n  - The worker publishes `InvoicePaidEvent` upon completion\n\n### 🔑 Key Patterns Demonstrated\n- **Cross-Domain Relationships**: UserService orchestrates InvoiceService for cascading operations\n- **Event-Driven Processing**: Async workers consume events from SQS queues\n- **Transaction Management**: All operations use application-level transactions\n- **Business Rule Enforcement**: Prevents invalid states (e.g., paying an already-paid invoice)\n- **Domain Events**: Decoupled communication between services via events\n\nThis example showcases how to structure a multi-domain system with event-driven workflows, making it easy to extend with additional domains and business logic.\n\n## 🗄️ Database Schema\n\nThe template includes example domains (User and Invoice). Database migrations are managed using [golang-migrate](https://github.com/golang-migrate/migrate) and located in `resources/db/migrations/`.\n\nThe schema includes:\n\n- **Users table**: Stores user information with email uniqueness\n- **Invoices table**: Stores invoices linked to users with status tracking\n\n\u003e 📖 For migration commands and database setup, see [DEVELOPMENT.md](DEVELOPMENT.md#-database-migrations).\n\n## 📨 Event System\n\nThe template includes a **generic event system** for decoupled communication:\n\n| Component | Purpose |\n|-----------|---------|\n| **BaseEvent** | Base class for all domain events with metadata |\n| **Event Publishing** | Events published to SNS topics |\n| **Event Consumption** | Workers consume events from SQS queues |\n| **Event Handlers** | Domain-specific consumers process events |\n\n1. **BaseEvent**: All domain events extend this base class with metadata\n2. **Event Publishing**: Events are published to SNS topics\n3. **Event Consumption**: Workers consume events from SQS queues\n4. **Event Handlers**: Domain-specific consumers process events\n\n**Example: Publishing a domain event**\n\n```python\n# In UserService.create_user()\nevent = UserCreatedEvent(\n    aggregate_id=str(user.id),\n    email=user.email,\n    name=user.name\n)\nawait event_publisher.publish(event)\n```\n\n**Example: Consuming events in a worker**\n\n```python\n# Worker automatically processes events from SQS\n# Event handlers execute domain logic asynchronously\nawait invoice_payment_requested_handler.handle(event)\n```\n\n## 🐳 Docker\n\nThe project uses Docker for both development and production:\n\n- **Workspace Container**: Consistent development environment with all tools pre-installed\n- **Production Images**: Optimized images for API, worker, and migrations\n\n\u003e 📖 For Docker setup and usage, see [DEVELOPMENT.md](DEVELOPMENT.md#-docker).\n\n## ☁️ Infrastructure\n\nDeploy infrastructure using **AWS CDK**:\n\n```bash\ncd resources/infra/cdk\nnpm install -g aws-cdk\ncdk bootstrap\ncdk deploy\n```\n\n## 🎯 Design Patterns\n\nThis template demonstrates the following patterns:\n\n- **3-Tier Architecture**: Clear separation of concerns\n- **Repository Pattern**: Data access abstraction\n- **Unit of Work**: Transaction management\n- **Domain Events**: Event-driven communication\n- **Dependency Injection**: Loose coupling\n- **Factory Pattern**: Service creation\n\n## 🤝 Contributing\n\nContributions are welcome! Please follow these guidelines:\n\n1. ✅ Follow the existing code structure and patterns\n2. ✅ Maintain type hints throughout (mypy must pass)\n3. ✅ Write tests for new features with mocked dependencies\n4. ✅ Run linting and type checking before committing (`make lint`)\n5. ✅ Use the workspace container for all development tasks\n6. ✅ Follow the Makefile commands for common tasks\n7. ✅ Update documentation for any architectural changes\n\n\u003e 📖 For detailed development workflow and guidelines, see [DEVELOPMENT.md](DEVELOPMENT.md#-contributing).\n\n## 📝 License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n---\n\n\u003cdiv align=\"center\"\u003e\n\n**Made with ❤️ for the Python backend community**\n\n⭐ Star this repo if you find it useful!\n\n\u003c/div\u003e\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcgund98%2Ffastapi-postgres-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcgund98%2Ffastapi-postgres-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcgund98%2Ffastapi-postgres-template/lists"}