This change refactors Events and our use of super-struct's objects for Command request payloads.
Rename Event to DomainEvent.
Refactor DomainEvent to a mix-in pattern that constructs a class so that all events have a class and developers do not need to worry about as const for type discrimination.
* Add new `Type` mix-in to replace use of `object` super-struct. Our tenet is to give all shapes of data a class name.
* Refactor `Command` to take `intent` instead of a `request`. TODO (open issue): define `confirmation` signature for the Command (it is `any` right now).
* Refactor `Command` to allow developers to return either an array of events or the events and a `response` (to be renamed to `confirmation`)
```ts
export const CreateFlightCommandHandler = new Command(
{
__filename,
aggregate: FlightScheduleAggregate,
intent: CreateFlightIntent,
events: [FlightCreatedEvent],
},
async (command, aggregate) => {
const flight = await aggregate.get(command.flightNo);
if (flight) {
throw "Can only create new flights that don't already exist";
}
// Option 1 - no response to the caller, only events added to the bus
return [
new FlightCreatedEvent({
payload: command,
}),
];
// Option 2 - response returned to command caller and events added to the bus
return {
response: "OK",
events: [
new FlightCreatedEvent({
payload: command,
}),
]
}
},
);
Closes #21 Closes #22
This change refactors Events and our use of super-struct's objects for Command request payloads.
Event
toDomainEvent
.DomainEvent
to a mix-in pattern that constructs a class so that all events have a class and developers do not need to worry aboutas const
for type discrimination.new ScheduledFlightAdded({ payload: { flightNo, add: { day, scheduledArrival, scheduledDeparture, }, }, })