# kpeoh/finance-tracker architecture

> Architecture diagram of the GitHub repository kpeoh/finance-tracker, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kpeoh/finance-tracker
- Repository: https://github.com/kpeoh/finance-tracker

## Overview

#### Purpose and Entry Points
Finance Tracker is a work-in-progress multi-user personal-finance API. It manages user-owned categories and transactions, secured with JWT bearer authentication. The executable ASGI entry point is `backend/app/main.py`; local operation runs PostgreSQL through `docker-compose.yml` and starts Uvicorn against that app. `frontend/` is only a placeholder.

#### HTTP API Composition
FastAPI route modules expose authentication, current-user identity, and protected category/transaction CRUD:
- `backend/app/api/routes/auth.py`
- `backend/app/api/routes/users.py`
- `backend/app/api/routes/categories.py`
- `backend/app/api/routes/transactions.py`

`backend/app/main.py` assembles these routers and provides the health endpoint. Route handlers form the API boundary: they validate request/response contracts, obtain dependencies, and delegate domain persistence rules rather than directly defining the data model.

#### Authentication and Request Identity
OAuth2 password-flow login issues JWTs; protected routes resolve the bearer token into the current user and enforce ownership:
- `backend/app/core/security.py`
- `backend/app/api/deps.py`
- `backend/app/schemas/auth.py`

There is intentionally no public registration endpoint. Users are provisioned directly in PostgreSQL, with roles including `admin`, `user`, and constrained `test`.

#### Domain Model and Contracts
SQLAlchemy models define the central persisted graph: users own categories and transactions, while each transaction references a category. Pydantic v2 schemas define API-facing create, update, and nested read shapes:
- `backend/app/models/user.py`
- `backend/app/models/category.py`
- `backend/app/models/transaction.py`
- `backend/app/schemas/category.py`
- `backend/app/schemas/transaction.py`

Transactions return related user/category data through nested read models. The schema layer isolates HTTP payload representation from ORM entities.

#### Service-Layer Rules
Services implement business and persistence operations behind routes:
- `backend/app/services/user.py`
- `backend/app/services/category.py`
- `backend/app/services/transaction.py`

The category and transaction services apply ownership checks; transaction operations additionally validate that the referenced category belongs to the requesting user. Test-role users are capped at five categories and fifteen transactions, making role-based limits a domain rule rather than a client concern.

#### Database and Lifecycle
The backend uses asynchronous SQLAlchemy 2 with `asyncpg` against PostgreSQL. Database engine/session setup is centralized in `backend/app/database.py`; environment-driven settings reside in `backend/app/core/config.py`. Alembic owns schema evolution:
- `backend/alembic/env.py`
- `backend/alembic/versions/31e92ce704bb_initial.py`

PostgreSQL is the current Docker Compose infrastructure service; the API itself is currently run outside Compose.

#### Operations and Evolution Boundaries
`backend/scripts/reset_test_user.py` is an operational data-management path: it finds the test-role user, clears their finance data, and seeds demo categories/transactions. It is intended for scheduled deployment use. Architecture choices and intended API/database layering are recorded in `docs/adr/0001-database-stack.md` and `docs/adr/0002-api-layer-structure.md`. Planned boundaries include stats endpoints, stronger login controls, refresh/logout flows, a containerized API, and a future frontend.

