eventful-dynamodb 0.1.2 → 0.1.3
raw patch · 3 files changed
+44/−23 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- eventful-dynamodb.cabal +1/−1
- src/Eventful/Store/DynamoDB.hs +34/−18
- tests/Eventful/Store/DynamoDBSpec.hs +9/−4
eventful-dynamodb.cabal view
@@ -3,7 +3,7 @@ -- see: https://github.com/sol/hpack name: eventful-dynamodb-version: 0.1.2+version: 0.1.3 synopsis: Library for eventful DynamoDB event stores description: Library for eventful DynamoDB event stores category: Database,Eventsourcing,AWS
src/Eventful/Store/DynamoDB.hs view
@@ -23,7 +23,6 @@ import qualified Data.HashMap.Strict as HM import Data.List.NonEmpty (NonEmpty (..)) import Data.Maybe (fromMaybe, isJust, mapMaybe)-import Data.Monoid ((<>)) import Data.Text (Text) import qualified Data.Text as T import Network.AWS@@ -71,11 +70,11 @@ :: (MonadAWS m) => DynamoDBEventStoreConfig serialized -> UUID- -> Maybe EventVersion+ -> EventStoreQueryRange EventVersion -> m [StoredEvent serialized]-getDynamoEvents config@DynamoDBEventStoreConfig{..} uuid mStartingVersion = do+getDynamoEvents config@DynamoDBEventStoreConfig{..} uuid range = do latestEvents <-- paginate (queryBase config uuid mStartingVersion) =$=+ paginate (queryBase config uuid range) =$= CL.concatMap (view qrsItems) $$ CL.consume return $ mapMaybe (decodeDynamoEvent config uuid) latestEvents@@ -100,7 +99,7 @@ -> m EventVersion latestEventVersion config@DynamoDBEventStoreConfig{..} uuid = do latestEvents <- fmap (view qrsItems) . send $- queryBase config uuid Nothing+ queryBase config uuid allEvents & qLimit ?~ 1 & qScanIndexForward ?~ False return $ EventVersion $ fromMaybe (-1) $ do@@ -115,23 +114,40 @@ queryBase :: DynamoDBEventStoreConfig serialized -> UUID- -> Maybe EventVersion+ -> EventStoreQueryRange EventVersion -> Query-queryBase DynamoDBEventStoreConfig{..} uuid mStartingVersion =+queryBase DynamoDBEventStoreConfig{..} uuid EventStoreQueryRange{..} = query dynamoDBEventStoreConfigTableName- & qKeyConditionExpression ?~ "#uuid = :uuid" <> versionCaseExpression- & qExpressionAttributeNames .~- HM.singleton "#uuid" dynamoDBEventStoreConfigUUIDAttributeName <>- versionVariableName- & qExpressionAttributeValues .~- HM.singleton ":uuid" (attributeValue & avS ?~ uuidToText uuid) <>- versionAttributeValue+ & qKeyConditionExpression ?~ T.intercalate " AND " (uuidCaseExpression : versionCaseExpression)+ & qExpressionAttributeNames .~ HM.fromList (uuidAttributeName : versionAttributeName)+ & qExpressionAttributeValues .~ HM.fromList (uuidAttributeValue : versionAttributeValues) where- versionCaseExpression = maybe "" (const " AND #version >= :version") mStartingVersion- versionVariableName = maybe HM.empty (const $ HM.singleton "#version" dynamoDBEventStoreConfigVersionAttributeName) mStartingVersion- versionAttributeValue = maybe HM.empty (HM.singleton ":version" . mkVersionValue) mStartingVersion- mkVersionValue (EventVersion version) = attributeValue & avN ?~ T.pack (show version)+ uuidAttributeName = ("#uuid", dynamoDBEventStoreConfigUUIDAttributeName)+ uuidAttributeValue = (":uuid", attributeValue & avS ?~ uuidToText uuid)+ uuidCaseExpression = "#uuid = :uuid"++ mkStartVersionAttributeValue vers = (":startVersion", attributeValue & avN ?~ T.pack (show vers))+ mkEndVersionAttributeValue vers = (":endVersion", attributeValue & avN ?~ T.pack (show vers))+ mkJustStart (EventVersion start) = (["#version >= :startVersion"], [mkStartVersionAttributeValue start])+ mkJustEnd (EventVersion end) = (["#version <= :endVersion"], [mkEndVersionAttributeValue end])+ mkBoth (EventVersion start) (EventVersion end) =+ ( ["#version BETWEEN :startVersion AND :endVersion"]+ , [ mkStartVersionAttributeValue start+ , mkEndVersionAttributeValue end+ ]+ )+ (versionCaseExpression, versionAttributeValues) =+ case (eventStoreQueryRangeStart, eventStoreQueryRangeLimit) of+ (StartFromBeginning, NoQueryLimit) -> ([], [])+ (StartFromBeginning, MaxNumberOfEvents maxNum) -> mkJustEnd (EventVersion maxNum - 1)+ (StartFromBeginning, StopQueryAt end) -> mkJustEnd end+ (StartQueryAt start, NoQueryLimit) -> mkJustStart start+ (StartQueryAt start, MaxNumberOfEvents maxNum) -> mkBoth start (EventVersion maxNum + start - 1)+ (StartQueryAt start, StopQueryAt end) -> mkBoth start end+ versionAttributeName =+ if null versionCaseExpression then []+ else [("#version", dynamoDBEventStoreConfigVersionAttributeName)] storeDynamoEvents :: (MonadAWS m)
tests/Eventful/Store/DynamoDBSpec.hs view
@@ -11,6 +11,11 @@ import Eventful.Store.DynamoDB import Eventful.TestHelpers +spec :: Spec+spec = do+ describe "DynamoDB event store" $ do+ eventStoreSpec dynamoRunner+ makeStore :: IO (EventStore CounterEvent AWS, Env) makeStore = do let@@ -27,7 +32,7 @@ return (store', env) -spec :: Spec-spec = do- describe "DynamoDB event store" $ do- eventStoreSpec makeStore (\env action -> runResourceT $ runAWS env action)+dynamoRunner :: EventStoreRunner AWS+dynamoRunner = EventStoreRunner $ \action -> do+ (store, env) <- makeStore+ runResourceT $ runAWS env (action store)