Not production ready. API might change heavily.

eventric.js 
Behavior-first application development. Runs on NodeJS and modern Browsers.
Note: eventric is currently under heavy development. The readme is outdated. It will be updated soon.
Philosophy
Getting started
npm install eventric
Important: eventric requires Promises.
If you have to support older browsers use the es6-promise polyfill.
Setup Context
Having discussed the upcoming TodoApp Project with the Business-Experts and fellow Developers it got clear that we should start with a Context
named Todo
.
eventric = require('eventric');
todoContext = eventric.context('Todo');
Define the Event
Inside of our Todo
Context things will happen which are called DomainEvents. A technique to come up with these is called EventStorming. Lets add two called TodoCreated
and TodoDescriptionChanged
.
todoContext.defineDomainEvents({
TodoCreated: function(params) {},
TodoDescriptionChanged: function(params) {
this.description = params.description;
}
});
Adding an Aggregate
Now we need an Aggregate which actually raises this DomainEvents.
todoContext.addAggregate('Todo', function() {
this.create = function() {
this.$emitDomainEvent('TodoCreated');
}
this.changeDescription = function(description) {
this.$emitDomainEvent('TodoDescriptionChanged', {description: description});
}
});
Hint: this.create
is called by convention when you create an aggregate using this.$aggregate.create
Hint: this.$emitDomainEvent
is dependency injected
Adding CommandHandlers
To actually work with the Context
from the outside world we need CommandHandlers
. Let's start by adding a simple one that will create an instance of our Todo
Aggregate.
todoContext.addCommandHandlers({
CreateTodo: function(params) {
return this.$aggregate.create('Todo')
.then(function (todo) {
return todo.$save();
});
}
});
Hint: this.$aggregate
is dependency injected
It would be nice if we could change the description of the Todo
, so let's add this CommandHandler
too.
todoContext.addCommandHandlers({
ChangeTodoDescription: function(params) {
return this.$aggregate.load('Todo', params.id)
.then(function (todo) {
todo.changeDescription(params.description);
return todo.$save();
});
}
});
Subscribe to a DomainEvent
And last but not least we want to console.log when the description of the Todo
changes.
todoContext.subscribeToDomainEvent('TodoDescriptionChanged', function(domainEvent) {
console.log(domainEvent.payload.description);
});
Executing Commands
Initialize the Context, create a Todo
and tell the Todo
to change its description.
todoContext.initialize()
.then(function() {
return todoContext.command('CreateTodo');
})
.then(function(todoId) {
return todoContext.command('ChangeTodoDescription', {
id: todoId,
description: 'Do something'
});
});
After executing the Commands the DomainEventHandler will print Do something
. Your Todo
Aggregate is now persisted using EventSourcing into the InMemory Store
.
License
MIT
Copyright (c) 2013-2015 SixSteps Team, eFa GmbH