Skip to content

Database Schema

ER Diagram

Tables

user_config

Single-row table (id luôn = 1). Lưu trữ thông tin tính Runway.

ColumnTypeConstraintMô tả
idINTEGERPK, CHECK(id = 1)Luôn = 1
total_cashINTEGERNOT NULLTổng tài sản
monthly_survival_costINTEGERNOT NULLChi phí sinh tồn / tháng
daily_burn_rateINTEGERNOT NULLTốc độ đốt tiền / ngày
currencyTEXTNOT NULL, DEFAULT 'VND'Đơn vị tiền tệ
created_atTEXTNOT NULLTimestamp tạo

categories

Danh mục chi tiêu, mỗi danh mục có tier.

ColumnTypeConstraintMô tả
idINTEGERPK AUTOINCREMENT-
nameTEXTNOT NULL, UNIQUETên danh mục
tierINTEGERNOT NULL, CHECK(1,2,3)1=Survival, 2=Comfort, 3=Wasted
is_systemINTEGERNOT NULL, DEFAULT 01 nếu tạo từ Onboarding
created_atTEXTNOT NULLTimestamp tạo

transactions

Giao dịch thu/chi.

ColumnTypeConstraintMô tả
idINTEGERPK AUTOINCREMENT-
amountINTEGERNOT NULLSố tiền
dateTEXTNOT NULL, DEFAULT date('now')Ngày giao dịch (YYYY-MM-DD)
category_idINTEGERFK → categories(id) ON DELETE CASCADEDanh mục
noteTEXTNOT NULL, DEFAULT ''Ghi chú
created_atTEXTNOT NULLTimestamp tạo

recurring_expenses

Chi phí định kỳ tự động lặp lại.

ColumnTypeConstraintMô tả
idINTEGERPK AUTOINCREMENT-
nameTEXTNOT NULLTên khoản chi
amountINTEGERNOT NULLSố tiền
frequencyTEXTNOT NULL, CHECK('daily','monthly')Tần suất
day_of_monthINTEGERnullable, CHECK(1-31)Ngày trong tháng (monthly)
category_idINTEGERFK → categories(id) ON DELETE SET NULLDanh mục
last_generated_dateTEXTnullableNgày tạo transaction gần nhất
created_atTEXTNOT NULLTimestamp tạo

Indexes

IndexTableColumnMục đích
idx_transactions_datetransactionsdateTối ưu query theo ngày
idx_transactions_categorytransactionscategory_idTối ưu query theo danh mục

Migration History

VersionMô tả
v1Tạo bảng user_config, categories, transactions + indexes
v2Thêm bảng recurring_expenses
v3Thêm cột currency vào user_config (default 'VND')
v4Thêm category_id và last_generated_date vào recurring_expenses

Lưu ý

  • Amounts lưu dưới dạng INTEGER (không phải float) để tránh lỗi làm tròn
  • Dates lưu dưới dạng TEXT (ISO format YYYY-MM-DD)
  • date('now') trong SQLite trả về UTC - cần chú ý timezone khi hiển thị