Views
Documentation of aggregated database views
Overview
BudgetBuddyDE uses PostgreSQL views for data aggregation and analysis. These views provide pre-calculated, optimized queries for frequently used data evaluations.
All views are located in the backend schema and are defined with Drizzle ORM.
transaction_receiver_view
Provides a deduplicated list of all receivers/senders from transactions and recurring payments.
Purpose
This view is used for autocomplete functions to suggest receiver names to users when entering data.
Fields
| Field | Type | Description |
|---|---|---|
receiver | STRING | Trimmed receiver name (without leading/trailing spaces) |
owner_id | VARCHAR | Owner's ID |
SQL Logic
Specifics
- DISTINCT: Removes duplicates
- TRIM(): Removes spaces at the beginning and end
- UNION: Combines receivers from both tables
- User Filter: Data is filtered by
owner_id
Usage
transaction_history_view
Aggregates transaction data by month, year, and category with calculated values for income, expenses, and balance.
Purpose
This view is used for:
- Dashboard Overviews: Display monthly income/expenses
- Category Analysis: Compare expenses per category
- Trend Analysis: Track development over multiple months
- Reports: Generate financial reports
Fields
| Field | Type | Description | Calculation |
|---|---|---|---|
month | NUMBER | Month (1-12) | EXTRACT(MONTH FROM processed_at) |
year | NUMBER | Year (e.g., 2024) | EXTRACT(YEAR FROM processed_at) |
owner_id | VARCHAR | Owner's ID | From transaction.owner_id |
category_id | UUID | Category ID | From transaction.category_id |
income | NUMBER | Total income | Sum of all positive transfer_amount values |
expenses | NUMBER | Total expenses (positive) | Sum of all negative transfer_amount values (absolute) |
balance | NUMBER | Balance (income - expenses) | Sum of all transfer_amount values |
SQL Logic
Calculations
income (Income)
- Sums only positive amounts
- Negative amounts are treated as 0
expenses (Expenses)
- Sums only negative amounts
- Uses
ABS()to obtain positive values - Positive amounts are treated as 0
balance (Balance)
- Simple sum of all amounts
- Equals:
income - expenses
Usage
Example Data
transaction_history_summary_view
Aggregates transaction data by month and year with calculated values for income, expenses, and balance - without category grouping.
Purpose
This view is used for:
- Overall Financial Overview: Display total monthly income/expenses across all categories
- High-level Dashboards: Show aggregated financial data without category breakdown
- Trend Analysis: Track overall financial development over time
- Summary Reports: Generate simplified financial reports
Fields
| Field | Type | Description | Calculation |
|---|---|---|---|
month | NUMBER | Month (1-12) | EXTRACT(MONTH FROM processed_at) |
year | NUMBER | Year (e.g., 2024) | EXTRACT(YEAR FROM processed_at) |
owner_id | VARCHAR | Owner's ID | From transaction.owner_id |
income | NUMBER | Total income | Sum of all positive transfer_amount values |
expenses | NUMBER | Total expenses (positive) | Sum of all negative transfer_amount values (absolute) |
balance | NUMBER | Balance (income - expenses) | Sum of all transfer_amount values |
SQL Logic
Difference from transaction_history_view
Unlike transaction_history_view, this view:
- Does NOT group by category - provides totals across all categories
- Has no category_id field - only month, year, and owner
- Returns one row per month/year instead of one row per month/year/category
- Ideal for summary dashboards and overall financial tracking
Calculations
The calculations are identical to transaction_history_view:
income (Income)
- Sums only positive amounts across all categories
expenses (Expenses)
- Sums only negative amounts across all categories
- Uses
ABS()to obtain positive values
balance (Balance)
- Simple sum of all amounts
- Equals:
income - expenses
Usage
Example Data
Comparison with transaction_history_view
spending_goal_view
Compares defined budget goals with actual expenses per budget, month, and year.
Purpose
This view is used for:
- Budget Tracking: Display progress against budget goals
- Overspending Warnings: Notifications when budgets are exceeded
- Budget Dashboards: Visual representation of expenses vs. goals
- Financial Planning: Analysis of budget compliance
Fields
| Field | Type | Description | Calculation |
|---|---|---|---|
month | NUMBER | Month (1-12) | EXTRACT(MONTH FROM transaction.processed_at) |
year | NUMBER | Year (e.g., 2024) | EXTRACT(YEAR FROM transaction.processed_at) |
budget_id | UUID | Budget ID | From budget.id |
owner_id | VARCHAR | Owner's ID | From budget.owner_id |
spending_goal | NUMBER | Defined budget goal | From budget.budget |
spending_so_far | NUMBER | Expenses so far (positive) | Sum of all negative transfer_amount values (absolute) |
SQL Logic
Joins
- INNER JOIN budget_category: Links budget with associated categories
- LEFT JOIN transaction: Links transactions (LEFT JOIN to also show budgets without transactions)
Calculations
spending_so_far (Expenses So Far)
- Sums only expenses (negative amounts)
ABS()makes the values positiveCOALESCE(..., 0)returns 0 when no transactions exist
Usage
Example Data
Metrics
The following metrics can be calculated with this view:
Performance Considerations
Indexes
For optimal performance, the following indexes should be created:
Materialized Views
For very large datasets, views can be implemented as materialized views:
Caching
Views should be cached at the application layer:
View Updates
Views are automatically updated since they are implemented as regular (non-materialized) views. Each query accesses the most current data.
Drizzle Migration
Views must be created manually in migrations, as Drizzle Kit does not generate them automatically:
Testing
Views can be tested with Drizzle ORM:
BudgetBuddyDE