• Public
  • Public/Protected
  • All

Stalagmite - v0.0.9



Stalagmite is a library that helps you building event sourced aggregates.



npm install stalagmite


Access documentation here with examples


In this example we will create a simple aggregate for a counter. Find the complete counter example here


import { buildAggregate, Aggregate, AggregateState } from 'stalagmite';

export interface Counter extends Aggregate<CounterState, CounterEvents> {
  init(counterId: string, initialCount: number): Outcome;
  count(number: number): Outcome;
  reset(): Outcome;

export interface CounterState extends AggregateState {
  id: string;
  count: number;

function counterEventResolver( // It's this function that mutate the state when applying an event.
  state: CounterState,
  event: CounterEvents,
): Outcome {

const commandId = 'command-id'; // The command you are building the aggregate for.

const aggregate = buildAggregate(commandId, initialState, eventResolver, {
  snapshotEvery: 2, // This options will generate snapshots every 2 events added

// Expose your business methods and expose aggregate methods.
return {
  init: () => {
  count: () => {
  reset: () => {

To see an eventResolver example click here

Aggregate interface:

// Aggregate interface
aggregate.apply(events: Event | Event[]): ApplyEventOutcome; // Apply an event to the aggregate.

aggregate.addEvent(events: E): Outcome; // Add and apply a new event to the aggregate.
aggregate.getUncommmitedEvents(): E[]; // Returns new events created not commited yet.
aggregate.eventsCommited(): void; // Call this function when you have saved those events in your event store. It clear the uncommited events array.

aggregate.getSequence(): number; // Returns aggregate sequence.
aggregate.state(): S; // Returns a deep copy of the current state.

aggregate.snapshot(): S; // Create an aggregate snapshot and return it.
aggregate.getSnapshot(): S[]; // Get all snapshots created.
aggregate.snapshotsCommited(): void; // Empty the list of snapshots.

For more details click here

Generated using TypeDoc