rightfold / purescript-postgresql-client

https://pursuit.purescript.org/packages/purescript-postgresql-client
BSD 3-Clause "New" or "Revised" License
35 stars 20 forks source link

Usability with constrained monad #45

Open Kamirus opened 4 years ago

Kamirus commented 4 years ago

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