Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new function for conditional access to db #21

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions src/EulerHS/KVConnector/Flow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module EulerHS.KVConnector.Flow
findAllWithOptionsKVConnector',
deleteWithKVConnector,
deleteReturningWithKVConnector,
deleteAllReturningWithKVConnector
deleteAllReturningWithKVConnector,
findAllWithKVAndConditionalDBInternal
)
where

Expand Down Expand Up @@ -897,8 +898,8 @@ findAllWithOptionsKVConnector' :: forall be table beM m.
Maybe Int ->
Maybe Int ->
m (MeshResult [table Identity])
findAllWithOptionsKVConnector' dbConf meshCfg whereClause mbLimit mbOffset =
findAllWithOptionsHelper dbConf meshCfg whereClause Nothing mbLimit mbOffset
findAllWithOptionsKVConnector' dbConf meshCfg whereClause =
findAllWithOptionsHelper dbConf meshCfg whereClause Nothing

findAllWithKVConnector :: forall be table beM m.
( HasCallStack,
Expand All @@ -925,7 +926,7 @@ findAllWithKVConnector dbConf meshCfg whereClause = do
let matchedKVLiveRows = findAllMatching whereClause (fst kvRows)
dbRes <- runQuery dbConf findAllQuery
case dbRes of
Right dbRows -> pure $ Right $ matchedKVLiveRows ++ (getUniqueDBRes dbRows (fst kvRows ++ snd kvRows))
Right dbRows -> pure $ Right $ matchedKVLiveRows ++ getUniqueDBRes dbRows (fst kvRows ++ snd kvRows)
Left err -> return $ Left $ MDBError err
Left err -> return $ Left err
else do
Expand All @@ -934,6 +935,41 @@ findAllWithKVConnector dbConf meshCfg whereClause = do
let source = if not isDisabled then KV_AND_SQL else SQL
pure res

findAllWithKVAndConditionalDBInternal :: forall be table beM m.
( HasCallStack,
BeamRuntime be beM,
Model be table,
MeshMeta be table,
KVConnector (table Identity),
ToJSON (table Identity),
FromJSON (table Identity),
Serialize.Serialize (table Identity),
L.MonadFlow m, B.HasQBuilder be, BeamRunner beM) =>
DBConfig beM ->
MeshConfig ->
Where be table ->
m (MeshResult [table Identity])
findAllWithKVAndConditionalDBInternal dbConf meshCfg whereClause = do
let isDisabled = meshCfg.kvHardKilled
if not isDisabled
then do
kvRes <- redisFindAll meshCfg whereClause
case kvRes of
Right kvRows -> do
let matchedKVLiveRows = findAllMatching whereClause (fst kvRows)
if not (null matchedKVLiveRows)
then pure $ Right matchedKVLiveRows
else do
let findAllQueryUpdated = DB.findRows (sqlSelect ! #where_ whereClause ! defaults)
dbRes <- runQuery dbConf findAllQueryUpdated
case dbRes of
Right dbRows -> pure $ Right $ getUniqueDBRes dbRows (snd kvRows)
Left err -> return $ Left $ MDBError err
Left err -> return $ Left err
else do
let findAllQuery = DB.findRows (sqlSelect ! #where_ whereClause ! defaults)
mapLeft MDBError <$> runQuery dbConf findAllQuery

redisFindAll :: forall be table beM m.
( HasCallStack,
BeamRuntime be beM,
Expand Down Expand Up @@ -977,7 +1013,7 @@ deleteObjectRedis :: forall table be beM m.
-- Show (table Identity), --debugging purpose
L.MonadFlow m
) =>
MeshConfig -> Bool -> Where be table -> (table Identity) -> m (MeshResult (table Identity))
MeshConfig -> Bool -> Where be table -> table Identity -> m (MeshResult (table Identity))
deleteObjectRedis meshCfg addPrimaryKeyToWhereClause whereClause obj = do
time <- fromIntegral <$> L.getCurrentDateInMillis
let pKeyText = getLookupKeyByPKey obj
Expand Down