bravent
Functional Event Sourcing
Last updated 4 years ago by vvgomes .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install bravent 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 0.1.0                                ...           latest (4 years ago)

10 Versions

  • 0.1.0                                ...           4 years ago
  • 0.0.9                                ...           4 years ago
  • 0.0.8                                ...           4 years ago
  • 0.0.7                                ...           4 years ago
  • 0.0.6                                ...           4 years ago
  • 0.0.5                                ...           4 years ago
  • 0.0.4                                ...           4 years ago
  • 0.0.3                                ...           4 years ago
  • 0.0.2                                ...           4 years ago
  • 0.0.1                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (9)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |