domaindriven: Batteries included event sourcing and CQRS

[ bsd3, library, web ] [ Propose Tags ]
Versions [RSS] 0.5.0
Change log ChangeLog.md
Dependencies aeson (>=2.0.3 && <2.2), async (>=2.2.4 && <2.3), base (>=4.7 && <5), bytestring (>=0.11.3 && <0.12), containers (>=0.6.5.1 && <0.7), deepseq (>=1.4.6.1 && <1.5), domaindriven-core (>=0.5.0 && <0.6), exceptions (>=0.10.4 && <0.11), generic-lens (>=2.2.1.0 && <2.3), http-types (>=0.12.3 && <0.13), microlens (>=0.4.12.0 && <0.5), mtl (>=2.2.2 && <2.3), openapi3 (>=3.2.2 && <3.3), postgresql-simple (>=0.6.4 && <0.7), random (>=1.2.1.1 && <1.3), servant-server (>=0.19.2 && <0.20), streamly (>=0.8.1.1 && <0.9), template-haskell (>=2.18.0.0 && <2.19), text (>=1.2.5.0 && <1.3), time (>=1.11.1.1 && <1.12), transformers (>=0.5.6.2 && <0.6), unliftio (>=0.2.0.1 && <0.3), unliftio-pool (>=0.2.2.0 && <0.3), unordered-containers (>=0.2.19.1 && <0.3), uuid (>=1.3.15 && <1.4), vector (>=0.12.3.1 && <0.13) [details]
License BSD-3-Clause
Copyright 2023 Tommy Engström
Author Tommy Engström
Maintainer tommy@tommyengstrom.com
Category Web
Home page https://github.com/tommyengstrom/domaindriven#readme
Bug tracker https://github.com/tommyengstrom/domaindriven/issues
Source repo head: git clone https://github.com/tommyengstrom/domaindriven
Uploaded by tommyengstrom at 2023-02-02T07:56:18Z
Distributions
Downloads 73 total (6 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2023-02-02 [all 1 reports]

Readme for domaindriven-0.5.0

[back to package description]

DomainDriven

DomainDriven is a batteries included synchronous event sourcing and CQRS library. The goal of this library is to allow you to implement DDD principles without focusing on the boilerplate.

It uses Template Haskell we generate a Servant server from the specification and we aim to keep the specification as succinct as we can.

The idea

  • Use a GADT to specify the actions, what will be translated into GETs and POSTs.
  • Make each event update run in a transaction, thereby avoiding the eventual consistency issues commonly associated with event sourcing.

How it works

In order to implement a model in domaindriven you have to define:

  • The model (current state)
  • The events
  • How to update the model when new events come in
  • The actions (queries and commands)
  • How to handle actions

Model

The model is the current state of the system. This is what you normally would keep in a database, but as this is an event sourced system the state is not fundamental as it can be recalculated.

Currently all implemented persistence strategies all keep the state in memory.

Events

Events are things that happened in the past. The event you define represent all the changes that can occur in the system.

Events should be specified in past tens.


data Event
    = IncreasedCounter
    | DecreasedCounter

Event handler

The model is calculated as a fold over the stream of events. As events happened in the past we can never refuse to handle them. This means the event handler is simply:

applyEvent :: Model -> Stored Event -> Model

where Stored is defined as:

data Stored a = Stored
    { storedEvent     :: a
    , storedTimestamp :: UTCTime
    , storedUUID      :: UUID
    }

Commands

Commands are defined using a GADT with one type parameter representing the return type. For example:

-- Same as: data StorageAction (x :: ParamPart) method a where
data StorageAction :: Action where
    GetFile
        :: P x "fileId" UUID 
        -> StorageAction x Query ByteString
    AddFile
        :: P x "fileContent" ByteString 
        -> StorageAction Cmd UUID
    RemoveFile 
        :: P x "fileId" UUID 
        -> StorageAction Cmd ()

Action handler

Actions, in contrast to events, are allowed to fail. If an action succeeds we need to return a value of the type specified by the constructor and, if it was a command, a list of events. The action handler do not update the state.

In addition you may need to make requests, read from disk, or perform other side effects in order to calculate the result.

ActionHandler is defined as:

type ActionHandler model event m c =
    forall method a. c 'ParamType method a -> HandlerType method model event m a

In practice this means you specify actions as


data CounterAction x method return where
   GetCounter ::CounterAction x Query Int
   IncreaseCounter ::CounterAction x Cmd Int
   DecreaseCounter ::CounterAction x Cmd Int

and the corresponding handler as

handleAction :: ActionHandler CounterAction CounterEvent IO a 
handleAction = \case
    GetCounter      -> Query $ pure -- Query is just `model -> IO a`
    IncreaseCounter -> Cmd $ \_ ->  `model -> IO (model -> a, [CounterEvent])`
        pure (id -- return state as is, after the event is applied
             , [CounterIncreased])
    DecreaseCounter -> Cmd $ \counter -> do
        when (counter < 1) (throwM NegativeNotSupported)
        pure (id, [CounterDecreased])

A Query takes a model -> m a, i.e. you get access to the model and the ability to run monadic efficts. Querys will be translates into GET in the generated API.

A Cmd has the additional ability of emitting events. It takes a model -> m (model -> a, [event]). The return value is specified as a function from the updated model to the return type. This way we can, in the Counter example, return the new value after the event handler has run.

Generating the server

Now we have defined the core parts of our service. We can now generate the server using the template-haskell function mkServer. It takes two arguments: The server config and the name of the GADT representing the actions. E.g. $(mkServer counterActionConfig ''CounterAction).

The ServerConfig, storeActionConfig in this example, contains the API options for the for the Action and all it's sub actions, as well as a all parameter names. This can be tenerated with $(mkServerConfig "counterActionConfig"), but due to TemplateHaskell's stage restrictions it cannot run in the same file as mkServer.

Simple example

Minimal example can be found in examples/simple/Main.hs, this uses the model defined in models/Models/Counter.hs