Code Monkey home page Code Monkey logo

event_sourcery_todo_app's Introduction

Event Sourcery Todo Example App

Deploy

An example event sourced/CQRS web application built using EventSourcery and its Postgres event store implementation.

This application is intended to illustrate concepts in EventSourcery, how they relate to each other, and how to use them in practice.

Get started

Ensure you have Postgres and Ruby 2.2 or higher installed.

First you need to install the correct ruby version to work with:

$ rbenv install

Then make sure you have postgresql running in the background:

$ brew services restart postgresql

Then run the setup script.

$ ./scripts/setup

Run the tests.

$ bundle exec rake

Using the Application

Start the web app and event stream processors via Foreman.

$ foreman start

Then you can manage your Todos using the request CLI script.

# Add a todo
$ ./scripts/request add -i aac35923-39b4-4c39-ad5d-f79d67bb2fb2 -t "Get to the chopper" -d "It's in the trees" -s [email protected] -D 2017-01-01

# Amend
$ ./scripts/request amend -i aac35923-39b4-4c39-ad5d-f79d67bb2fb2 -t "Get to the chopper, NOW"

# Complete
$ ./scripts/request complete -i aac35923-39b4-4c39-ad5d-f79d67bb2fb2 -D 2017-01-01

# Abandon
$ ./scripts/request abandon -i aac35923-39b4-4c39-ad5d-f79d67bb2fb2 -D 2017-01-01

# List
$ ./scripts/request list -l outstanding
$ ./scripts/request list -l scheduled
$ ./scripts/request list -l completed

Application Structure

├── app
│   ├── aggregates
│   │   └── todo.rb
│   ├── commands
│   │   └── todo
│   │       ├── abandon.rb
│   │       ├── add.rb
│   │       ├── amend.rb
│   │       └── complete.rb
│   ├── errors.rb
│   ├── events
│   │   ├── stakeholder_notified_of_todo_completion.rb
│   │   ├── todo_abandoned.rb
│   │   ├── todo_added.rb
│   │   ├── todo_amended.rb
│   │   └── todo_completed.rb
│   ├── projections
│   │   ├── completed_todos
│   │   │   ├── projector.rb
│   │   │   └── query.rb
│   │   ├── outstanding_todos
│   │   │   ├── projector.rb
│   │   │   └── query.rb
│   │   └── scheduled_todos
│   │       ├── projector.rb
│   │       └── query.rb
│   ├── reactors
│   │   └── todo_completed_notifier.rb
│   ├── utils.rb
│   └── web
│       └── server.rb
├── config
│   └── environment.rb

Events

These are our domain events. They are stored in our event store as a list of immutable facts over time. Together they form the source of truth for our application's state.

  • TodoAdded
  • TodoCompleted
  • TodoAbandoned
  • TodoAmended
  • StakeholderNotifiedOfTodoCompletion

A Todo can have the following attributes:

  • title
  • description
  • due_date
  • stakeholder_email

Commands

The set of command handlers and commands that can be issued against the system. These form an interface between the web API and the domain model in the aggregate.

Aggregates

The domain is modeled via aggregates. In this application we only have one aggregate root: Todo. It loads its state from the event store (via the repository), executes commands, and raises new events which are saved back to the store (again via the repository).

Projections

You can think of projections as read-only models. They are created and updated by projectors and in this case show different current state views over the events that are the source of truth for our application state.

  • OutstandingTodos
  • CompletedTodos
  • ScheduledTodos (has due date)

Reactors

Reactors listen for events and take some action. Often these actions will involve emitting other events into the store. Sometimes it may involve triggering side effects in external systems.

Reactors can be used to build process managers or sagas.

  • TodoCompletedNotifier
    • "sends" an email notifying stakeholders of todo completion.
    • Emits StakeholderNotifiedOfTodoCompletion event to record this fact.

Data flow of an "Add Todo" Request

Below we see the flow of data of an "add todo" request. Note that arrows indicate data flow.

Note that stage 1 and 2 are not synchronous. This means EventSourcery applications need to embrace eventual consistency.

Also note that we are only showing one projection below. The other projectors and reactors will also update their projections based on the TodoAdded event.


         1. Add Todo      │       2. Update Outstanding   │     3. Issue Outstanding
                                    Todos Projection                Todos Query
               │          │                               │
               ▼                                                          ▲
       ┌───────────────┐  │                               │               │
       │Command Handler│                                                  │
       └───────────────┘  │                               │          F. Handle
               │                                                       Query
     B. Call add todo on  │                               │               │
          aggregate                                                       │
               │          │                               │               │
               ▼                       ┌─────────────┐                    │
        ┌─────────────┐   │            │ Outstanding │    │        ┌─────────────┐
        │             │       ┌───────▶│    Todos    │             │             │
     ┌─▶│  Aggregate  │   │   │        │  Projector  │    │        │Query Handler│
     │  │             │       │        └─────────────┘             │             │
     │  └─────────────┘   │  D. Read          │           │        └─────────────┘
     │         │              event      E. Update                        ▲
 A. Load  C. Save new     │   │          Projection       │               │
state from   event            │               │                        G. Read
  events       │          │   │               │           │       Outstanding Todos
     │         ▼              │               ▼                      Projection
     │  ┌─────────────┐   │   │        ┌─────────────┐    │               │
     │  │             │       │        │ Outstanding │                    │
     └──│ Event Store │───┼───┘        │    Todos    │    │               │
        │             │                │  Database   │────────────────────┘
        └─────────────┘   │            │    Table    │    │
                                       └─────────────┘
                          │                               │

Routes

The application exposes a web UI with the following API.

GET /todos/outstanding
GET /todos/completed
GET /todos/scheduled
POST /todo/:id (add)
PUT /todo/:id (amend)
POST /todo/:id/complete
POST /todo/:id/abandon

event_sourcery_todo_app's People

Contributors

2potatocakes avatar dependabot[bot] avatar grassdog avatar hayleycodes avatar johnsyweb avatar orien avatar pablolee avatar salamagd avatar stevehodgkiss avatar twe4ked avatar usamasaddiq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.