Commit 47ed29c5 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Experiment with Free-Church encoding in DBTx

parent b0568522
......@@ -42,6 +42,7 @@ import Database.PostgreSQL.Simple.Transaction qualified as PG
import Gargantext.Database.Class
import Opaleye
import Prelude
import Control.Monad.Free.Church
data DBOperation = DBRead | DBWrite
......@@ -75,7 +76,7 @@ data DBTransactionOp err (r :: DBOperation) next where
-- | Monadic failure for DB transactions.
DBFail :: err -> DBTransactionOp err r next
newtype DBTx err r a = DBTx { _DBTx :: Free (DBTransactionOp err r) a }
newtype DBTx err r a = DBTx { _DBTx :: F (DBTransactionOp err r) a }
deriving (Functor, Applicative, Monad)
type DBQuery err r a = DBTx err r a
......@@ -130,7 +131,7 @@ withReadOnlyTransactionM conn action =
runDBTx :: DBUpdate err a -> DBTxCmd err a
runDBTx (DBTx m) = do
pool <- view connPool
withResourceM pool $ \conn -> withTransactionM conn $ foldFree (evalOp conn) m
withResourceM pool $ \conn -> withTransactionM conn $ foldF (evalOp conn) m
-- | /NOTE/ the input type is 'DBReadOnly', i.e. a transaction where /all/
-- the operations are 'DBRead'. This makes impossible to sneak in updates
......@@ -138,7 +139,7 @@ runDBTx (DBTx m) = do
runDBQuery :: DBReadOnly err r a -> DBTxCmd err a
runDBQuery (DBTx m) = do
pool <- view connPool
withResourceM pool $ \conn -> withReadOnlyTransactionM conn $ foldFree (evalOp conn) m
withResourceM pool $ \conn -> withReadOnlyTransactionM conn $ foldF (evalOp conn) m
-- | The main evaluator, turns our pure operations into side-effects that run into the
-- 'DBCmd'.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment