Module

Database.Postgres.Transaction

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

#withTransaction Source

withTransaction :: forall a eff. (Client -> Aff (db :: DB | eff) a) -> Client -> Aff (db :: DB | eff) 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

#begin Source

begin :: forall eff. Client -> Aff (db :: DB | eff) Unit

#commit Source

commit :: forall eff. Client -> Aff (db :: DB | eff) Unit

#rollback Source

rollback :: forall eff. Client -> Aff (db :: DB | eff) Unit