Skip to content

Domain.Rewrite reference

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.

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,
},
}) {}

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);
export class DeleteCustomer extends Domain.Rewrite("DeleteCustomer", {
of: Customer,
delete: true,
}) {}
yield* customers.rewrite(customerId, new DeleteCustomer());
export class DropNotes extends Domain.Rewrite("DropNotes", {
of: Account,
events: { NoteAdded: () => null },
}) {}