Skip to content

Commit b7e882e

Browse files
committed
Use connection pooling and add disconnect for disconnecting all connections
Fixes #4, but requires some work on configuring the pool.
1 parent b277700 commit b7e882e

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

MODULE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ end :: forall eff. Client -> Eff (db :: DB | eff) Unit
119119
```
120120

121121

122+
#### `disconnect`
123+
124+
``` purescript
125+
disconnect :: forall eff. Eff (db :: DB | eff) Unit
126+
```
127+
128+
122129

123130
## Module Database.Postgres.SqlValue
124131

src/Database/Postgres.purs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Database.Postgres
66
, ConnectionString()
77
, mkConnectionString
88
, connect
9+
, disconnect
910
, end
1011
, execute, execute_
1112
, query, query_
@@ -18,6 +19,7 @@ import Control.Alt
1819
import Control.Monad.Eff
1920
import Control.Monad.Trans
2021
import Data.Either
22+
import Data.Function (Fn2(), runFn2)
2123
import Data.Array
2224
import Data.Foreign
2325
import Data.Foreign.Class
@@ -115,20 +117,11 @@ withConnection :: forall eff a
115117
. ConnectionInfo
116118
-> (Client -> Aff (db :: DB | eff) a)
117119
-> Aff (db :: DB | eff) a
118-
withConnection info p = do
119-
client <- connect info
120-
finally (p client) $ liftEff (end client)
120+
withConnection info p = runFn2 _withConnection (mkConnectionString info) p
121121

122122
liftError :: forall e a. ForeignError -> Aff e a
123123
liftError err = throwError $ error (show err)
124124

125-
finally :: forall eff a. Aff eff a -> Aff eff Unit -> Aff eff a
126-
finally a sequel = do
127-
res <- attempt a
128-
sequel
129-
either throwError pure res
130-
131-
132125
foreign import connect' """
133126
function connect$prime(conString) {
134127
return function(success, error) {
@@ -146,6 +139,32 @@ foreign import connect' """
146139
}
147140
""" :: forall eff. String -> Aff (db :: DB | eff) Client
148141

142+
foreign import _withConnection
143+
"""
144+
function _withConnection(conString, cb) {
145+
return function(success, error) {
146+
var pg = require('pg');
147+
pg.connect(conString, function(err, client, done) {
148+
if (err) {
149+
done(true);
150+
return error(err);
151+
}
152+
cb(client)(function(v) {
153+
done();
154+
success(v);
155+
}, function(err) {
156+
done();
157+
error(err);
158+
})
159+
});
160+
};
161+
}
162+
""" :: forall eff a.
163+
Fn2
164+
ConnectionString
165+
(Client -> Aff (db :: DB | eff) a)
166+
(Aff (db :: DB | eff) a)
167+
149168
foreign import runQuery_ """
150169
function runQuery_(queryStr) {
151170
return function(client) {
@@ -212,3 +231,11 @@ foreign import end """
212231
};
213232
}
214233
""" :: forall eff. Client -> Eff (db :: DB | eff) Unit
234+
235+
foreign import disconnect
236+
"""
237+
function disconnect() {
238+
var pg = require('pg');
239+
pg.end();
240+
}
241+
""" :: forall eff. Eff (db :: DB | eff) Unit

test/Main.purs

Lines changed: 2 additions & 0 deletions
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

0 commit comments

Comments
 (0)