/
Transaction.purs
56 lines (33 loc) · 962 Bytes
/
Transaction.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module MySQL.Transaction
( withTransaction
) where
import Prelude
import Effect.Aff (Aff, attempt)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Control.Monad.Error.Class (throwError)
import Data.Either (Either(..))
import MySQL.Connection (Connection)
withTransaction
:: forall a
. (Connection -> Aff a)
-> Connection
-> Aff a
withTransaction handler conn = do
begin conn
r <- attempt $ handler conn
case r of
Left e -> do
rollback conn
throwError e
Right r' -> do
commit conn
pure r'
begin :: Connection -> Aff Unit
begin = fromEffectFnAff <<< _begin
commit :: Connection -> Aff Unit
commit = fromEffectFnAff <<< _commit
rollback :: Connection -> Aff Unit
rollback = fromEffectFnAff <<< _rollback
foreign import _begin :: Connection -> EffectFnAff Unit
foreign import _commit :: Connection -> EffectFnAff Unit
foreign import _rollback :: Connection -> EffectFnAff Unit