Skip to content

Domain.Model reference

Source: packages/domain/src/Model.ts

The aggregates, views and policies you host together. layer(policies) builds them all on the Platform you provide, and provides each aggregate and view as a service.

import * as Domain from "@alchemy.run/domain";
import * as Layer from "effect/Layer";
import { Account } from "./Account.ts";
import { AccountSummary } from "./AccountSummary.ts";
import { Customer } from "./Customer.ts";
import { MainAccountProvisioning, MainAccountProvisioningLive } from "./MainAccountProvisioning.ts";
export class Bank extends Domain.Model("Bank", {
aggregates: [Account, Customer],
views: [AccountSummary],
policies: [MainAccountProvisioning],
}) {}
export const BankInMemory = Bank.layer(MainAccountProvisioningLive).pipe(
Layer.provide(Domain.InMemory),
);

Building the Layer checks the definitions first: a view consuming a source keyed by another aggregate without keyOf, a view with state but no initial, a policy listening for an event no source emits, a stream with two writers, a schema with no JSON form. It dies with a Domain.DefinitionError for each problem, all in one Cause, so a test that builds the Layer reports every one. Problems that depend on what a handler returns, such as decide returning an event its command does not declare, surface when the handler runs.

import { it } from "@effect/vitest";
import * as Layer from "effect/Layer";
import { BankInMemory } from "./Bank.ts";
it.effect("the model is well defined", () => Layer.build(BankInMemory));

List each Domain.Decision in decisions. Every command has one implementation, an aggregate or a decision: a second is a type error on its entry.

export class Bank extends Domain.Model("Bank", {
aggregates: [Account],
decisions: [Transfer],
}) {}

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.

A model lists its declarations, so tooling can walk them and read each one’s annotations.

export class Bank extends Domain.Model(
"Bank",
{ aggregates: [Account], views: [AccountSummary] },
{ description: "Accounts and their balances." },
) {}
const glossary = Bank.commands.map((command) => `${command.tag}: ${command.annotations.description}`);