Domain.Rewrite reference
Rewrite
Section titled “Rewrite”Source:
packages/domain/src/Rewrite.ts
A change to the events an aggregate instance has already recorded: for removing personal data, or correcting what should never have been stored. Each handler keeps, replaces or removes one kind of event. Running it changes the instance’s stored events and state, then every view and projection built from them.
Sequence numbers never change: a removed event leaves a gap. Policies and sagas are not told, since what they did has already happened.
Rewrite: Removing personal data
Section titled “Rewrite: Removing personal data”import * as Domain from "@alchemy.run/domain";import { Account, AccountOpened } from "./Account.ts";import { CustomerId } from "./Customer.ts";
export class ForgetOwner extends Domain.Rewrite("ForgetOwner", { of: Account, input: { customerId: CustomerId }, events: { AccountOpened: (event, { customerId }) => event.customerId === customerId ? new AccountOpened({ ...event, owner: "" }) : event, },}) {}Rewrite: Running it
Section titled “Rewrite: Running it”List it in the model’s rewrites, then run it on one instance. The call
returns once the instance’s own history is rewritten; rewritten waits
until every view and projection has caught up.
const accounts = yield* Account;const receipt = yield* accounts.rewrite(accountId, new ForgetOwner({ customerId }));yield* accounts.rewritten(receipt);Rewrite: Removing an instance
Section titled “Rewrite: Removing an instance”export class DeleteCustomer extends Domain.Rewrite("DeleteCustomer", { of: Customer, delete: true,}) {}
yield* customers.rewrite(customerId, new DeleteCustomer());Rewrite: Removing an event
Section titled “Rewrite: Removing an event”export class DropNotes extends Domain.Rewrite("DropNotes", { of: Account, events: { NoteAdded: () => null },}) {}