purescript-postgresql-client icon indicating copy to clipboard operation
purescript-postgresql-client copied to clipboard

Usability with constrained monad

Open Kamirus opened this issue 5 years ago • 0 comments

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

Kamirus avatar Feb 07 '20 10:02 Kamirus