Skip to content

Domain.Command reference

Source: packages/domain/src/Command.ts

A request to change the model. It names its input, which includes the id of every aggregate instance it involves, the events it may record, the rejections it may return, the middleware that guards it, and the reply its caller receives. An aggregate implements it for one instance; a Domain.Decision implements it across several.

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

middleware runs in list order before the command reaches its aggregate: the first entry runs first, and what it provides, such as the current actor, reaches the entries after it and decide.

import * as Domain from "@alchemy.run/domain";
import * as Schema from "effect/Schema";
import { AccountId, MoneyWithdrawn } from "./Account.ts";
import { MfaChallenge } from "./MfaChallenge.ts";
import { Session } from "./Session.ts";
export class WithdrawMoney extends Domain.Command("WithdrawMoney", {
middleware: [Session, MfaChallenge],
input: { accountId: AccountId, amount: Schema.Int },
events: [MoneyWithdrawn],
reply: { balance: Schema.Int },
}) {}

A command that involves more than one aggregate instance names each of them in its input, and a Domain.Decision implements it.

import * as Domain from "@alchemy.run/domain";
import * as Schema from "effect/Schema";
import { AccountId, InsufficientFunds, TransferReceived, TransferSent } from "./Account.ts";
export class TransferMoney extends Domain.Command("TransferMoney", {
input: { from: AccountId, to: AccountId, amount: Schema.Int },
events: [TransferSent, TransferReceived],
rejects: [InsufficientFunds],
}) {}

Pass a commandId, and a resend with the same id returns the first receipt instead of applying the command again.

const receipt = yield* bank.WithdrawMoney({ accountId, amount: 2_000 }, { commandId: requestId });

A command answers as soon as its events are committed; the views apply them a moment later. Through an Api, pass consistency: "strong" to answer once every view the events reach has applied them, so a read of a view after the answer includes the command. A view that takes longer than 10 seconds fails the call with ConsistencyTimeout; the command is committed all the same. Domain.DefaultConsistency sets it for every command of a client.

yield* bank.DepositMoney({ accountId, amount: 500 }, { consistency: "strong" });
const summary = yield* bank.AccountSummary.get(accountId); // includes the deposit
const receipt = yield* bank.DepositMoney({ accountId, amount: 500 });
const later = yield* bank.AccountSummary.get(accountId, { atLeast: receipt });

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.

An Api’s MCP tools, CLI help and OpenAPI document show its description, in place of the sentence they would generate.

export class WithdrawMoney extends Domain.Command(
"WithdrawMoney",
{ input: { accountId: AccountId, amount: Schema.Int }, events: [MoneyWithdrawn] },
{ description: "Take money out of an account. The amount is in cents." },
) {}