Model and wire
model() registers your definitions. wire() turns a model into a typed runtime client.
Import
import { memory, model, wire } from '@marcelsud/accord'model(name, contract)
const Bank = model('Bank', {
aggregates: { account: Account, savings: SavingsAccount },
views: { summary: Summary },
policies: [NotifyDeposit],
projections: [Totals],
workflows: { job: Job },
streams: { activity: Activity },
})aggregates
Record<string, Aggregate>. The keys are the client aliases: bank.account, bank.savings.
views optional
Record<string, View>. Exposed as bank.summary.
policies optional
Policy[].
projections optional
Projection[].
workflows optional
Record<string, Workflow>. Exposed as bank.workflows.job.
streams optional
Record<string, Stream>. Exposed as bank.streams.activity.
Rules checked when you call model():
- Every definition name must be unique and non-empty.
- Aliases must be unique and can't be
workflows,streams,flush, orthen. - Views and consumers must subscribe to a registered source, and only to events that source declares.
- Versions are safe integers
>= 1.
wire(model, options?)
const bank = wire(Bank, {
storage: memory(),
services: { rules: { maximum: 1_000 } },
})storage optional
RuntimeStore. Defaults to a fresh memory() store. Pass the same memory() object to share state between runtimes in one process, or a SQLite store for durability.
services
Record<string, Service>. Required when anything in the model declares uses. TypeScript tells you what's missing.
The runtime
| Member | Description |
|---|---|
bank.<aggregate>.<method>(id, ...args) | Run a command. See Aggregate |
bank.<aggregate>.get(id) / .events(id) | Read state / history |
bank.<view>.get(key, options?) | Read a view. See View |
bank.workflows.<name>(input) | Run a workflow |
bank.streams.<name> | Append to and read a stream |
bank.flush() | Deliver pending events to consumers |
flush()
const [, error] = await bank.flush()
if (error !== null) throw errorCatches all consumers up and returns the first delivery failure. Failed deliveries keep their checkpoint and retry on the next flush. There's no background scheduler or automatic backoff.
memory()
The default in-process store. It provides atomic aggregate commits, keyed concurrency, receipts, replay, and consumer checkpoints, and also holds workflow checkpoints.