Options
All
  • Public
  • Public/Protected
  • All
Menu

Stalagmite - v0.0.9

Index

Aggregate Interfaces

Commands Interfaces

Events Interfaces

Other Interfaces

Outcome Interfaces

Events Type aliases

Other Type aliases

Outcome Type aliases

Other Functions

Utility Functions

Events Type aliases

EventResolver

EventResolver<S, E>: (state: S, event: E) => Outcome

This is the function you aggregate will use to mutate your state. If you know redux it's like a reducer.

example
function eventResolver(state: CounterState, event: CounterEvents): Outcome {
  switch (event.name) {
    case "CounterInitiated":
      state.id = event.payload.counterId;
      state.count = event.payload.count;
      break;

    case "NumberCounted":
      state.count += event.payload.number;
      break;

    case "CounterReseted":
      state.count = event.payload.count;
      break;

    default:
      const { name: eventName } = event;
      missingSwitchCaseHandling(eventName);
  }

  return {
    outcome: "SUCCESS",
    data: {},
  };
}

Type parameters

Type declaration

    • Parameters

      • state: S
      • event: E

      Returns Outcome

Other Type aliases

ApplyEventOutcome

Outcome Type aliases

Outcome

It represent an operation result.

Other Functions

buildAggregate

  • buildAggregate<S, E>(commandId: string, state: S, resolver: EventResolver<S, E>, options?: { snapshotEvery?: number }): Aggregate<S, E>
  • Use this function to build your aggregate instances.

    example
    export function createCounter(
      commandId: string,
      state?: CounterState
    ): Counter {
      const initialState: CounterState = {
        id: "none",
        count: 0,
        sequence: 0,
        commandId: commandId,
      };
    
      if (state) {
        state.commandId = commandId;
      }
    
      const currentState = state || initialState;
    
      const aggregate = buildAggregate(currentState, counterEventResolver);
    
      return {
        init: buildInit(aggregate),
        count: buildCount(aggregate),
        reset: buildReset(aggregate),
        ...aggregate,
      };
    }
    

    Type parameters

    Parameters

    • commandId: string
    • state: S

      The current state of you aggregate

    • resolver: EventResolver<S, E>

      The EventResolver your aggregate will use to mutate the state.

    • Optional options: { snapshotEvery?: number }
      • Optional snapshotEvery?: number

        This option will generate a snapshots every X events added.

    Returns Aggregate<S, E>

    This function will return an new aggregate instance.

Utility Functions

missingSwitchCaseHandling

  • missingSwitchCaseHandling(x: never): never
  • This function will help you be certain that you handle all cases in switch case. If one case is not covered typescript will raise an error at compilation time.

    example
     if (result.outcome === "FAILURE") {
       switch (result.errorCode) {
         case "ERROR_1":
           return;
    
         case "ERROR_2":
           return;
    
         default:
    
           // If you haven't covered all cases this line will generate a Typescript error
           // that tells you which case you've missed
           missingSwitchCaseHandling(result.errorCode);
       }
     }
    

    Parameters

    • x: never

    Returns never

Generated using TypeDoc