https://github.com/sachacr/stalagmite
Stalagmite is a library that helps you building event sourced aggregates.
https://github.com/sachacr/stalagmite
cqrs cqrs-es event-sourcing event-sourcing-and-cqrs
Last synced: 7 months ago
JSON representation
Stalagmite is a library that helps you building event sourced aggregates.
- Host: GitHub
- URL: https://github.com/sachacr/stalagmite
- Owner: SachaCR
- Created: 2021-04-23T21:15:32.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-07-06T07:18:40.000Z (over 4 years ago)
- Last Synced: 2024-05-17T00:39:13.816Z (over 1 year ago)
- Topics: cqrs, cqrs-es, event-sourcing, event-sourcing-and-cqrs
- Language: TypeScript
- Homepage: https://sachacr.github.io/stalagmite/modules.html
- Size: 673 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Stalagmite

Stalagmite is a library that helps you building event sourced aggregates.
# Installation:
[](https://www.npmjs.com/package/stalagmite)
`npm install stalagmite`
# Documentation:
[Access documentation here with examples](https://sachacr.github.io/stalagmite/modules.html)
# Example:
In this example we will create a simple aggregate for a counter. Find the complete [counter example here](https://github.com/SachaCR/stalagmite/tree/main/src/examples/counter)
# buildAggregate()
```typescript
import { buildAggregate, Aggregate, AggregateState } from 'stalagmite';export interface Counter extends Aggregate {
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](https://github.com/SachaCR/stalagmite/blob/main/src/examples/counter/events.ts#L85)
## Aggregate interface:
```typescript
// 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 ](https://sachacr.github.io/stalagmite/modules.html)