Main entry points: [`backend/app/database.py`](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/database.py) (Async database sessions), [`backend/app/services/transaction.py`](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/services/transaction.py) (Transaction service), [`backend/app/models/user.py`](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/models/user.py) (User model), [`backend/app/services/category.py`](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/services/category.py) (Category service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API Runtime"]
  node_app["FastAPI ASGI app<br/>entry point<br/>[main.py]"]
  node_routes["Protected API routers<br/>FastAPI routes"]
  node_contracts["API schemas<br/>Pydantic v2<br/>[transaction.py]"]
end

subgraph group_auth["Identity"]
  node_security{{"JWT security<br/>OAuth2/JWT<br/>[security.py]"}}
  node_identity["Current-user dependency<br/>FastAPI dependency<br/>[deps.py]"]
end

subgraph group_domain["Domain"]
  node_user_service["User service<br/>[user.py]"]
  node_category_service["Category service<br/>[category.py]"]
  node_transaction_service["Transaction service<br/>[transaction.py]"]
  node_user_model["User model<br/>SQLAlchemy model<br/>[user.py]"]
  node_category_model["Category model<br/>SQLAlchemy model<br/>[category.py]"]
  node_transaction_model["Transaction model<br/>SQLAlchemy model<br/>[transaction.py]"]
end

subgraph group_data["Data &amp; Evolution"]
  node_database["Async database sessions<br/>SQLAlchemy async<br/>[database.py]"]
  node_postgres[("PostgreSQL<br/>database<br/>[docker-compose.yml]")]
  node_migrations["Alembic migrations<br/>schema evolution<br/>[env.py]"]
  node_config["Environment settings<br/>configuration<br/>[config.py]"]
end

subgraph group_ops["Operations"]
  node_reset_script["Reset test user<br/>operational script<br/>[reset_test_user.py]"]
end

node_app -->|"mounts"| node_routes
node_routes -->|"validates payloads"| node_contracts
node_routes -->|"requires identity"| node_identity
node_routes -->|"login"| node_security
node_identity -->|"validates token"| node_security
node_routes -->|"delegates users"| node_user_service
node_routes -->|"delegates categories"| node_category_service
node_routes -->|"delegates transactions"| node_transaction_service
node_user_service -->|"persists"| node_user_model
node_category_service -->|"persists"| node_category_model
node_transaction_service -->|"persists"| node_transaction_model
node_category_service -->|"checks owner"| node_user_model
node_transaction_service -->|"checks owner"| node_user_model
node_transaction_service -->|"validates category owner"| node_category_model
node_user_model -->|"owns"| node_category_model
node_user_model -->|"owns"| node_transaction_model
node_transaction_model -->|"references"| node_category_model
node_user_service -->|"uses session"| node_database
node_category_service -->|"uses session"| node_database
node_transaction_service -->|"uses session"| node_database
node_database -->|"asyncpg"| node_postgres
node_config -->|"configures"| node_database
node_migrations -->|"evolves schema"| node_postgres
node_reset_script -->|"clears and seeds"| node_database

click node_app "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/main.py"
click node_contracts "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/schemas/transaction.py"
click node_security "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/core/security.py"
click node_identity "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/api/deps.py"
click node_user_service "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/services/user.py"
click node_category_service "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/services/category.py"
click node_transaction_service "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/services/transaction.py"
click node_user_model "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/models/user.py"
click node_category_model "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/models/category.py"
click node_transaction_model "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/models/transaction.py"
click node_database "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/database.py"
click node_postgres "https://github.com/kpeoh/finance-tracker/blob/main/docker-compose.yml"
click node_migrations "https://github.com/kpeoh/finance-tracker/blob/main/backend/alembic/env.py"
click node_config "https://github.com/kpeoh/finance-tracker/blob/main/backend/app/core/config.py"
click node_reset_script "https://github.com/kpeoh/finance-tracker/blob/main/backend/scripts/reset_test_user.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_routes,node_contracts toneBlue
class node_security,node_identity toneAmber
class node_user_service,node_category_service,node_transaction_service,node_user_model,node_category_model,node_transaction_model toneMint
class node_database,node_postgres,node_migrations,node_config toneRose
class node_reset_script toneIndigo
```

## Components

### API Runtime

- [FastAPI ASGI app](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/main.py): entry point · Assembles routers and serves health checks. · `backend/app/main.py`
- **Protected API routers**: FastAPI routes · Auth, identity, category, and transaction endpoints.
- [API schemas](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/schemas/transaction.py): Pydantic v2 · Separates HTTP create, update, and nested read payloads from ORM entities. · `backend/app/schemas/transaction.py`

### Identity

- [JWT security](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/core/security.py): OAuth2/JWT · Password login issues tokens and validates bearer credentials. · `backend/app/core/security.py`
- [Current-user dependency](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/api/deps.py): FastAPI dependency · Resolves authenticated users for protected requests. · `backend/app/api/deps.py`

### Domain

- [User service](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/services/user.py): service · `backend/app/services/user.py`
- [Category service](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/services/category.py): service · Enforces ownership and test-user category limits. · `backend/app/services/category.py`
- [Transaction service](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/services/transaction.py): service · Enforces ownership, category ownership, and test-user transaction limits. · `backend/app/services/transaction.py`
- [User model](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/models/user.py): SQLAlchemy model · Users own categories and transactions and carry roles. · `backend/app/models/user.py`
- [Category model](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/models/category.py): SQLAlchemy model · User-owned transaction classification. · `backend/app/models/category.py`
- [Transaction model](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/models/transaction.py): SQLAlchemy model · References its owning user and category. · `backend/app/models/transaction.py`

### Data & Evolution

- [Async database sessions](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/database.py): SQLAlchemy async · Central engine and session setup using asyncpg. · `backend/app/database.py`
- [PostgreSQL](https://github.com/kpeoh/finance-tracker/blob/main/docker-compose.yml): database · `docker-compose.yml`
- [Alembic migrations](https://github.com/kpeoh/finance-tracker/blob/main/backend/alembic/env.py): schema evolution · Owns PostgreSQL schema creation and upgrades. · `backend/alembic/env.py`
- [Environment settings](https://github.com/kpeoh/finance-tracker/blob/main/backend/app/core/config.py): configuration · `backend/app/core/config.py`

### Operations

- [Reset test user](https://github.com/kpeoh/finance-tracker/blob/main/backend/scripts/reset_test_user.py): operational script · Clears and seeds demo finance data for the constrained test role. · `backend/scripts/reset_test_user.py`

## Connections

- FastAPI ASGI app → Protected API routers: mounts
- Protected API routers → API schemas: validates payloads
- Protected API routers → Current-user dependency: requires identity
- Protected API routers → JWT security: login
- Current-user dependency → JWT security: validates token
- Protected API routers → User service: delegates users
- Protected API routers → Category service: delegates categories
- Protected API routers → Transaction service: delegates transactions
- User service → User model: persists
- Category service → Category model: persists
- Transaction service → Transaction model: persists
- Category service → User model: checks owner
- Transaction service → User model: checks owner
- Transaction service → Category model: validates category owner
- User model → Category model: owns
- User model → Transaction model: owns
- Transaction model → Category model: references
- User service → Async database sessions: uses session
- Category service → Async database sessions: uses session
- Transaction service → Async database sessions: uses session
- Async database sessions → PostgreSQL: asyncpg
- Environment settings → Async database sessions: configures
- Alembic migrations → PostgreSQL: evolves schema
- Reset test user → Async database sessions: clears and seeds
