Add hoistPG function

This commit is contained in:
Tomasz Rybarczyk 2018-12-16 18:55:37 +01:00
parent a6deace1cb
commit 280320b553

View File

@ -27,6 +27,7 @@ import Database.PostgreSQL.Row (class FromSQLRow, class ToSQLRow, Row1)
import Database.PostgreSQL.Value (class FromSQLValue) import Database.PostgreSQL.Value (class FromSQLValue)
import Database.PostgreSQL.Value (class FromSQLValue, class ToSQLValue, fromSQLValue, instantFromString, instantToString, null, toSQLValue, unsafeIsBuffer) as Value import Database.PostgreSQL.Value (class FromSQLValue, class ToSQLValue, fromSQLValue, instantFromString, instantToString, null, toSQLValue, unsafeIsBuffer) as Value
import Effect.Aff (Aff) import Effect.Aff (Aff)
import Effect.Aff.Class (class MonadAff, liftAff)
type Database = String type Database = String
@ -39,6 +40,12 @@ type Database = String
-- | PGError a)`. -- | PGError a)`.
type PG a = ExceptT PGError Aff a type PG a = ExceptT PGError Aff a
hoistPG m. MonadAff m PG ~> ExceptT PGError m
hoistPG m = ExceptT $ liftAff $
runExceptT m >>= case _ of
Right a pure (Right a)
Left pgError pure (Left pgError)
-- | Run an action with a connection. The connection is released to the pool -- | Run an action with a connection. The connection is released to the pool
-- | when the action returns. -- | when the action returns.
withConnection withConnection
@ -118,3 +125,5 @@ onIntegrityError errorResult db =
case e of case e of
IntegrityError _ -> errorResult IntegrityError _ -> errorResult
_ -> throwError e _ -> throwError e