Skip to content

feat(expense): make total_amount a live summary roll-up (#84)#87

Merged
os-zhuang merged 1 commit into
mainfrom
claude/live-rollups-84
Jul 19, 2026
Merged

feat(expense): make total_amount a live summary roll-up (#84)#87
os-zhuang merged 1 commit into
mainfrom
claude/live-rollups-84

Conversation

@os-zhuang

Copy link
Copy Markdown
Contributor

目的

#84 的第一步:既然 framework#1867(嵌套写崩溃)已修、运行时也实现了 summary 字段类型,把 expense 里手工维护的反规范化 expense_report.total_amount 换成原生 live roll-up

total_amount: Field.summary({
  summaryOperations: { object: 'expense_line', field: 'amount', function: 'sum', relationshipField: 'expense_report' },
})

引擎在每次 expense_line insert/update/delete 时自动重算 —— 无钩子、无反规范化、无客户端维护的 header。

配套改动

  • expense_report.hook.ts:去掉 total_amount = 0 的插入默认(现在是计算字段)。
  • data/index.ts:reports 不再 seed total_amount —— summary 从 seeded lines 推导。
  • 「submitted 需 total > 0」校验改为在 draft→submitted 转移时触发(previous.status != "submitted",此时 lines 已存在),而非每次写入 —— 这样直接以最终状态 seed 的 report 不受 rollup 时序影响。
  • CHARTER.md:重写「Rollup vs. lifecycle hooks」一节。

运行时验证(objectstack dev --fresh,真实 SQLite)

  • 种子加载 0 错误(修正前:submitted report 在 lines 之前插入 → 校验失败,连带 lines 失败)。
  • 某 report 的 total_amount = 2223,其 4 条 seeded line 之和正好 2223 —— 通过 expense_line → expense_reportlookup FK live 重算(不需要 master_detail)。

Gate

pnpm typecheck ✓ · pnpm build ✓ · pnpm format:check ✓。

说明 / 后续

这是 5 个反规范化 rollup 的第一个。其余(procurement received_amount、pm_project、content total_*、compliance last_status)在 #84 跟进 —— 其中 compliance 的「最近一次评估结果」是非聚合,应走嵌套写钩子而非 summary。转换时发现每个模板可能像 expense 一样有校验/种子的时序交互,需逐个验证。

关联:#84、framework#1867、framework#3232。

🤖 Generated with Claude Code


Generated by Claude Code

Now that nested cross-object writes are safe (framework#1867 fixed) and the
runtime computes the `summary` field type, replace the hand-maintained
denormalized `expense_report.total_amount` with a native live roll-up:

    total_amount: Field.summary({
      summaryOperations: { object: 'expense_line', field: 'amount',
                           function: 'sum', relationshipField: 'expense_report' },
    })

The engine recomputes it on every expense_line insert/update/delete — no hook,
no client-maintained header. Supporting changes:
- expense_report.hook.ts: drop the `total_amount = 0` insert default (computed now).
- data/index.ts: stop seeding `total_amount` on reports — the summary derives it
  from the seeded lines as they load.
- The "submitted needs total > 0" validation now fires on the draft→submitted
  TRANSITION (previous.status != "submitted"), when the lines already exist,
  instead of on every write — so a report seeded directly in its final state
  isn't gated by rollup timing.
- CHARTER.md: rewrite the "Rollup vs. lifecycle hooks" section for the live
  summary.

Verified at runtime (objectstack dev --fresh, real SQLite): seed loads with zero
errors and a report's total_amount (2223) equals the sum of its four seeded
lines (2223), recomputed live over the expense_line→expense_report lookup FK.

Gate: pnpm typecheck ✓ · pnpm build ✓ · pnpm format:check ✓.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BZguyAaQbyUpwMZ2gMLaAP
@os-zhuang
os-zhuang merged commit 94192ed into main Jul 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants