Skip to content

Domain.Aggregate reference

Source: packages/domain/src/Aggregate.ts

One entity that decides, such as a bank account: a consistency boundary. It takes a command, checks it against its current state, and records events or returns a rejection. Each instance handles one command at a time, and its events, new state and reply commit together.

import * as Domain from "@alchemy.run/domain";
import * as Schema from "effect/Schema";
export const AccountId = Schema.String.pipe(Schema.brand("AccountId"));
export class MoneyWithdrawn extends Domain.Event("MoneyWithdrawn", {
amount: Schema.Int,
balanceAfter: Schema.Int,
}) {}
export class InsufficientFunds extends Domain.Rejection("InsufficientFunds", {
balance: Schema.Int,
requested: Schema.Int,
}) {}
export class WithdrawMoney extends Domain.Command("WithdrawMoney", {
input: { accountId: AccountId, amount: Schema.Int },
events: [MoneyWithdrawn],
rejects: [InsufficientFunds],
reply: { balance: Schema.Int },
}) {}
export class Account extends Domain.Aggregate("Account", {
id: AccountId,
state: Schema.Struct({ balance: Schema.Int }),
initial: { balance: 0 },
commands: [WithdrawMoney],
keyOf: (cmd) => cmd.accountId,
decide: {
WithdrawMoney: (account, cmd) =>
cmd.amount > account.balance
? new InsufficientFunds({ balance: account.balance, requested: cmd.amount })
: [new MoneyWithdrawn({ amount: cmd.amount, balanceAfter: account.balance - cmd.amount })],
},
evolve: {
MoneyWithdrawn: (_, e) => ({ balance: e.balanceAfter }),
},
reply: {
WithdrawMoney: (s) => ({ balance: s.balance }),
},
}) {}

A handler can be an Effect.fn that reads what its command’s middleware provides.

decide: {
WithdrawMoney: Effect.fn(function* (account, cmd) {
const actor = yield* CurrentActor;
if (actor.customerId !== account.owner) return yield* new NotOwner();
return [new MoneyWithdrawn({ amount: cmd.amount, balanceAfter: account.balance - cmd.amount })];
}),
},
const accounts = yield* Account;
const { reply } = yield* accounts.send(new WithdrawMoney({ accountId, amount: 30 }));
yield* accounts.schedule(new SendStatement({ accountId }), { after: "30 days", commandId: "statement" });
yield* accounts.unschedule(accountId, "statement"); // cancel it while it is pending

A Domain.Decision records events on the instances it reads. List them in events, with an evolve handler for each. An aggregate that only takes part in decisions leaves out commands, keyOf and decide.

export class UsernameClaim extends Domain.Aggregate("UsernameClaim", {
id: Username,
state: Schema.Union([
Schema.TaggedStruct("Free", {}),
Schema.TaggedStruct("Claimed", { customerId: CustomerId }),
]),
initial: { _tag: "Free" },
events: [UsernameClaimed],
evolve: {
UsernameClaimed: (_, e) => ({ _tag: "Claimed" as const, customerId: e.customerId }),
},
}) {}

The last argument is its annotations: data for what is built on the model, read back as X.annotations. Add your own keys by augmenting Domain.Annotations.

export class Account extends Domain.Aggregate(
"Account",
{
id: AccountId,
state: Schema.Struct({ balance: Schema.Int }),
initial: { balance: 0 },
commands: [DepositMoney],
keyOf: (cmd) => cmd.accountId,
decide: { DepositMoney: (account, cmd) => [new MoneyDeposited({ balanceAfter: account.balance + cmd.amount })] },
evolve: { MoneyDeposited: (_, e) => ({ balance: e.balanceAfter }) },
},
{ description: "A bank account, which keeps its own balance." },
) {}