Domain.Decision reference
Decision
Section titled “Decision”Source:
packages/domain/src/Decision.ts
A command decided against several aggregate instances at once. It reads the instances the command names, decides against all of their states together, and records events on them. Every event commits, or none does: a transfer never debits one account without crediting the other.
A decision keeps no state of its own. Each instance folds what is recorded
on it with its own evolve, so views, projections and policies see those
events like any other.
Decision: Deciding across aggregates
Section titled “Decision: Deciding across aggregates”The command’s input names every instance it involves.
import * as Domain from "@alchemy.run/domain";import * as Schema from "effect/Schema";
export const AccountId = Schema.String.pipe(Schema.brand("AccountId"));
export class TransferSent extends Domain.Event("TransferSent", { amount: Schema.Int }) {}export class TransferReceived extends Domain.Event("TransferReceived", { amount: Schema.Int }) {}export class InsufficientFunds extends Domain.Rejection("InsufficientFunds") {}
export class TransferMoney extends Domain.Command("TransferMoney", { input: { from: AccountId, to: AccountId, amount: Schema.Int }, events: [TransferSent, TransferReceived], rejects: [InsufficientFunds],}) {}
export class Account extends Domain.Aggregate("Account", { id: AccountId, state: Schema.Struct({ balance: Schema.Int }), initial: { balance: 0 }, events: [TransferSent, TransferReceived], evolve: { TransferSent: (account, e) => ({ balance: account.balance - e.amount }), TransferReceived: (account, e) => ({ balance: account.balance + e.amount }), },}) {}
export class Transfer extends Domain.Decision(TransferMoney, { reads: { from: Account, to: Account }, keyOf: (cmd) => ({ from: cmd.from, to: cmd.to }), decide: ({ from, to }, cmd) => cmd.amount > from.state.balance ? new InsufficientFunds() : [ from.record(new TransferSent({ amount: cmd.amount })), to.record(new TransferReceived({ amount: cmd.amount })), ],}) {}reads names the aggregates and keyOf the instance of each a command
is for. A model checks that it hosts every aggregate in reads.
Decision: Reading several instances under one name
Section titled “Decision: Reading several instances under one name”keyOf may return an array of ids for a name. decide then receives an
array for it.
export class PayOut extends Domain.Command("PayOut", { input: { from: AccountId, to: Schema.Array(AccountId), amount: Schema.Int }, events: [TransferSent, TransferReceived],}) {}
export class Payout extends Domain.Decision(PayOut, { reads: { from: Account, to: Account }, keyOf: (cmd) => ({ from: cmd.from, to: cmd.to }), decide: ({ from, to }, cmd) => to.flatMap((account) => [ from.record(new TransferSent({ amount: cmd.amount })), account.record(new TransferReceived({ amount: cmd.amount })), ]),}) {}Decision: Stating what it must keep
Section titled “Decision: Stating what it must keep”transitions are rules about the step: the states the decision read,
before and after, with the command and the events. Nothing runs one when a
command arrives. Verify.model and Prove.decision prove that decide
can’t break one, and that it keeps the rules of each aggregate it records
on.
export class Transfer extends Domain.Decision(TransferMoney, { reads: { from: Account, to: Account }, keyOf: (cmd) => ({ from: cmd.from, to: cmd.to }), decide: ({ from, to }, cmd) => cmd.amount > from.state.balance ? new InsufficientFunds() : [ from.record(new TransferSent({ amount: cmd.amount })), to.record(new TransferReceived({ amount: cmd.amount })), ], transitions: { MoneyIsConserved: ({ before, after }) => after.from.balance + after.to.balance === before.from.balance + before.to.balance, },}) {}Decision: Hosting and sending
Section titled “Decision: Hosting and sending”List the decision in its model. The class is a service tag: yield* it
for its client.
export class Bank extends Domain.Model("Bank", { aggregates: [Account], decisions: [Transfer],}) {}
const transfer = yield* Transfer;yield* transfer.send(new TransferMoney({ from, to, amount: 30 }));Decision: Authorizing in decide
Section titled “Decision: Authorizing in decide”decide can be an Effect.fn that reads what its command’s middleware
provides.
decide: Effect.fn(function* ({ from, to }, cmd) { const actor = yield* CurrentActor; if (actor.customerId !== from.state.owner) return yield* new NotOwner(); return [ from.record(new TransferSent({ amount: cmd.amount })), to.record(new TransferReceived({ amount: cmd.amount })), ];}),