ryan-mars / stochastic

TypeScript framework for building event-driven services. Easily go from Event Storming → Code.
MIT License
6 stars 1 forks source link

feat: refactor event and types to use classes with a mixin pattern. #31

Closed sam-goodwin closed 3 years ago

sam-goodwin commented 3 years ago

Closes #21 Closes #22

This change refactors Events and our use of super-struct's objects for Command request payloads.

new ScheduledFlightAdded({ payload: { flightNo, add: { day, scheduledArrival, scheduledDeparture, }, }, })


* 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,
        }),
      ]
    }
  },
);