Skip to content

Commit 52d4185

Browse files
committed
Merge pull request #8 from anttih/connection-pooling
Add connection pooling
2 parents 6b12a51 + f00c15b commit 52d4185

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

MODULE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,29 @@ withConnection :: forall eff a. ConnectionInfo -> (Client -> Aff (db :: DB | eff
126126
Connects to the database, calls the provided function with the client
127127
and returns the results.
128128

129+
#### `withClient`
130+
131+
``` purescript
132+
withClient :: forall eff a. ConnectionInfo -> (Client -> Aff (db :: DB | eff) a) -> Aff (db :: DB | eff) a
133+
```
134+
135+
Takes a Client from the connection pool, runs the given function with
136+
the client and returns the results.
137+
129138
#### `end`
130139

131140
``` purescript
132141
end :: forall eff. Client -> Eff (db :: DB | eff) Unit
133142
```
134143

135144

145+
#### `disconnect`
146+
147+
``` purescript
148+
disconnect :: forall eff. Eff (db :: DB | eff) Unit
149+
```
150+
151+
136152

137153
## Module Database.Postgres.SqlValue
138154

src/Database/Postgres.purs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ module Database.Postgres
66
, ConnectionString()
77
, mkConnectionString
88
, connect
9+
, disconnect
910
, end
1011
, execute, execute_
1112
, query, query_
1213
, queryValue, queryValue_
1314
, queryOne, queryOne_
1415
, withConnection
16+
, withClient
1517
) where
1618

1719
import Control.Alt
1820
import Control.Monad.Eff
1921
import Control.Monad.Trans
2022
import Data.Either
23+
import Data.Function (Fn2(), runFn2)
2124
import Data.Array
2225
import Data.Foreign
2326
import Data.Foreign.Class
@@ -119,6 +122,14 @@ withConnection info p = do
119122
client <- connect info
120123
finally (p client) $ liftEff (end client)
121124

125+
-- | Takes a Client from the connection pool, runs the given function with
126+
-- | the client and returns the results.
127+
withClient :: forall eff a
128+
. ConnectionInfo
129+
-> (Client -> Aff (db :: DB | eff) a)
130+
-> Aff (db :: DB | eff) a
131+
withClient info p = runFn2 _withClient (mkConnectionString info) p
132+
122133
liftError :: forall e a. ForeignError -> Aff e a
123134
liftError err = throwError $ error (show err)
124135

@@ -146,6 +157,32 @@ foreign import connect' """
146157
}
147158
""" :: forall eff. String -> Aff (db :: DB | eff) Client
148159

160+
foreign import _withClient
161+
"""
162+
function _withClient(conString, cb) {
163+
return function(success, error) {
164+
var pg = require('pg');
165+
pg.connect(conString, function(err, client, done) {
166+
if (err) {
167+
done(true);
168+
return error(err);
169+
}
170+
cb(client)(function(v) {
171+
done();
172+
success(v);
173+
}, function(err) {
174+
done();
175+
error(err);
176+
})
177+
});
178+
};
179+
}
180+
""" :: forall eff a.
181+
Fn2
182+
ConnectionString
183+
(Client -> Aff (db :: DB | eff) a)
184+
(Aff (db :: DB | eff) a)
185+
149186
foreign import runQuery_ """
150187
function runQuery_(queryStr) {
151188
return function(client) {
@@ -212,3 +249,11 @@ foreign import end """
212249
};
213250
}
214251
""" :: forall eff. Client -> Eff (db :: DB | eff) Unit
252+
253+
foreign import disconnect
254+
"""
255+
function disconnect() {
256+
var pg = require('pg');
257+
pg.end();
258+
}
259+
""" :: forall eff. Eff (db :: DB | eff) Unit

test/Main.purs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ main = runAff (trace <<< show) (const $ trace "All ok") $ do
2626

2727
exampleQueries
2828

29+
liftEff $ disconnect
30+
2931
data Artist = Artist
3032
{ name :: String
3133
, year :: Number
@@ -63,7 +65,7 @@ exampleError = withConnection connectionInfo $ \c -> do
6365
queryOne_ (Query "select year from artist") c
6466

6567
exampleQueries :: forall eff. Aff (trace :: Trace, db :: DB | eff) Unit
66-
exampleQueries = withConnection connectionInfo $ \c -> do
68+
exampleQueries = withClient connectionInfo $ \c -> do
6769
liftEff $ trace "Example queries with params:"
6870
execute_ (Query "delete from artist") c
6971
execute_ (Query "insert into artist values ('Led Zeppelin', 1968)") c

0 commit comments

Comments
 (0)