Skip to content

This is an ongoing exploration of CQRS, Event Sourcing, Pub/Sub and Dependency Injection in the context of ES6/7 and a variation of S.O.L.I.D. principles applied to functions rather than classes.

Notifications You must be signed in to change notification settings

jonatanpedersen/fun-with-events

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fun-with-events

This is an ongoing exploration of CQRS, Event Sourcing, Pub/Sub and Dependency Injection in the context of ES6/7 and a variation of S.O.L.I.D. principles applied to functions rather than classes.

Setup

Clone from GitHub:

git clone [email protected]:jonatanpedersen/fun-with-events.git

Install dependencies:

npm install

Usage

Start the services:

sudo npm start

Open the user interface https://localhost

Note: You need RabbitMQ and MongoDB. The application connects to amqp://guest:guest@localhost:5672 and mongodb://localhost:27017/test by default.

Responsibilities

write api

  • Listens for commands and dispatches them to handlers that read and append events from and to event streams. The event streams persists events to storage and publishes events to a message queue

read api

  • Listens for events and dispatches them to handlers that update view models
  • Listens for queries and dispatches them to handlers that serve view models

ui

  • Serve static html files
  • Send commands to the write api
  • Send queries to the read api

command

Something that describes a desired mutation of a domain object. Consists of a name and some parameters.

{
  name: 'createTask',
  params: {
    taskId: 1,
    description: 'Get ducks in a row'
  }
}

Actions are always named with a verb followed by the name of the domain object. e.g. createTask or acceptOrder .

event

Something that tell us that a domain object has mutated. Consists of a name and some data describing the mutation.

{
  name: 'taskCreated',
  data: {
    taskId: 1,
    description: 'Get ducks in a row'
  }
}

query

Something that describes a view on the read side. Contains a name and optional parameters.

{
  name: 'tasks',
  params: {
    order: 1
  }
}

dispatch

Dispatches a command, event or query to the appropriate handle function.

async function dispatch(thing) {
}

handle

Handles a command by mutating a domain object on the write side. Handles an event by updating a view on the read side. Handles a query by returning a view from the read side.

async function handle(thing) {
}

validate

Validates a command, event or query and throws an error if necessary.

async function validate(thing) {
}

composition root

The applications main() function is the composition root, and it is composed primarily of functions.

dependency injection

All functions should have their dependencies injected during creation.

function create(dependency1, dependency2) {
  return async function (arg1, arg2) {
      // call dependency1
      // call dependency2
  }
}

Licence

The MIT License (MIT)

Copyright (c) 2015 Jonatan Pedersen https://www.jonatanpedersen.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

This is an ongoing exploration of CQRS, Event Sourcing, Pub/Sub and Dependency Injection in the context of ES6/7 and a variation of S.O.L.I.D. principles applied to functions rather than classes.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published