module Data.CQRS.Types.EventStore
( EventStore(..)
, StoreError(..)
, applyIso
) where
import Control.Monad ((>=>))
import Data.CQRS.Types.PersistedEvent
import Data.CQRS.Types.StoreError
import Data.UUID.Types (UUID)
import System.IO.Streams (InputStream)
import qualified System.IO.Streams.Combinators as SC
data EventStore e = EventStore {
esStoreEvents :: UUID -> [PersistedEvent e] -> IO ()
,
esRetrieveEvents :: forall a . UUID -> Int -> (InputStream (PersistedEvent e) -> IO a) -> IO a
,
esRetrieveAllEvents :: forall a . (InputStream (UUID, PersistedEvent e) -> IO a) -> IO a
}
applyIso :: forall e' e . (e' -> e, e -> e') -> EventStore e -> EventStore e'
applyIso (f, g) (EventStore storeEvents' retrieveEvents' retrieveAllEvents') =
EventStore storeEvents retrieveEvents retrieveAllEvents
where
storeEvents :: UUID -> [PersistedEvent e'] -> IO ()
storeEvents aggregateId = storeEvents' aggregateId . map (fmap f)
retrieveEvents :: forall a . UUID -> Int -> (InputStream (PersistedEvent e') -> IO a) -> IO a
retrieveEvents aggregateId v0 p = retrieveEvents' aggregateId v0 $ SC.map (fmap g) >=> p
retrieveAllEvents :: forall a. (InputStream (UUID, PersistedEvent e') -> IO a) -> IO a
retrieveAllEvents p = retrieveAllEvents' $ SC.map (\(aggregateId, e) -> (aggregateId, fmap g e)) >=> p