← ClaudeAtlas

fastapi-prolisted

FastAPI + SQLAlchemy 2.0 async patterns + review checklist: routers, Depends DI, Pydantic schemas, session/transaction discipline, Alembic safety, auth/CORS/errors. Use when writing or reviewing FastAPI routes, async repositories, or migrations. Not for non-web Python. Output: layered, transaction-safe API.
hmj1026/dhpk · ★ 2 · API & Backend · score 68
Install: claude install-skill hmj1026/dhpk
# FastAPI + SQLAlchemy (async) Pro Web-API layer guidance. Builds on the `python` module (typing, async discipline, ruff/pyright). Layering: **Router → Service → Repository**; routers stay thin (validate, delegate, shape response), business logic lives in services, data-access in repositories. ## Routers & dependency injection - Inject collaborators via `Depends(...)` — DB session, current user, settings. Don't instantiate sessions/clients inside the handler. - Declare an explicit `response_model=` and `status_code=` on every route. Never return raw ORM objects — return a Pydantic response schema (prevents lazy-load serialization surprises and leaking columns). - Keep one session per request, yielded by a dependency; commit/rollback at the edge of the unit of work, not scattered through services. - Version/prefix routers (`APIRouter(prefix=...)`); group by domain. Paginate list endpoints with a consistent envelope (ccas standardized `/pipeline/runs` pagination) and bound max page size. ## Pydantic schemas - Separate **request** (`...Create` / `...Update`) and **response** (`...Out`) models from ORM models. Use `model_config = ConfigDict(from_attributes=True)` for ORM→schema conversion. - Validate at the boundary: constrained types, field validators. Reject bad input with 422 rather than letting it reach the DB. - Don't put secrets/internal fields in response schemas. ## SQLAlchemy 2.0 async - `async with AsyncSession(...) as session:` — every query is