/
Pool.purs
67 lines (54 loc) · 1.49 KB
/
Pool.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
57
58
59
60
61
62
63
64
65
66
67
module MySQL.Pool
( PoolInfo
, Pool
, defaultPoolInfo
, createPool
, closePool
, withPool
) where
import Prelude
import Data.Function.Uncurried (Fn2, runFn2)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Foreign (Foreign)
import MySQL.Connection (ConnectionInfo, Connection)
import MySQL.Milliseconds (Milliseconds(..))
import Simple.JSON (write)
type PoolInfo =
{ acquireTimeout :: Milliseconds
, waitForConnections :: Boolean
, connectionLimit :: Int
, queueLimit :: Int
}
foreign import data Pool :: Type
defaultPoolInfo :: PoolInfo
defaultPoolInfo =
{ acquireTimeout: Milliseconds 10000.0
, waitForConnections: true
, connectionLimit: 10
, queueLimit: 0
}
createPool :: ConnectionInfo -> PoolInfo -> Effect Pool
createPool cinfo pinfo =
runFn2 _createPool
(write cinfo)
(write pinfo)
getConnection :: Pool -> Aff Connection
getConnection = fromEffectFnAff <<< _getConnection
releaseConnection :: Connection -> Aff Unit
releaseConnection = fromEffectFnAff <<< _releaseConnection
withPool
:: forall a
. (Connection -> Aff a)
-> Pool
-> Aff a
withPool handler pool = do
conn <- getConnection pool
r <- handler conn
releaseConnection conn
pure r
foreign import _createPool :: Fn2 Foreign Foreign (Effect Pool)
foreign import closePool :: Pool -> Effect Unit
foreign import _getConnection :: Pool -> EffectFnAff Connection
foreign import _releaseConnection :: Connection -> EffectFnAff Unit