| name | fb-backend |
| description | Use when writing, editing, or debugging backend Python code in the Family Budget project โ adding API endpoints, creating or modifying SQLModel models, writing Alembic migrations, implementing service logic, fixing auth bugs, working with WebSocket/Redis pub-sub, or writing backend tests. Trigger on: "add endpoint", "new model", "create migration", "fix API", "add service", "backend bug", "authentication issue", "database query", "WebSocket event", "Redis cache", "write test", "pytest", "SQLModel", "FastAPI route", "Pydantic schema", "alembic", "background job", "scheduler".
|
| version | 1.0.0 |
| author | Family Budget Team |
| tags | ["backend","fastapi","sqlmodel","postgresql"] |
| user-invocable | true |
Family Budget โ Backend Development
Stack
| Layer | Technology |
|---|
| Framework | FastAPI 0.121.2 |
| ORM | SQLModel 0.0.22 (SQLAlchemy 2.0 + Pydantic v2) |
| Database | PostgreSQL 16 via asyncpg 0.29.0 |
| Migrations | Alembic 1.14.0 |
| Caching | Redis โฅ5.0.0 |
| Auth | JWT (python-jose) + Argon2 + TOTP + WebAuthn |
| Real-time | WebSocket + Redis Pub/Sub |
| Scheduler | APScheduler 3.11.0 |
| JSON | orjson (3-10ร faster, used as default response class) |
Project Structure
backend/app/
โโโ main.py โ FastAPI app, lifespan, middleware setup
โโโ scheduler.py โ APScheduler cron jobs
โโโ api/
โ โโโ v1/
โ โ โโโ router.py โ aggregates all v1 endpoint routers
โ โ โโโ endpoints/ โ 32+ endpoint modules (CRUD + WS + admin)
โ โโโ web/
โ โโโ router.py โ Jinja2 page routes (HTMLResponse)
โโโ models/ โ SQLModel ORM models (38 files)
โโโ schemas/ โ Pydantic request/response schemas
โโโ services/ โ business logic (45+ services)
โโโ db/
โ โโโ migrations/ โ Alembic (env.py + versions/)
โโโ core/
โ โโโ config.py โ Settings (pydantic-settings)
โ โโโ dependencies.py โ CurrentUser, CurrentAdmin, get_session
โ โโโ exceptions.py โ APIException hierarchy
โโโ middleware/ โ JWT, CORS, logging, CSP, rate-limit
Key Rules (read these first)
- Session is auto-transactional โ
get_session commits on success, rolls back on error. Never call session.commit() inside an endpoint.
- Never call
session.commit() in service functions โ services receive a session and operate within the endpoint's transaction.
- User isolation via
apply_user_filter() โ always filter user-owned data by current_user.id; use core/user_isolation.py.
- Exception hierarchy โ raise
NotFoundException, ConflictException, etc. from core/exceptions.py, never raw HTTPException in business logic.
- Schema separation โ separate
Create, Update, Response Pydantic classes per domain; model_config = ConfigDict(from_attributes=True) on all Response schemas.
- Migrations are timestamp-named โ
YYYYMMDD_<hex>_<slug>.py; never auto-generate migrations in production, always review generated SQL.
- orjson everywhere โ use
ORJSONResponse for custom responses; default app response class is already orjson.
- Cache invalidation โ after every write that affects cached data, call the relevant
cache_service.invalidate_*().
- WebSocket broadcast โ after every write that clients may display in real-time, call the relevant
broadcast_*() from budget_ws.py.
- Rate limiting โ use
@limiter.limit("X/minute") on auth endpoints and any endpoint that can be abused.
Reference Files
Load only what you need:
| Task | Read |
|---|
| Adding a new REST endpoint | references/new-feature.md |
| Creating/modifying a SQLModel model | references/patterns.md#models |
| Writing Pydantic schemas | references/patterns.md#schemas |
| Writing service logic | references/patterns.md#services |
| Creating an Alembic migration | references/patterns.md#migrations |
| Authentication / authorization | references/patterns.md#auth |
| WebSocket events / Redis Pub/Sub | references/patterns.md#websocket |
| Writing backend tests | references/testing.md |
| Background jobs / scheduler | references/patterns.md#scheduler |