The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: "Part 8: Change a deployed model" |
| 3 | description: Change what a view computes after it has data, add a view over every account, and follow what each deploy does with the events already recorded. |
| 4 | sidebar: |
| 5 | order: 8 |
| 6 | --- |
| 7 | |
| 8 | The bank is deployed and its accounts have events. A view's state is derived from those events, so changing a view means computing it again. In this part you change the summary, add a total across every account, and follow what each deploy does with the history. |
| 9 | |
| 10 | ## Count every movement |
| 11 | |
| 12 | ```diff lang="typescript" |
| 13 | // src/AccountSummary.view.ts |
| 14 | import * as Domain from "@alchemy.run/domain"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as Schema from "effect/Schema"; |
| 17 | import { Account } from "./Account.aggregate.ts"; |
| 18 | import { CurrentActor } from "./CurrentActor.context.ts"; |
| 19 | import { NotOwner } from "./NotOwner.rejection.ts"; |
| 20 | import { Session } from "./Session.middleware.ts"; |
| 21 | |
| 22 | export class AccountSummary extends Domain.View("AccountSummary", { |
| 23 | from: [Account], |
| 24 | key: Account, |
| 25 | middleware: [Session], |
| 26 | rejects: [NotOwner], |
| 27 | - state: Schema.Struct({ owner: Schema.NullOr(Schema.String), balance: Schema.Int }), |
| 28 | - initial: { owner: null, balance: 0 }, |
| 29 | + state: Schema.Struct({ |
| 30 | + owner: Schema.NullOr(Schema.String), |
| 31 | + balance: Schema.Int, |
| 32 | + movements: Schema.Int, |
| 33 | + }), |
| 34 | + initial: { owner: null, balance: 0, movements: 0 }, |
| 35 | evolve: { |
| 36 | AccountOpened: (v, e) => ({ ...v, owner: e.owner }), |
| 37 | - MoneyDeposited: (v, e) => ({ ...v, balance: e.balanceAfter }), |
| 38 | - MoneyWithdrawn: (v, e) => ({ ...v, balance: e.balanceAfter }), |
| 39 | + MoneyDeposited: (v, e) => ({ ...v, balance: e.balanceAfter, movements: v.movements + 1 }), |
| 40 | + MoneyWithdrawn: (v, e) => ({ ...v, balance: e.balanceAfter, movements: v.movements + 1 }), |
| 41 | }, |
| 42 | validate: Effect.fn(function* (_request, summary) { |
| 43 | const actor = yield* CurrentActor; |
| 44 | if (summary?.owner && summary.owner !== actor.customerId) return yield* new NotOwner(); |
| 45 | }), |
| 46 | }) {} |
| 47 | ``` |
| 48 | |
| 49 | The summary now counts deposits and withdrawals. Deployed as it is, each stored summary would keep what the old code computed, and only movements from now on would be counted. |
| 50 | |
| 51 | ## Bump the version |
| 52 | |
| 53 | ```diff lang="typescript" |
| 54 | // src/AccountSummary.view.ts |
| 55 | export class AccountSummary extends Domain.View("AccountSummary", { |
| 56 | from: [Account], |
| 57 | key: Account, |
| 58 | // ... |
| 59 | validate: Effect.fn(function* (_request, summary) { |
| 60 | const actor = yield* CurrentActor; |
| 61 | if (summary?.owner && summary.owner !== actor.customerId) return yield* new NotOwner(); |
| 62 | }), |
| 63 | + version: 1, |
| 64 | }) {} |
| 65 | ``` |
| 66 | |
| 67 | A view's `version` says it computes something different from what's stored. Bump it whenever you change a deployed view's `evolve`, `state`, `keyOf`, or the events and sources it reads, and only ever increase it. |
| 68 | |
| 69 | ## Deploy the change |
| 70 | |
| 71 | ```sh |
| 72 | bun alchemy deploy |
| 73 | ``` |
| 74 | |
| 75 | The deploy uploads the Worker and returns without waiting on the summaries. Each summary is keyed by its account and reads only that account's events, so it's rebuilt where it is: it keeps its Durable Object and its storage, and the first time it's touched after the deploy it reads its account's events again, with the new code, before it answers. |
| 76 | |
| 77 | ## Print the count |
| 78 | |
| 79 | ```diff lang="typescript" |
| 80 | // scripts/try.ts |
| 81 | // Print the balance now and after every change, in the background. |
| 82 | yield* summaries.pipe( |
| 83 | - Stream.runForEach((summary) => Effect.log(`balance: ${summary?.balance}`)), |
| 84 | + Stream.runForEach((summary) => |
| 85 | + Effect.log(`balance: ${summary?.balance}, movements: ${summary?.movements}`), |
| 86 | + ), |
| 87 | Effect.forkChild, |
| 88 | ); |
| 89 | ``` |
| 90 | |
| 91 | ```sh |
| 92 | bun scripts/try.ts https://bank-bankworker-….workers.dev |
| 93 | ``` |
| 94 | |
| 95 | ``` |
| 96 | [12:00:01.527] INFO (#9): balance: 70, movements: 2 |
| 97 | [12:00:02.467] INFO (#9): balance: 170, movements: 3 |
| 98 | [12:00:03.750] INFO (#9): balance: 140, movements: 4 |
| 99 | ``` |
| 100 | |
| 101 | The first line already counts the deposit and withdrawal you made in [Part 5](/domain/tutorial/part-5), which happened before `movements` existed. Your numbers depend on what you've done since. |
| 102 | |
| 103 | ## Read during the rollout |
| 104 | |
| 105 | A Durable Object that was busy when you deployed keeps running the code it started with until it goes idle, for up to 5 minutes by default; see [Code updates](/cloudflare/compute/code-updates). A summary on the old code can't rebuild for version 1, so until it moves, the Worker answers its reads itself: it reads the account's events and computes the summary with the new code, without storing it. Once the object starts on the new code, it rebuilds and answers again. |
| 106 | |
| 107 | ## Total deposits by day |
| 108 | |
| 109 | ```typescript |
| 110 | // src/DailyDeposits.view.ts |
| 111 | import * as Domain from "@alchemy.run/domain"; |
| 112 | import * as DateTime from "effect/DateTime"; |
| 113 | import * as Schema from "effect/Schema"; |
| 114 | import { Account } from "./Account.aggregate.ts"; |
| 115 | |
| 116 | export class DailyDeposits extends Domain.View("DailyDeposits", { |
| 117 | from: [Account], |
| 118 | key: Schema.String, |
| 119 | keyOf: { MoneyDeposited: ({ envelope }) => DateTime.formatIsoDateUtc(envelope.at) }, |
| 120 | state: Schema.Struct({ total: Schema.Int }), |
| 121 | initial: { total: 0 }, |
| 122 | evolve: { MoneyDeposited: (v, e) => ({ total: v.total + e.amount }) }, |
| 123 | }) {} |
| 124 | ``` |
| 125 | |
| 126 | `keyOf` sends every account's deposit to the day it happened, such as `2026-11-02`, so one key adds up deposits from many accounts. |
| 127 | |
| 128 | ## Host the totals |
| 129 | |
| 130 | ```diff lang="typescript" |
| 131 | // src/Bank.model.ts |
| 132 | import * as Domain from "@alchemy.run/domain"; |
| 133 | import { Account } from "./Account.aggregate.ts"; |
| 134 | import { AccountSummary } from "./AccountSummary.view.ts"; |
| 135 | import { Customer } from "./Customer.aggregate.ts"; |
| 136 | +import { DailyDeposits } from "./DailyDeposits.view.ts"; |
| 137 | import { |
| 138 | MainAccountProvisioning, |
| 139 | MainAccountProvisioningLive, |
| 140 | } from "./MainAccountProvisioning.policy.ts"; |
| 141 | import { Statement } from "./Statement.view.ts"; |
| 142 | |
| 143 | export class Bank extends Domain.Model("Bank", { |
| 144 | aggregates: [Account, Customer], |
| 145 | - views: [AccountSummary, Statement], |
| 146 | + views: [AccountSummary, Statement, DailyDeposits], |
| 147 | policies: [MainAccountProvisioning], |
| 148 | }) {} |
| 149 | |
| 150 | export const BankHandlers = MainAccountProvisioningLive; |
| 151 | ``` |
| 152 | |
| 153 | ## Expose the totals |
| 154 | |
| 155 | ```diff lang="typescript" |
| 156 | // src/BankApi.api.ts |
| 157 | import * as Domain from "@alchemy.run/domain"; |
| 158 | import { Bank } from "./Bank.model.ts"; |
| 159 | |
| 160 | export class BankApi extends Domain.Api("BankApi", { |
| 161 | model: Bank, |
| 162 | commands: ["RegisterCustomer", "DepositMoney", "WithdrawMoney"], |
| 163 | - views: ["AccountSummary"], |
| 164 | + views: ["AccountSummary", "DailyDeposits"], |
| 165 | }) {} |
| 166 | ``` |
| 167 | |
| 168 | The totals declare no `validate`, so any client of the Api can read them. |
| 169 | |
| 170 | ## Deploy and wait for the history |
| 171 | |
| 172 | ```sh |
| 173 | bun alchemy deploy |
| 174 | ``` |
| 175 | |
| 176 | ``` |
| 177 | · Backfilling view:DailyDeposits, view:Statement (3s) ... |
| 178 | · Backfilling view:DailyDeposits reading history; view:Statement complete (9s) ... |
| 179 | · Backfilled view:DailyDeposits, view:Statement |
| 180 | ``` |
| 181 | |
| 182 | This deploy waits. A day's total doesn't know which accounts deposited that day, so it can't fetch its own history the way a summary does. Instead, the deploy walks every account the bank has recorded, reads its events, and sends each deposit to its day, then returns once every total has its whole history. It reports how far each view has got while it works, and names every view it keeps filled, so the statement shows up already complete. Deposits made meanwhile wait their turn and arrive in order after the history. |
| 183 | |
| 184 | ## Read today's total |
| 185 | |
| 186 | ```diff lang="typescript" |
| 187 | // scripts/try.ts |
| 188 | import * as Domain from "@alchemy.run/domain"; |
| 189 | +import * as DateTime from "effect/DateTime"; |
| 190 | import * as Effect from "effect/Effect"; |
| 191 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 192 | import * as Layer from "effect/Layer"; |
| 193 | import * as Stream from "effect/Stream"; |
| 194 | import { AccountId } from "../src/Account.id.ts"; |
| 195 | import { BankApi } from "../src/BankApi.api.ts"; |
| 196 | import { CustomerId } from "../src/Customer.id.ts"; |
| 197 | import { sessionClient } from "../src/Session.middleware.ts"; |
| 198 | |
| 199 | const [url, customerId = "c-1"] = process.argv.slice(2); |
| 200 | const accountId = AccountId.make(`${customerId}-main`); |
| 201 | |
| 202 | const program = Effect.gen(function* () { |
| 203 | const bank = yield* BankApi.client; |
| 204 | // ... |
| 205 | yield* bank.WithdrawMoney(accountId, { amount: 30 }); |
| 206 | yield* Effect.sleep("2 seconds"); |
| 207 | + const today = DateTime.formatIsoDateUtc(yield* DateTime.now); |
| 208 | + const totals = yield* bank.DailyDeposits.get(today); |
| 209 | + yield* Effect.log(`deposited today: ${totals?.total}`); |
| 210 | }); |
| 211 | ``` |
| 212 | |
| 213 | Run the script again, and the last line adds up every deposit made today, by every customer, including those from before the totals existed: |
| 214 | |
| 215 | ``` |
| 216 | [12:00:05.910] INFO (#1): deposited today: 5600 |
| 217 | ``` |
| 218 | |
| 219 | ## Change the totals |
| 220 | |
| 221 | ```diff lang="typescript" |
| 222 | // src/DailyDeposits.view.ts |
| 223 | export class DailyDeposits extends Domain.View("DailyDeposits", { |
| 224 | from: [Account], |
| 225 | key: Schema.String, |
| 226 | keyOf: { MoneyDeposited: ({ envelope }) => DateTime.formatIsoDateUtc(envelope.at) }, |
| 227 | - state: Schema.Struct({ total: Schema.Int }), |
| 228 | - initial: { total: 0 }, |
| 229 | - evolve: { MoneyDeposited: (v, e) => ({ total: v.total + e.amount }) }, |
| 230 | + state: Schema.Struct({ total: Schema.Int, count: Schema.Int }), |
| 231 | + initial: { total: 0, count: 0 }, |
| 232 | + evolve: { MoneyDeposited: (v, e) => ({ total: v.total + e.amount, count: v.count + 1 }) }, |
| 233 | + version: 1, |
| 234 | }) {} |
| 235 | ``` |
| 236 | |
| 237 | The totals now count deposits too, so their version goes up, the same as for the summary. |
| 238 | |
| 239 | ## Deploy the rebuild |
| 240 | |
| 241 | ```sh |
| 242 | bun alchemy deploy |
| 243 | ``` |
| 244 | |
| 245 | ``` |
| 246 | · Backfilling view:DailyDeposits#1, view:Statement (3s) ... |
| 247 | · Backfilling view:DailyDeposits#1 reading history; view:Statement complete (11s) ... |
| 248 | · Backfilled view:DailyDeposits#1, view:Statement |
| 249 | ``` |
| 250 | |
| 251 | This deploy waits again. `#1` is the totals' new version. The totals can't rebuild where they are, for the same reason they couldn't fill themselves, so they move to fresh storage and the deploy fills it from every account's events. The old totals are deleted with this deploy, so until it returns, reads see the new totals filling in. A view built on another view is rebuilt whenever the one it reads is, by whichever path that view takes. |
| 252 | |
| 253 | :::note |
| 254 | Both ways a view is rebuilt, and how to fill a new view keyed by its account without making the deploy wait, are in [Change a view: bump its version](/domain/model#change-a-view-bump-its-version). How long a backfill takes as the bank grows is in [History at scale](/domain/backfill). |
| 255 | ::: |