0.0.8 • Published 8 years ago

bravent v0.0.8

Weekly downloads
12
License
ISC
Repository
github
Last release
8 years ago

Bravent 🏂

build status npm version

Bravent is an Event Sourcing micro framework for Javascript. It is inspired by the Functional Data idea by Greg Young. The goal is to define the abstractions needed for an event sourced application in terms of pure functions and immutable data structures.

Getting Started

$ npm install bravent --save
import { defineAggregate } from "bravent";

const Counter = defineAggregate({
  initialState: 0,
  eventHandlers: {
    counterIncremented: (state, event) => state + 1,
    counterDecremented: (state, event) => state - 1
  },
  commandHandlers: {
    incrementCounter: (state, command) => [{ type: "counterIncremented" }],
    decrementCounter: (state, command) => [{ type: "counterDecremented" }]
  }
});

// Bravent doesn't impose how you store your events.
let events = [];

// An aggregate instance is a container, like a functor.
const counter = Counter.of(events);
console.log(counter.state()); // => 0

// Dispatching a command returns a new aggregate instance.
// There are no side effects on the original aggregate.
const newCounter =
  counter
    .dispatch({ type: "incrementCounter" })
    .dispatch({ type: "decrementCounter" })
    .dispatch({ type: "incrementCounter" })
    .dispatch({ type: "incrementCounter" });

console.log(newCounter.state()); // => 2

// You can pass callbacks when dispatching commands.
const onSuccess = (newEvents) => console.log("New events:", newEvents);
const onFailure = (errors) => console.log("Errors:", errors);

newCounter.dispatch({ type: "incrementCounter" }, onSuccess, onFailure);
// => New events: [{ type:"counterIncremented" }]

For a complete running example, see bravent-todo. More documentation coming soon.

License

Feel free to use this code as you please.

0.1.0

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago