The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: "Part 2: Read it with a view" |
| 3 | description: Show each account's balance and a statement of every movement, kept up to date as events arrive. |
| 4 | sidebar: |
| 5 | order: 2 |
| 6 | --- |
| 7 | |
| 8 | The account decides, but it doesn't answer questions. Add two views: a summary with the balance, and a statement with one line per movement. |
| 9 | |
| 10 | ## Show the balance |
| 11 | |
| 12 | ```typescript |
| 13 | // src/AccountSummary.view.ts |
| 14 | import * as Domain from "@alchemy.run/domain"; |
| 15 | import * as Schema from "effect/Schema"; |
| 16 | import { Account } from "./Account.aggregate.ts"; |
| 17 | |
| 18 | export class AccountSummary extends Domain.View("AccountSummary", { |
| 19 | from: [Account], |
| 20 | key: Account, |
| 21 | state: Schema.Struct({ owner: Schema.NullOr(Schema.String), balance: Schema.Int }), |
| 22 | initial: { owner: null, balance: 0 }, |
| 23 | evolve: { |
| 24 | AccountOpened: (v, e) => ({ ...v, owner: e.owner }), |
| 25 | MoneyDeposited: (v, e) => ({ ...v, balance: e.balanceAfter }), |
| 26 | MoneyWithdrawn: (v, e) => ({ ...v, balance: e.balanceAfter }), |
| 27 | }, |
| 28 | }) {} |
| 29 | ``` |
| 30 | |
| 31 | A view folds events into state, one copy per key. `key: Account` gives every account its own summary. |
| 32 | |
| 33 | ## Host the view |
| 34 | |
| 35 | ```diff lang="typescript" |
| 36 | // src/Bank.model.ts |
| 37 | import * as Domain from "@alchemy.run/domain"; |
| 38 | import { Account } from "./Account.aggregate.ts"; |
| 39 | +import { AccountSummary } from "./AccountSummary.view.ts"; |
| 40 | |
| 41 | export class Bank extends Domain.Model("Bank", { |
| 42 | aggregates: [Account], |
| 43 | + views: [AccountSummary], |
| 44 | }) {} |
| 45 | ``` |
| 46 | |
| 47 | ## Check the balance |
| 48 | |
| 49 | ```typescript |
| 50 | // test/views.test.ts |
| 51 | import { it } from "@effect/vitest"; |
| 52 | import { Story } from "@alchemy.run/domain/Story"; |
| 53 | import { Account } from "../src/Account.aggregate.ts"; |
| 54 | import { AccountSummary } from "../src/AccountSummary.view.ts"; |
| 55 | import { Bank } from "../src/Bank.model.ts"; |
| 56 | import { DepositMoney } from "../src/DepositMoney.command.ts"; |
| 57 | import { AccountOpened } from "../src/OpenAccount.command.ts"; |
| 58 | import { WithdrawMoney } from "../src/WithdrawMoney.command.ts"; |
| 59 | |
| 60 | const story = Story.make(Bank); |
| 61 | const account = Account.ref("a-1"); |
| 62 | |
| 63 | it.effect("the summary follows the balance", () => |
| 64 | story( |
| 65 | Story.given(account, new AccountOpened({ owner: "sam" })), |
| 66 | Story.when(account, new DepositMoney({ amount: 100 })), |
| 67 | Story.when(account, new WithdrawMoney({ amount: 30 })), |
| 68 | Story.view(AccountSummary, account, { owner: "sam", balance: 70 }), |
| 69 | ), |
| 70 | ); |
| 71 | ``` |
| 72 | |
| 73 | `view` checks a key's state after everything before it has been applied. A partial object checks only the fields you give. |
| 74 | |
| 75 | ## Run the story |
| 76 | |
| 77 | ```sh |
| 78 | bun vitest run |
| 79 | ``` |
| 80 | |
| 81 | The view is right, but the story fails: |
| 82 | |
| 83 | ``` |
| 84 | committed events no step asserts (assert them with then): Account/a-1: MoneyDeposited { amount: 100, balanceAfter: 100 }, MoneyWithdrawn { amount: 30,… (from step 2) |
| 85 | |
| 86 | ✓ 1 Given Account/a-1: AccountOpened { owner: "sam" } |
| 87 | ✓ 2 When Account/a-1: DepositMoney { amount: 100 } |
| 88 | ✓ 3 When Account/a-1: WithdrawMoney { amount: 30 } |
| 89 | ✓ 4 View AccountSummary(a-1): { owner: "sam", balance: 70 } |
| 90 | ✗ end Account/a-1: MoneyDeposited { amount: 100, balanceAfter: 100 }, MoneyWithdrawn { amount: 30,… (from step 2) |
| 91 | events so far |
| 92 | Account/a-1: AccountOpened → MoneyDeposited → MoneyWithdrawn |
| 93 | ``` |
| 94 | |
| 95 | Every event a command records needs a `then`. The story checked the view, but nothing checked what the two commands recorded. |
| 96 | |
| 97 | ## Assert the events |
| 98 | |
| 99 | ```diff lang="typescript" |
| 100 | // test/views.test.ts |
| 101 | -import { DepositMoney } from "../src/DepositMoney.command.ts"; |
| 102 | +import { DepositMoney, MoneyDeposited } from "../src/DepositMoney.command.ts"; |
| 103 | import { AccountOpened } from "../src/OpenAccount.command.ts"; |
| 104 | -import { WithdrawMoney } from "../src/WithdrawMoney.command.ts"; |
| 105 | +import { MoneyWithdrawn, WithdrawMoney } from "../src/WithdrawMoney.command.ts"; |
| 106 | // ... |
| 107 | Story.when(account, new WithdrawMoney({ amount: 30 })), |
| 108 | + Story.then( |
| 109 | + account, |
| 110 | + new MoneyDeposited({ amount: 100, balanceAfter: 100 }), |
| 111 | + new MoneyWithdrawn({ amount: 30, balanceAfter: 70 }), |
| 112 | + ), |
| 113 | Story.view(AccountSummary, account, { owner: "sam", balance: 70 }), |
| 114 | ``` |
| 115 | |
| 116 | `then` takes several events in the order they were recorded. Run `bun vitest run` again and the story passes. A passing story has checked everything the model did. |
| 117 | |
| 118 | ## Describe a statement line |
| 119 | |
| 120 | ```typescript |
| 121 | // src/Statement.view.ts |
| 122 | import * as Domain from "@alchemy.run/domain"; |
| 123 | import * as Schema from "effect/Schema"; |
| 124 | |
| 125 | export class StatementLine extends Domain.Event("StatementLine", { |
| 126 | kind: Schema.Literals(["deposit", "withdrawal"]), |
| 127 | amount: Schema.Int, |
| 128 | balance: Schema.Int, |
| 129 | }) {} |
| 130 | ``` |
| 131 | |
| 132 | A view can record events of its own. One `StatementLine` describes any movement of money. |
| 133 | |
| 134 | ## Emit a line for every movement |
| 135 | |
| 136 | ```diff lang="typescript" |
| 137 | // src/Statement.view.ts |
| 138 | import * as Domain from "@alchemy.run/domain"; |
| 139 | import * as Schema from "effect/Schema"; |
| 140 | +import { Account } from "./Account.aggregate.ts"; |
| 141 | |
| 142 | export class StatementLine extends Domain.Event("StatementLine", { |
| 143 | kind: Schema.Literals(["deposit", "withdrawal"]), |
| 144 | amount: Schema.Int, |
| 145 | balance: Schema.Int, |
| 146 | }) {} |
| 147 | + |
| 148 | +export class Statement extends Domain.View("Statement", { |
| 149 | + from: [Account], |
| 150 | + key: Account, |
| 151 | + events: [StatementLine], |
| 152 | + emit: { |
| 153 | + MoneyDeposited: (e) => |
| 154 | + new StatementLine({ kind: "deposit", amount: e.amount, balance: e.balanceAfter }), |
| 155 | + MoneyWithdrawn: (e) => |
| 156 | + new StatementLine({ kind: "withdrawal", amount: e.amount, balance: e.balanceAfter }), |
| 157 | + }, |
| 158 | +}) {} |
| 159 | ``` |
| 160 | |
| 161 | `emit` takes each account event and returns the line to record. The statement keeps no state of its own; its lines are its history. |
| 162 | |
| 163 | ## Host the statement |
| 164 | |
| 165 | ```diff lang="typescript" |
| 166 | // src/Bank.model.ts |
| 167 | import { Account } from "./Account.aggregate.ts"; |
| 168 | import { AccountSummary } from "./AccountSummary.view.ts"; |
| 169 | +import { Statement } from "./Statement.view.ts"; |
| 170 | |
| 171 | export class Bank extends Domain.Model("Bank", { |
| 172 | aggregates: [Account], |
| 173 | - views: [AccountSummary], |
| 174 | + views: [AccountSummary, Statement], |
| 175 | }) {} |
| 176 | ``` |
| 177 | |
| 178 | ## Check the statement |
| 179 | |
| 180 | ```diff lang="typescript" |
| 181 | // test/views.test.ts |
| 182 | import { DepositMoney, MoneyDeposited } from "../src/DepositMoney.command.ts"; |
| 183 | import { AccountOpened } from "../src/OpenAccount.command.ts"; |
| 184 | +import { Statement } from "../src/Statement.view.ts"; |
| 185 | import { MoneyWithdrawn, WithdrawMoney } from "../src/WithdrawMoney.command.ts"; |
| 186 | // ... |
| 187 | ), |
| 188 | ); |
| 189 | + |
| 190 | +it.effect("the statement has one line per movement", () => |
| 191 | + story( |
| 192 | + Story.given(account, new AccountOpened({ owner: "sam" })), |
| 193 | + Story.when(account, new DepositMoney({ amount: 100 })), |
| 194 | + Story.when(account, new WithdrawMoney({ amount: 30 })), |
| 195 | + Story.then( |
| 196 | + account, |
| 197 | + new MoneyDeposited({ amount: 100, balanceAfter: 100 }), |
| 198 | + new MoneyWithdrawn({ amount: 30, balanceAfter: 70 }), |
| 199 | + ), |
| 200 | + Story.emitted(Statement, account, [ |
| 201 | + { kind: "deposit", amount: 100, balance: 100 }, |
| 202 | + { kind: "withdrawal", amount: 30, balance: 70 }, |
| 203 | + ]), |
| 204 | + ), |
| 205 | +); |
| 206 | ``` |
| 207 | |
| 208 | `emitted` checks every event a view key has emitted, in order. |
| 209 | |
| 210 | ```sh |
| 211 | bun vitest run |
| 212 | ``` |
| 213 | |
| 214 | The account decides, and two views answer: what the balance is, and how it got there. |
| 215 | |
| 216 | :::note |
| 217 | Next, open an account for every new customer with a [policy](/domain/tutorial/part-3). Views can also emit events for other views, and route one event to several keys: see [View](/domain/view). |
| 218 | ::: |