Skip to content

Application Layers

概述

StrawMoneyBook frontend 採分層架構:UI 負責呈現與互動,Store 管理 reactive 狀態,Service 實作業務規則,Repository 封裝 SQL。Backend 則以 route handler → collab store → SQLite 為主線。

理想依賴方向

text
ui/views
  → ui/stores
    → core/services
      → core/repositories
        → core/db

ui/services(runtime / sync / platform)
  → core/services + services/* API clients

backend/routes/*
  → backend/collab/*
    → collab-store.sqlite
mermaid
flowchart TB
  Views[ui/views]
  Stores[ui/stores]
  UiSvc[ui/services]
  CoreSvc[core/services]
  Repo[core/repositories]
  DB[(SQLite)]
  Views --> Stores
  Views --> UiSvc
  Stores --> CoreSvc
  UiSvc --> CoreSvc
  CoreSvc --> Repo
  Repo --> DB

各層職責

層級目錄職責
Viewfrontend/src/ui/views/頁面、表單、路由級 UI
Componentfrontend/src/ui/components/可重用 UI 區塊
Storefrontend/src/ui/stores/Pinia 狀態、action 委派 Service
UI Servicefrontend/src/ui/services/啟動/runtime、同步、平台整合
Core Servicefrontend/src/core/services/領域業務邏輯(交易、預算、借貸…)
Repositoryfrontend/src/core/repositories/SQL CRUD、查詢
Domainfrontend/src/core/domain/純函式領域規則(posting、balance)
Shared APIfrontend/src/services/對 backend / Google 的 HTTP client

Backend:

層級目錄職責
Routebackend/src/routes/HTTP 入口、auth 檢查
Collabbackend/src/collab/使用者、共同帳本、session store

已知例外與收斂方向

architecture-review-smb-130.md

問題現況收斂方向
core → ui 反向依賴部分 core service 曾直接碰 Pinia / routerbootstrap / membership 已拆出 UI 層協調
View 直連 Repository分析、搜尋、日曆等頁曾直接 import repo統一走 transaction-query.service.js
啟動邏輯過度集中main.js 堆疊過多 side effect拆到 app-bootstrap / app-runtime / ledger-runtime
Store 當 coordinatorledger.store 曾負責 cross-store reset邏輯移至 ledger-runtime.service.js
Backend route 過胖auth / shared-ledgers 單檔過長已拆成 routes/auth/routes/shared-ledgers/ 子目錄

維護守則

  • 頁面優先透過 Store 操作,不直接在 view 寫 DB
  • 交易查詢統一經 transaction-query.service.js
  • 業務規則改 Service,SQL 改 Repository
  • 日期 key 統一用 core/utils/id.js 的 helper

Backend 路由分派

backend/src/routes/route-table.js 將 pathname prefix 對應到 handler 模組:

  • /api/auth/* — 登入、refresh、Google、Email
  • /api/shared-ledgers/* — 共同帳本 CRUD、快照、邀請
  • /api/referrals/* — 邀請碼、會員加成
  • /api/admin/* — 管理後台

下一步

StrawMoneyBook Developer Guide