0.1.3 • Published 5 years ago

reventor v0.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Stupid-simple event sourcing.

REVENTOR

npm version GitHub license

An event-driven persistence for the application layer inspired by Redux.

Note: this currently provides an in-memory implementation, not suitable for production

createStore

const { createStore, adapters: { InMemory } } = require('reventor')

// Just like Redux, a reducer takes the previous state and a
// current event to produce the next state. This reducer will be used
// to map over a set of events for a given aggregate
function reducer (previousState = {}, event) {
  const { eventType, data } = event

  switch (eventType) {
  case 'acct:created':
    return { ...data, balance: 0 }
  case 'acct:deposit':
    return {
       ...previousState,
       balance: previousState.balance + data.amount
    }
  case 'acct:withdrawal':
    return {
       ...previousState,
       balance: previousState.balance - data.amount
    }
  }
  default:
    return previousState
  }
}

const adapter = new InMemory()
const { dispatch, getState } = createStore(adapter, reducer)

module.exports = { dispatch, getState }

dispatch

A function for dispatching events which takes a single event, or array of event objects.

dispatch({
  eventType: 'account:create'
  aggregateId: 'account:1234',
  aggregateVersion: 0,
  data: {
    owner: {
      email: 'owner@example.com',
      phone: {
        type: 'cell'
        number: '+15628675309'
      }
    }
    createdAt: Date.now(),
  }
})

getState

Get the state of an aggreaget.

getState('account:1234')

TODOs

  1. Add couchdb adpater
  2. Add subscribe / unsubscribe methods to store
  3. Add postgres adapter
  4. Implement snapshots
  5. More examples
  6. Monitoring / management UI
0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago