feat(expense): make total_amount a live summary roll-up (#84)#87
Merged
Conversation
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
This was referenced Jul 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
目的
#84 的第一步:既然 framework#1867(嵌套写崩溃)已修、运行时也实现了
summary字段类型,把 expense 里手工维护的反规范化expense_report.total_amount换成原生 live roll-up。引擎在每次
expense_lineinsert/update/delete 时自动重算 —— 无钩子、无反规范化、无客户端维护的 header。配套改动
expense_report.hook.ts:去掉total_amount = 0的插入默认(现在是计算字段)。data/index.ts:reports 不再 seedtotal_amount—— summary 从 seeded lines 推导。previous.status != "submitted",此时 lines 已存在),而非每次写入 —— 这样直接以最终状态 seed 的 report 不受 rollup 时序影响。CHARTER.md:重写「Rollup vs. lifecycle hooks」一节。运行时验证(
objectstack dev --fresh,真实 SQLite)total_amount = 2223,其 4 条 seeded line 之和正好 2223 —— 通过expense_line → expense_report的 lookup FK live 重算(不需要 master_detail)。Gate
pnpm typecheck✓ ·pnpm build✓ ·pnpm format:check✓。说明 / 后续
这是 5 个反规范化 rollup 的第一个。其余(procurement
received_amount、pm_project、contenttotal_*、compliancelast_status)在 #84 跟进 —— 其中 compliance 的「最近一次评估结果」是非聚合,应走嵌套写钩子而非 summary。转换时发现每个模板可能像 expense 一样有校验/种子的时序交互,需逐个验证。关联:#84、framework#1867、framework#3232。
🤖 Generated with Claude Code
Generated by Claude Code