{-# LANGUAGE TemplateHaskell #-}
module Effectful.PostgreSQL.Connection
(
WithConnection (..)
, withConnection
, runWithConnection
, runWithConnectInfo
)
where
import qualified Database.PostgreSQL.Simple as PSQL
import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.TH
data WithConnection :: Effect where
WithConnection :: (PSQL.Connection -> m a) -> WithConnection m a
makeEffect ''WithConnection
runWithConnection ::
(HasCallStack) => PSQL.Connection -> Eff (WithConnection : es) a -> Eff es a
runWithConnection :: forall (es :: [Effect]) a.
HasCallStack =>
Connection -> Eff (WithConnection : es) a -> Eff es a
runWithConnection Connection
conn = EffectHandler WithConnection es
-> Eff (WithConnection : es) a -> Eff es a
forall (e :: Effect) (es :: [Effect]) a.
(DispatchOf e ~ 'Dynamic) =>
EffectHandler e es -> Eff (e : es) a -> Eff es a
interpret (EffectHandler WithConnection es
-> Eff (WithConnection : es) a -> Eff es a)
-> EffectHandler WithConnection es
-> Eff (WithConnection : es) a
-> Eff es a
forall a b. (a -> b) -> a -> b
$ \LocalEnv localEs es
env -> \case
WithConnection Connection -> Eff localEs a
f ->
LocalEnv localEs es
-> ((forall {r}. Eff localEs r -> Eff es r) -> Eff es a)
-> Eff es a
forall (es :: [Effect]) (handlerEs :: [Effect])
(localEs :: [Effect]) a.
(HasCallStack, SharedSuffix es handlerEs) =>
LocalEnv localEs handlerEs
-> ((forall r. Eff localEs r -> Eff es r) -> Eff es a) -> Eff es a
localSeqUnlift LocalEnv localEs es
env (((forall {r}. Eff localEs r -> Eff es r) -> Eff es a) -> Eff es a)
-> ((forall {r}. Eff localEs r -> Eff es r) -> Eff es a)
-> Eff es a
forall a b. (a -> b) -> a -> b
$ \forall {r}. Eff localEs r -> Eff es r
unlift -> Eff localEs a -> Eff es a
forall {r}. Eff localEs r -> Eff es r
unlift (Eff localEs a -> Eff es a) -> Eff localEs a -> Eff es a
forall a b. (a -> b) -> a -> b
$ Connection -> Eff localEs a
f Connection
conn
runWithConnectInfo ::
(HasCallStack, IOE :> es) => PSQL.ConnectInfo -> Eff (WithConnection : es) a -> Eff es a
runWithConnectInfo :: forall (es :: [Effect]) a.
(HasCallStack, IOE :> es) =>
ConnectInfo -> Eff (WithConnection : es) a -> Eff es a
runWithConnectInfo ConnectInfo
connInfo Eff (WithConnection : es) a
eff =
((forall a. Eff es a -> IO a) -> IO a) -> Eff es a
forall b. ((forall a. Eff es a -> IO a) -> IO b) -> Eff es b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. Eff es a -> IO a) -> IO a) -> Eff es a)
-> ((forall a. Eff es a -> IO a) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \forall a. Eff es a -> IO a
unlift ->
IO a -> IO a
forall a. IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> IO a)
-> ((Connection -> IO a) -> IO a) -> (Connection -> IO a) -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConnectInfo -> (Connection -> IO a) -> IO a
forall c. ConnectInfo -> (Connection -> IO c) -> IO c
PSQL.withConnect ConnectInfo
connInfo ((Connection -> IO a) -> IO a) -> (Connection -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \Connection
conn ->
Eff es a -> IO a
forall a. Eff es a -> IO a
unlift (Eff es a -> IO a) -> Eff es a -> IO a
forall a b. (a -> b) -> a -> b
$ Connection -> Eff (WithConnection : es) a -> Eff es a
forall (es :: [Effect]) a.
HasCallStack =>
Connection -> Eff (WithConnection : es) a -> Eff es a
runWithConnection Connection
conn Eff (WithConnection : es) a
eff