Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

stalagmite

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stalagmite

Event sourcing library that helps you building event sourced aggregates.

latest
Source
npmnpm
Version
0.0.10
Version published
Maintainers
1
Created
Source

Stalagmite

test

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

Installation:

https://nodei.co/npm/stalagmite.png?downloads=true&downloadRank=true&stars=true

npm install stalagmite

Documentation:

Access documentation here with examples

Example:

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

buildAggregate()

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: () => {
    /*...*/
  },
  ...aggregate,
};

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

Keywords

event-sourcing

FAQs

Package last updated on 05 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts