eventric
behavior-first application development
Last updated 3 years ago by efacilitation .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install eventric 
SYNC missed versions from official npm registry.

eventric logo

eventric.js Build Status

Minimalist JavaScript framework to build applications based on DDD, CQRS and Event Sourcing. Supports (micro)service based architectures and focuses on high testability.

eventric is written in CoffeeScript. If you need a JavaScript tutorial please compile the snippets below yourself.

Current road map

Currently there is an event store implementation for MongoDB which will not work correctly in a multi process scenario. We will soon be working on an event store implementation for http://geteventstore.com which will get rid of this limitation.

Implementations for other databases are currently not planned.

Tutorial

This tutorial will guide you through all features of eventric by implementing a simplified Todo application.

There is no API documentation. If you want to dig deeper, have a look at the source code and the specs.

Installation

Install the framework inside your application with npm install eventric.

eventric requires Promises. If you have to support older browsers install and use a polyfill, such as es6-promise or rsvp.js.

Context

First create a Todo context inside your application.

todoContext = eventric.context 'Todo'

Contexts create architectural boundaries inside an eventric based application. They can be compared to (micro)services and somewhat also to bounded contexts from an implementation perspective.

A context holds its own event store and provides a self contained space for domain events, aggregates, command and query handlers, projections and an event publishing infrastructure (which may be outsourced soon).

Domain events

After creating the context the necessary domain events can be defined.

todoContext.defineDomainEvents
  TodoCreated: ({title}) ->
    @title = title

  TodoTitleChanged: ({title}) ->
    @title = title

  TodoFinished: ->

Domain event definitions in eventric consist of two parts: The domain event name and the payload constructor function. This definition is similar to a class used in statically typed languages such as Java or C#. Inside the payload constructor function the expected values must be assigned to members of this.

Note: It is best practice to use the same name for parameters and assigned member variables.

Aggregates

Domain events in eventric can only be emitted from inside aggregate instances. An aggregate is defined through a plain class which must at least implement a create() function. This function will be automatically called when creating a new aggregate from inside a command handler.

Use the injected function $emitDomainEvent(eventName, eventPayload) to emit a new domain event. This will cause two things to happen:

  • The new domain event is saved in the list of new domain events
  • If existing, the correct handle function on the aggregate is executed (handle<event name>())

The above mentioned handle functions are also used when loading and rebuilding an aggregate to create the current state.

todoContext.addAggregate 'Todo', class Todo

  create: ({title}) ->
    if not title
      throw new Error 'title missing'
    @$emitDomainEvent 'TodoCreated',
      title: title


  changeTitle: ({title}) ->
    if not title
      throw new Error 'title missing'
    if @isFinished
      throw new Error 'todo already finished'

    @$emitDomainEvent 'TodoTitleChanged',
      title: title


  finish: ->
    @$emitDomainEvent 'TodoFinished'


  handleTodoFinished: ->
    @isFinished = true


Note: Aggregate functions (except for handle functions) can return promises. eventric will wait for them to resolve.

Command handlers

Command handlers define the write side of the application service layer inside a context.

Definition

Command handlers are registered by passing an object to the context where the keys define the command names.

todoContext.addCommandHandlers

  CreateTodo: ({title}) ->
    @$aggregate.create 'Todo',
      title: title
    .then (todo) ->
      todo.$save()


  ChangeTodoTitle: ({todoId, title}) ->
    @$aggregate.load 'Todo', todoId
    .then (todo) ->
      todo.changeTitle title: title
      todo.$save()


  FinishTodo: ({todoId}) ->
    @$aggregate.load 'Todo', todoId
    .then (todo) ->
      todo.finish()
      todo.$save()


Use the injected service $aggregate to create, load, modify and save aggregate instances. Creating an aggregate will cause the create() function defined on the aggregate class to be called. Execute the injected function $save() to save new domain events and publish them via the event bus.

Although discouraged, queries can be executed by using the injected service $query.

Execution

After defining the command handlers they can be executed from outside the context. In order to work with a context it needs to be initialized. The initialization is mainly required for projections.

todoContext.initialize()
.then ->
  todoContext.command 'CreateTodo', title: 'My first todo'
.then (todoId) ->
  todoContext.command 'ChangeTodoTitle',
    todoId: todoId
    title: 'My first changed todo'
  .then ->
    todoContext.command 'FinishTodo',
      todoId: todoId
.then ->
  console.log 'todo created, changed and finished'

Domain event handler

Domain event handlers can be registered for specific events and even for specific aggregate instances.

todoContext.subscribeToDomainEvent 'TodoFinished', (domainEvent) ->
  console.log 'finished todo', domainEvent.aggregate.id

todoContext.subscribeToDomainEventWithAggregateId 'TodoTitleChanged', 'some aggregate id', (domainEvent) ->
  console.log 'change title to: ', domainEvent.payload.title

Projections

Projections always replay an event stream from the beginning. They are used to create or populate read models.

todosReadModel = {}

todosProjection =

  initialize: (params, done) ->
    done()


  handleTodoCreated: (domainEvent) ->
    todosReadModel[domainEvent.aggregate.id] =
      title: domainEvent.payload.title


  handleTodoTitleChanged: (domainEvent) ->
    todosReadModel[domainEvent.aggregate.id].title = domainEvent.payload.title


  handleTodoFinished: (domainEvent) ->
    todosReadModel[domainEvent.aggregate.id].isFinished = true


todoContext.addProjection todosProjection


todoCountReadModel = 0

todoCountProjection =

  initialize: (params, done) ->
    done()


  handleTodoCreated: (domainEvent) ->
    todoCountReadModel++


todoContext.addProjection todoCountProjection

Important: Projections must be added to a context before it is initialized.

Query handlers

Query handlers define the read side of the application service layer inside an eventric context.

Definition

Query handlers are registered the same way command handlers are by passing an object to the context.

todoContext.addQueryHandlers

  getTodoList: (params) ->
    return todosReadModel


  getTodoCount: (params) ->
    return todoCountReadModel

Execution

Similar to command handlers queries can be executed from outside the context after defining them.

todoContext.initialize()
.then ->
  todoContext.command 'CreateTodo', title: 'My first todo'
.then (todoId) ->
  todoContext.command 'ChangeTodoTitle',
    todoId: todoId
    title: 'My first changed todo'
  .then ->
    todoContext.command 'FinishTodo',
      todoId: todoId
.then ->
  todoContext.query 'getTodoList', {}
.then (todoList) ->
  console.log 'current todos:', todoList
  todoContext.query 'getTodoCount'
.then (todoCount) ->
  console.log 'current todo count:', todoCount

Stores

The event store inside a context is responsible for saving domain events and searching them by aggregate id or event name.

In memory

By default eventric uses an in memory event store which is mainly useful for demo applications and testing purposes.

MongoDB

For actual applications use the mongodb event store to save domain events in a persistent way. First, install it together with the mongodb module.

npm install mongodb npm install eventric-store-mongodb

Then, before initializing any contexts, connect to the database and set the mongodb event store as eventric store.

mongodb = require 'mongodb'
EventricMongoDBStore = require 'eventric-store-mongodb'

mongodb.MongoClient.connect 'your db url', (error, database) ->
  eventric.setStore EventricMongoDBStore, dbInstance: database

  # initialize todo context

Persistent read models

An event store inside a context only handles domain event persistence (write side of the application). Saving persistent read models (or views) are not scope of the eventric framework.

To illustrate how this can be done the above todo list projection is rewritten to use mongodb as read model store.

todoListProjection =

  initialize: (params, done) ->
    database.dropCollection 'todos'
    .then ->
      database.collection 'todos'
    .then (collection) ->
      @collection = collection
      done()


  handleTodoCreated: (domainEvent) ->
    @collection.insert
      id: domainEvent.aggregate.id
      title: domainEvent.payload.title


  handleTodoTitleChanged: (domainEvent) ->
    @collection.update id: domainEvent.aggregate.id,
      $set: title: domainEvent.payload.title


  handleTodoFinished: (domainEvent) ->
    @collection.update id: domainEvent.aggregate.id,
      $set: isFinished: true


Note: The above will cause the read model to be emptied whenever the process is restarted. Consider this a best practice.

The query handler can be changed accordingly to directly access the mongodb collection.

todoContext.addQueryHandlers

  getTodos: ->
    database.collection 'todos'
    .then (collection) ->
      collection.find({}).toArray()


Remotes

eventric supports service oriented architectures. Consider every context to be a possible standalone (micro)service. All contexts share the same API: commands, queries, domain event handlers and projections.

Use the Remote interface in order to communicate between contexts.

In memory

By default eventric provides an in memory remote which is useful for in-process communication and testing purposes.

todoContext = eventric.remote 'Todo'
todoContext.command 'CreateTodo'
.then (todoId) ->
  console.log todoId

Socket.IO

All previous examples were meant to be executed in a single process on the server side of an application. In order to communicate with a context running on a server from a browser use the Socket.IO remote implementations.

First, install the modules together with Socket.IO.

npm install socket.io
npm install eventric-remote-socketio-endpoint
npm install eventric-remote-socketio-client

Then, configure eventric on the server side to use Socket.IO as additional remote endpoint.

socketIO = require 'socket.io'
socketIORemoteEndpoint  = require 'eventric-remote-socketio-endpoint'

io = socketIO.listen 1234
socketIORemoteEndpointOptions =
  ioInstance: io
socketIORemoteEndpoint.initialize socketIORemoteEndpointOptions, ->
  eventric.addRemoteEndpoint socketIORemoteEndpoint

Finally, include the Socket.IO client and eventric in an html file, configure the remote client and create a remote.

<!DOCTYPE html>
<html>
  <head>
    <title>eventric tutorial</title>
  </head>
  <body>
    <!-- fix paths to files -->
    <script type="text/javascript" src="node_modules/socket.io/node_modules/socket.io-client/socket.io.js" ></script>
    <script type="text/javascript" src="node_modules/eventric/dist/release/eventric.js" ></script>
    <script type="text/javascript" src="node_modules/eventric-remote-socketio-client/dist/release/eventric_remote_socketio_client.js" ></script>

    <script type="text/javascript">
      var socket = io.connect('http://localhost:1234');
      socketIORemoteClient = window['eventric-remote-socketio-client'];
      socketIORemoteClient.initialize({
        ioClientInstance: socket
      })
      .then(function() {
        var todoContext = eventric.remote('Todo');
        todoContext.setClient(socketIORemoteClient);

        todoContext.command('CreateTodo', {title: 'My first todo'})
        .then(function(todoId) {
          console.log('Todo created: ', todoId);
        })
        .catch(function(error) {
          console.error('Error creating todo: ', error);
        });
      });
    </script>
  </body>
</html>

Note: Socket.IO remotes are not limited to browser to server. They can easily be used for server to server communication.

Remote projections

One major strength of eventric is the possibility to create remote projections (or client side projections). This feature makes it easily possible to create reactive user interfaces in the browser.

todoContext = eventric.remote 'Todo'
todoContext.setClient socketIORemoteClient
todoProjection =

  initialize: (params, done) ->
    document.querySelector('body').innerHTML = '<h1>Todos</h1><div class="todos"></div>';
    done()


  handleTodoCreated: (domainEvent) ->
    todoElement = document.createElement 'div'
    todoElement.setAttribute 'id', domainEvent.aggregate.id
    todoElement.innerHTML = domainEvent.payload.title
    document.querySelector('.todos').appendChild todoElement


  handleTodoTitleChanged: (domainEvent) ->
    document.querySelector("[id='#{domainEvent.aggregate.id}']").innerHTML = domainEvent.payload.title


  handleTodoFinished: (domainEvent) ->
    document.querySelector("[id='#{domainEvent.aggregate.id}']").setAttribute 'style', 'text-decoration: line-through'


todoContext.initializeProjection todoProjection, {}

Note: initializeProjection() may be renamed to addProjection() in order to stay consistent with the context API.

License

MIT

Copyright (c) 2013-2016 SixSteps Team, efa GmbH

Current Tags

  • 0.26.0                                ...           latest (3 years ago)

178 Versions

  • 0.26.0                                ...           3 years ago
  • 0.25.6                                ...           3 years ago
  • 0.25.5                                ...           4 years ago
  • 0.25.4                                ...           4 years ago
  • 0.25.3                                ...           4 years ago
  • 0.25.2                                ...           4 years ago
  • 0.25.1                                ...           4 years ago
  • 0.25.0                                ...           4 years ago
  • 0.24.1                                ...           4 years ago
  • 0.24.0                                ...           4 years ago
  • 0.23.0                                ...           4 years ago
  • 0.22.0                                ...           4 years ago
  • 0.21.6                                ...           5 years ago
  • 0.21.5                                ...           5 years ago
  • 0.21.4                                ...           5 years ago
  • 0.21.2                                ...           5 years ago
  • 0.21.1                                ...           5 years ago
  • 0.21.0                                ...           5 years ago
  • 0.20.1                                ...           5 years ago
  • 0.20.0                                ...           5 years ago
  • 0.19.0                                ...           5 years ago
  • 0.18.0                                ...           5 years ago
  • 0.17.1                                ...           5 years ago
  • 0.17.0                                ...           5 years ago
  • 0.16.0                                ...           5 years ago
  • 0.15.0                                ...           5 years ago
  • 0.14.0                                ...           5 years ago
  • 0.13.1                                ...           5 years ago
  • 0.13.0                                ...           5 years ago
  • 0.12.0                                ...           5 years ago
  • 0.11.3                                ...           5 years ago
  • 0.11.2                                ...           5 years ago
  • 0.11.1                                ...           5 years ago
  • 0.11.0                                ...           5 years ago
  • 0.10.5                                ...           5 years ago
  • 0.10.4                                ...           5 years ago
  • 0.10.3                                ...           5 years ago
  • 0.10.2                                ...           5 years ago
  • 0.10.1                                ...           5 years ago
  • 0.10.0                                ...           5 years ago
  • 0.9.3                                ...           5 years ago
  • 0.9.2                                ...           5 years ago
  • 0.9.1                                ...           5 years ago
  • 0.9.0                                ...           5 years ago
  • 0.8.6                                ...           5 years ago
  • 0.8.5                                ...           5 years ago
  • 0.8.4                                ...           5 years ago
  • 0.8.3                                ...           5 years ago
  • 0.8.2                                ...           5 years ago
  • 0.8.0                                ...           5 years ago
  • 0.7.2                                ...           5 years ago
  • 0.7.1                                ...           5 years ago
  • 0.7.0                                ...           5 years ago
  • 0.6.0                                ...           5 years ago
  • 0.5.2                                ...           5 years ago
  • 0.4.1                                ...           5 years ago
  • 0.4.0                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.24                                ...           5 years ago
  • 0.1.23                                ...           5 years ago
  • 0.1.22                                ...           5 years ago
  • 0.1.21                                ...           5 years ago
  • 0.1.20                                ...           5 years ago
  • 0.1.19                                ...           5 years ago
  • 0.1.18                                ...           5 years ago
  • 0.1.17                                ...           5 years ago
  • 0.1.16                                ...           5 years ago
  • 0.1.15                                ...           5 years ago
  • 0.1.14                                ...           5 years ago
  • 0.1.13                                ...           6 years ago
  • 0.1.12                                ...           6 years ago
  • 0.1.11                                ...           6 years ago
  • 0.1.10                                ...           6 years ago
  • 0.1.9                                ...           6 years ago
  • 0.1.8                                ...           6 years ago
  • 0.1.7                                ...           6 years ago
  • 0.1.6                                ...           6 years ago
  • 0.1.5                                ...           6 years ago
  • 0.1.4                                ...           6 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.2                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.105                                ...           6 years ago
  • 0.0.104                                ...           6 years ago
  • 0.0.103                                ...           6 years ago
  • 0.0.102                                ...           6 years ago
  • 0.0.101                                ...           6 years ago
  • 0.0.99                                ...           6 years ago
  • 0.0.98                                ...           6 years ago
  • 0.0.97                                ...           6 years ago
  • 0.0.96                                ...           6 years ago
  • 0.0.95                                ...           6 years ago
  • 0.0.94                                ...           6 years ago
  • 0.0.93                                ...           6 years ago
  • 0.0.92                                ...           6 years ago
  • 0.0.91                                ...           6 years ago
  • 0.0.90                                ...           6 years ago
  • 0.0.88                                ...           6 years ago
  • 0.0.87                                ...           6 years ago
  • 0.0.86                                ...           6 years ago
  • 0.0.85                                ...           6 years ago
  • 0.0.84                                ...           6 years ago
  • 0.0.83                                ...           6 years ago
  • 0.0.82                                ...           6 years ago
  • 0.0.81                                ...           6 years ago
  • 0.0.80                                ...           6 years ago
  • 0.0.79                                ...           6 years ago
  • 0.0.78                                ...           6 years ago
  • 0.0.77                                ...           6 years ago
  • 0.0.76                                ...           6 years ago
  • 0.0.75                                ...           6 years ago
  • 0.0.73                                ...           6 years ago
  • 0.0.72                                ...           6 years ago
  • 0.0.71                                ...           6 years ago
  • 0.0.70                                ...           6 years ago
  • 0.0.69                                ...           6 years ago
  • 0.0.68                                ...           6 years ago
  • 0.0.67                                ...           6 years ago
  • 0.0.66                                ...           6 years ago
  • 0.0.65                                ...           6 years ago
  • 0.0.64                                ...           6 years ago
  • 0.0.62                                ...           6 years ago
  • 0.0.61                                ...           6 years ago
  • 0.0.60                                ...           6 years ago
  • 0.0.58                                ...           6 years ago
  • 0.0.57                                ...           6 years ago
  • 0.0.56                                ...           6 years ago
  • 0.0.55                                ...           6 years ago
  • 0.0.54                                ...           6 years ago
  • 0.0.53                                ...           6 years ago
  • 0.0.52                                ...           6 years ago
  • 0.0.51                                ...           6 years ago
  • 0.0.50                                ...           6 years ago
  • 0.0.49                                ...           6 years ago
  • 0.0.48                                ...           6 years ago
  • 0.0.46                                ...           6 years ago
  • 0.0.45                                ...           6 years ago
  • 0.0.44                                ...           6 years ago
  • 0.0.43                                ...           6 years ago
  • 0.0.42                                ...           6 years ago
  • 0.0.41                                ...           6 years ago
  • 0.0.40                                ...           6 years ago
  • 0.0.35                                ...           6 years ago
  • 0.0.34                                ...           6 years ago
  • 0.0.33                                ...           6 years ago
  • 0.0.32                                ...           6 years ago
  • 0.0.31                                ...           6 years ago
  • 0.0.30                                ...           6 years ago
  • 0.0.29                                ...           6 years ago
  • 0.0.28                                ...           6 years ago
  • 0.0.27                                ...           6 years ago
  • 0.0.26                                ...           6 years ago
  • 0.0.25                                ...           6 years ago
  • 0.0.24                                ...           6 years ago
  • 0.0.23                                ...           6 years ago
  • 0.0.22                                ...           6 years ago
  • 0.0.21                                ...           6 years ago
  • 0.0.20                                ...           6 years ago
  • 0.0.19                                ...           6 years ago
  • 0.0.18                                ...           6 years ago
  • 0.0.17                                ...           6 years ago
  • 0.0.16                                ...           6 years ago
  • 0.0.15                                ...           6 years ago
  • 0.0.13                                ...           6 years ago
  • 0.0.12                                ...           6 years ago
  • 0.0.11                                ...           6 years ago
  • 0.0.10                                ...           6 years ago
  • 0.0.9                                ...           6 years ago
  • 0.0.8                                ...           6 years ago
  • 0.0.7                                ...           6 years ago
  • 0.0.6                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
  • 0.0.0                                ...           6 years ago
Downloads
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 3
Last Month 9
Dependencies (2)
Dev Dependencies (18)

Copyright 2014 - 2016 © taobao.org |