Skip to content

Commit f00c15b

Browse files
committed
Reintroduce withConnection without pooling
Rename the pooling version to `withClient`.
1 parent b7e882e commit f00c15b

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

MODULE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ withConnection :: forall eff a. ConnectionInfo -> (Client -> Aff (db :: DB | eff
112112
Connects to the database, calls the provided function with the client
113113
and returns the results.
114114

115+
#### `withClient`
116+
117+
``` purescript
118+
withClient :: forall eff a. ConnectionInfo -> (Client -> Aff (db :: DB | eff) a) -> Aff (db :: DB | eff) a
119+
```
120+
121+
Takes a Client from the connection pool, runs the given function with
122+
the client and returns the results.
123+
115124
#### `end`
116125

117126
``` purescript

src/Database/Postgres.purs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Database.Postgres
1313
, queryValue, queryValue_
1414
, queryOne, queryOne_
1515
, withConnection
16+
, withClient
1617
) where
1718

1819
import Control.Alt
@@ -117,11 +118,28 @@ withConnection :: forall eff a
117118
. ConnectionInfo
118119
-> (Client -> Aff (db :: DB | eff) a)
119120
-> Aff (db :: DB | eff) a
120-
withConnection info p = runFn2 _withConnection (mkConnectionString info) p
121+
withConnection info p = do
122+
client <- connect info
123+
finally (p client) $ liftEff (end client)
124+
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
121132

122133
liftError :: forall e a. ForeignError -> Aff e a
123134
liftError err = throwError $ error (show err)
124135

136+
finally :: forall eff a. Aff eff a -> Aff eff Unit -> Aff eff a
137+
finally a sequel = do
138+
res <- attempt a
139+
sequel
140+
either throwError pure res
141+
142+
125143
foreign import connect' """
126144
function connect$prime(conString) {
127145
return function(success, error) {
@@ -139,9 +157,9 @@ foreign import connect' """
139157
}
140158
""" :: forall eff. String -> Aff (db :: DB | eff) Client
141159

142-
foreign import _withConnection
160+
foreign import _withClient
143161
"""
144-
function _withConnection(conString, cb) {
162+
function _withClient(conString, cb) {
145163
return function(success, error) {
146164
var pg = require('pg');
147165
pg.connect(conString, function(err, client, done) {

test/Main.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ exampleError = withConnection connectionInfo $ \c -> do
6565
queryOne_ (Query "select year from artist") c
6666

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

0 commit comments

Comments
 (0)