|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +A production-ready multi-tenant SaaS backend built with FastAPI, SQLAlchemy 2.0 async, and PostgreSQL. Uses modern Python 3.12+ with strict typing. |
| 8 | + |
| 9 | +## Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +just dev # Start dev server with hot-reload (port 8000) |
| 13 | +just test # Run all tests |
| 14 | +just test tests/path # Run specific test file |
| 15 | +just test-unit # Run unit tests only |
| 16 | +just test-integration # Run integration tests only |
| 17 | +just test-cov # Run with coverage report |
| 18 | +just lint # Lint (ruff) + type check (mypy) |
| 19 | +just fix # Auto-fix linting issues |
| 20 | +just typecheck # Type check only |
| 21 | +just services # Start PostgreSQL + Redis via Docker |
| 22 | +just services-down # Stop services |
| 23 | +just migrate # Run database migrations |
| 24 | +just migration "name" # Create new migration |
| 25 | +just seed # Generate demo data |
| 26 | +``` |
| 27 | + |
| 28 | +## Architecture |
| 29 | + |
| 30 | +**Modular Monolith** with strict layering: |
| 31 | + |
| 32 | +``` |
| 33 | +Routes → Services → Repositories → Models |
| 34 | +``` |
| 35 | + |
| 36 | +- **Routes** (`routes.py`): HTTP handling, uses `Annotated[X, Depends()]` for DI |
| 37 | +- **Services** (`services.py`): Business logic, orchestration |
| 38 | +- **Repositories** (`repos.py`): Data access, query building |
| 39 | +- **Models** (`models.py`): SQLAlchemy table definitions |
| 40 | +- **Schemas** (`schemas.py`): Pydantic request/response models |
| 41 | + |
| 42 | +**Import rules** (enforced by Tach): |
| 43 | +- Routes cannot import Repositories |
| 44 | +- Services cannot import Routes |
| 45 | +- Models import nothing |
| 46 | + |
| 47 | +## Multi-Tenancy |
| 48 | + |
| 49 | +Row-level isolation via `tenant_id` column. All tenant-scoped models **must** use `TenantMixin`: |
| 50 | + |
| 51 | +```python |
| 52 | +from app.core.database import Base, TenantMixin, TimestampMixin, UUIDMixin |
| 53 | + |
| 54 | +class Item(Base, UUIDMixin, TimestampMixin, TenantMixin): |
| 55 | + __tablename__ = "items" |
| 56 | + name: Mapped[str] = mapped_column(String(255)) |
| 57 | +``` |
| 58 | + |
| 59 | +Never query without tenant context unless explicitly bypassing for admin operations. |
| 60 | + |
| 61 | +## Module Structure |
| 62 | + |
| 63 | +New modules go in `src/app/modules/{name}/`: |
| 64 | + |
| 65 | +``` |
| 66 | +src/app/modules/{name}/ |
| 67 | +├── __init__.py # Router + module registration |
| 68 | +├── routes.py # HTTP endpoints |
| 69 | +├── schemas.py # Pydantic models |
| 70 | +├── services.py # Business logic |
| 71 | +├── repos.py # Data access |
| 72 | +└── models.py # SQLAlchemy models |
| 73 | +``` |
| 74 | + |
| 75 | +## API Conventions |
| 76 | + |
| 77 | +All endpoints must have `response_model`, `summary`, and `tags`: |
| 78 | + |
| 79 | +```python |
| 80 | +@router.post( |
| 81 | + "/items", |
| 82 | + response_model=ItemResponse, |
| 83 | + status_code=status.HTTP_201_CREATED, |
| 84 | + summary="Create a new item", |
| 85 | + tags=["items"], |
| 86 | +) |
| 87 | +``` |
| 88 | + |
| 89 | +Use `Annotated[X, Depends()]` for all dependencies. |
| 90 | + |
| 91 | +## Testing |
| 92 | + |
| 93 | +- Unit tests: `tests/unit/` - mock dependencies, no I/O |
| 94 | +- Integration tests: `tests/integration/` - real database |
| 95 | +- Factories: `tests/factories/` - use polyfactory for test data |
| 96 | +- Mark async tests with `@pytest.mark.asyncio` (auto mode enabled) |
| 97 | + |
| 98 | +## Key Dependencies |
| 99 | + |
| 100 | +- **uv**: Package manager (use `uv run` prefix) |
| 101 | +- **Ruff**: Linting and formatting |
| 102 | +- **Mypy**: Type checking |
| 103 | +- **Alembic**: Database migrations |
| 104 | +- **ARQ**: Background jobs (Redis-based) |
| 105 | +- **structlog**: Structured logging |
| 106 | + |
| 107 | +## Error Handling |
| 108 | + |
| 109 | +All errors return RFC 7807 Problem Details format with `trace_id`. |
0 commit comments