purescript-postgresql-client
purescript-postgresql-client copied to clipboard
Usability with constrained monad
The problem is with functions like withTransaction or withConnection
that require a function (m a -> Aff (Either PGError a)), where m is the constrained monad.
AFAIK you must concretize the monad type to use it.
I ended up writing my own version of withTransaction
withTransaction_
∷ ∀ e m
. MonadError e m
⇒ (String → m Unit)
→ m ~> m
withTransaction_ execute action = do
execute "BEGIN TRANSACTION"
a ← action `catchError` \e → do
execute "ROLLBACK TRANSACTION"
throwError e
execute "COMMIT TRANSACTION"
pure a
And the specific version:
withTransaction ∷ ∀ m. MyMonad m ⇒ m ~> m
withTransaction = withTransaction_ myExecute