Module

Database.Postgres.Transaction

Package
purescript-node-postgres
Repository
epost/purescript-node-postgres

#withTransaction Source

withTransaction :: forall a. (Client -> Aff a) -> Client -> Aff a

Runs an asynchronous action in a database transaction. The transaction will be rolled back if the computation fails and committed otherwise.

Here the first insert will be rolled back:

moneyTransfer :: forall e. (Client -> Aff e Unit) -> Client -> Aff e Unit
moneyTransfer = withTransaction $ \c -> do
  execute_ (Query "insert into accounts ...") c
  throwError $ error "Something went wrong"
  execute_ (Query "insert into accounts ...") c