Skip to content

Add connection pooling #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions MODULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,29 @@ withConnection :: forall eff a. ConnectionInfo -> (Client -> Aff (db :: DB | eff
Connects to the database, calls the provided function with the client
and returns the results.

#### `withClient`

``` purescript
withClient :: forall eff a. ConnectionInfo -> (Client -> Aff (db :: DB | eff) a) -> Aff (db :: DB | eff) a
```

Takes a Client from the connection pool, runs the given function with
the client and returns the results.

#### `end`

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


#### `disconnect`

``` purescript
disconnect :: forall eff. Eff (db :: DB | eff) Unit
```



## Module Database.Postgres.SqlValue

Expand Down
45 changes: 45 additions & 0 deletions src/Database/Postgres.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ module Database.Postgres
, ConnectionString()
, mkConnectionString
, connect
, disconnect
, end
, execute, execute_
, query, query_
, queryValue, queryValue_
, queryOne, queryOne_
, withConnection
, withClient
) where

import Control.Alt
import Control.Monad.Eff
import Control.Monad.Trans
import Data.Either
import Data.Function (Fn2(), runFn2)
import Data.Array
import Data.Foreign
import Data.Foreign.Class
Expand Down Expand Up @@ -119,6 +122,14 @@ withConnection info p = do
client <- connect info
finally (p client) $ liftEff (end client)

-- | Takes a Client from the connection pool, runs the given function with
-- | the client and returns the results.
withClient :: forall eff a
. ConnectionInfo
-> (Client -> Aff (db :: DB | eff) a)
-> Aff (db :: DB | eff) a
withClient info p = runFn2 _withClient (mkConnectionString info) p

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

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

foreign import _withClient
"""
function _withClient(conString, cb) {
return function(success, error) {
var pg = require('pg');
pg.connect(conString, function(err, client, done) {
if (err) {
done(true);
return error(err);
}
cb(client)(function(v) {
done();
success(v);
}, function(err) {
done();
error(err);
})
});
};
}
""" :: forall eff a.
Fn2
ConnectionString
(Client -> Aff (db :: DB | eff) a)
(Aff (db :: DB | eff) a)

foreign import runQuery_ """
function runQuery_(queryStr) {
return function(client) {
Expand Down Expand Up @@ -212,3 +249,11 @@ foreign import end """
};
}
""" :: forall eff. Client -> Eff (db :: DB | eff) Unit

foreign import disconnect
"""
function disconnect() {
var pg = require('pg');
pg.end();
}
""" :: forall eff. Eff (db :: DB | eff) Unit
4 changes: 3 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ main = runAff (trace <<< show) (const $ trace "All ok") $ do

exampleQueries

liftEff $ disconnect

data Artist = Artist
{ name :: String
, year :: Number
Expand Down Expand Up @@ -63,7 +65,7 @@ exampleError = withConnection connectionInfo $ \c -> do
queryOne_ (Query "select year from artist") c

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