From b879abadade3d295669611db6ca61849ad103438 Mon Sep 17 00:00:00 2001 From: Erik Post Date: Thu, 30 Apr 2015 14:24:38 +0200 Subject: [PATCH 1/3] Some minor polishing. --- README.org | 2 +- bower.json | 1 + src/Database/Postgres.purs | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.org b/README.org index 7b85e1f..af930e3 100644 --- a/README.org +++ b/README.org @@ -34,6 +34,6 @@ pulp install pulp test #+end_src -* Documentation +* Usage See [[file:./MODULE.md][Module documentation]]. diff --git a/bower.json b/bower.json index 86dac26..0917bfb 100644 --- a/bower.json +++ b/bower.json @@ -7,6 +7,7 @@ { "name": "Antti Holvikari" } ], "license": "MIT", + "keywords": ["purescript", "database", "sql", "postgres"], "ignore": [ "**/.*", "node_modules", diff --git a/src/Database/Postgres.purs b/src/Database/Postgres.purs index 7b3497b..138a362 100644 --- a/src/Database/Postgres.purs +++ b/src/Database/Postgres.purs @@ -68,7 +68,7 @@ execute_ :: forall eff a. Query a -> Client -> Aff (db :: DB | eff) Unit execute_ (Query sql) client = void $ runQuery_ sql client -- | Runs a query and returns all results. -query :: forall eff a p +query :: forall eff a . (IsForeign a) => Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) [F a] query (Query sql) params client = do @@ -144,7 +144,7 @@ foreign import connect' """ return client; } } - """ :: forall eff. String -> Aff (db :: DB | eff) Client + """ :: forall eff. ConnectionString -> Aff (db :: DB | eff) Client foreign import runQuery_ """ function runQuery_(queryStr) { From c8ef75e6289525069020bfe6bae2eb6f3781463e Mon Sep 17 00:00:00 2001 From: Erik Post Date: Thu, 30 Apr 2015 14:41:39 +0200 Subject: [PATCH 2/3] Replace node-postgres by node-any-db calls in FFI sections. --- README.org | 6 +++--- src/Database/Postgres.purs | 32 +++++++++++++++----------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/README.org b/README.org index af930e3..2640285 100644 --- a/README.org +++ b/README.org @@ -4,11 +4,11 @@ This is an initial experiment in providing bindings to the [[https://www.npmjs.o * Installation -I'll assume you have [[http://www.purescript.org/][PureScript]] and [[http://www.postgresql.org/][PostgreSQL]] installed. You'll also need [[https://github.com/bodil/pulp][purescript-pulp]] and [[https://www.npmjs.org/package/pg][pg]]: +I'll assume you have [[http://www.purescript.org/][PureScript]] and [[http://www.postgresql.org/][PostgreSQL]] installed. You'll also need [[https://github.com/bodil/pulp][purescript-pulp]] and the postgres version of [[https://github.com/grncdr/node-any-db][node-any-db]]: #+begin_src bash -npm install pg -npm install -g pulp +npm install any-db-postgres +npm install pulp #+end_src Clone the project: diff --git a/src/Database/Postgres.purs b/src/Database/Postgres.purs index 138a362..3b3d287 100644 --- a/src/Database/Postgres.purs +++ b/src/Database/Postgres.purs @@ -132,25 +132,23 @@ finally a sequel = do foreign import connect' """ function connect$prime(conString) { return function(success, error) { - var pg = require('pg'); - var client = new pg.Client(conString); - client.connect(function(err) { + var anyDB = require('any-db'); + anyDB.createConnection(conString, function(err, con) { if (err) { error(err); } else { - success(client); + success(con); } - }) - return client; - } + }); + }; } """ :: forall eff. ConnectionString -> Aff (db :: DB | eff) Client foreign import runQuery_ """ function runQuery_(queryStr) { - return function(client) { + return function(con) { return function(success, error) { - client.query(queryStr, function(err, result) { + con.query(queryStr, function(err, result) { if (err) { error(err); } else { @@ -165,9 +163,9 @@ foreign import runQuery_ """ foreign import runQuery """ function runQuery(queryStr) { return function(params) { - return function(client) { + return function(con) { return function(success, error) { - client.query(queryStr, params, function(err, result) { + con.query(queryStr, params, function(err, result) { if (err) return error(err); success(result.rows); }) @@ -179,9 +177,9 @@ foreign import runQuery """ foreign import runQueryValue_ """ function runQueryValue_(queryStr) { - return function(client) { + return function(con) { return function(success, error) { - client.query(queryStr, function(err, result) { + con.query(queryStr, function(err, result) { if (err) return error(err); success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined); }) @@ -193,9 +191,9 @@ foreign import runQueryValue_ """ foreign import runQueryValue """ function runQueryValue(queryStr) { return function(params) { - return function(client) { + return function(con) { return function(success, error) { - client.query(queryStr, params, function(err, result) { + con.query(queryStr, params, function(err, result) { if (err) return error(err); success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined); }) @@ -206,9 +204,9 @@ foreign import runQueryValue """ """ :: forall eff. String -> [SqlValue] -> Client -> Aff (db :: DB | eff) Foreign foreign import end """ - function end(client) { + function end(con) { return function() { - client.end(); + con.end(); }; } """ :: forall eff. Client -> Eff (db :: DB | eff) Unit From b5680cba4a9c62d3c297a589589f62b3e9337358 Mon Sep 17 00:00:00 2001 From: Erik Post Date: Thu, 30 Apr 2015 15:05:28 +0200 Subject: [PATCH 3/3] Replace `Client` from node-postgres by `Connection` from node-anydb. --- src/Database/Postgres.purs | 70 +++++++++++++++++++------------------- test/Main.purs | 6 ++-- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/Database/Postgres.purs b/src/Database/Postgres.purs index 3b3d287..4db6940 100644 --- a/src/Database/Postgres.purs +++ b/src/Database/Postgres.purs @@ -1,6 +1,6 @@ module Database.Postgres ( Query(..) - , Client() + , Connection() , DB() , ConnectionInfo() , ConnectionString() @@ -32,7 +32,7 @@ import Database.Postgres.SqlValue newtype Query a = Query String -foreign import data Client :: * +foreign import data Connection :: * foreign import data DB :: ! @@ -56,68 +56,68 @@ mkConnectionString ci = <> ci.db -- | Makes a connection to the database. -connect :: forall eff. ConnectionInfo -> Aff (db :: DB | eff) Client +connect :: forall eff. ConnectionInfo -> Aff (db :: DB | eff) Connection connect = connect' <<< mkConnectionString -- | Runs a query and returns nothing. -execute :: forall eff a. Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) Unit -execute (Query sql) params client = void $ runQuery sql params client +execute :: forall eff a. Query a -> [SqlValue] -> Connection -> Aff (db :: DB | eff) Unit +execute (Query sql) params con = void $ runQuery sql params con -- | Runs a query and returns nothing -execute_ :: forall eff a. Query a -> Client -> Aff (db :: DB | eff) Unit -execute_ (Query sql) client = void $ runQuery_ sql client +execute_ :: forall eff a. Query a -> Connection -> Aff (db :: DB | eff) Unit +execute_ (Query sql) con = void $ runQuery_ sql con -- | Runs a query and returns all results. query :: forall eff a . (IsForeign a) - => Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) [F a] -query (Query sql) params client = do - rows <- runQuery sql params client + => Query a -> [SqlValue] -> Connection -> Aff (db :: DB | eff) [F a] +query (Query sql) params con = do + rows <- runQuery sql params con pure $ read <$> rows -- | Just like `query` but does not make any param replacement -query_ :: forall eff a. (IsForeign a) => Query a -> Client -> Aff (db :: DB | eff) [a] -query_ (Query sql) client = do - rows <- runQuery_ sql client +query_ :: forall eff a. (IsForeign a) => Query a -> Connection -> Aff (db :: DB | eff) [a] +query_ (Query sql) con = do + rows <- runQuery_ sql con either liftError pure (sequence $ read <$> rows) -- | Runs a query and returns the first row, if any queryOne :: forall eff a . (IsForeign a) - => Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) (Maybe a) -queryOne (Query sql) params client = do - rows <- runQuery sql params client + => Query a -> [SqlValue] -> Connection -> Aff (db :: DB | eff) (Maybe a) +queryOne (Query sql) params con = do + rows <- runQuery sql params con maybe (pure Nothing) (either liftError (pure <<< Just)) $ read <$> (rows !! 0) -- | Just like `queryOne` but does not make any param replacement -queryOne_ :: forall eff a. (IsForeign a) => Query a -> Client -> Aff (db :: DB | eff) (Maybe a) -queryOne_ (Query sql) client = do - rows <- runQuery_ sql client +queryOne_ :: forall eff a. (IsForeign a) => Query a -> Connection -> Aff (db :: DB | eff) (Maybe a) +queryOne_ (Query sql) con = do + rows <- runQuery_ sql con maybe (pure Nothing) (either liftError (pure <<< Just)) $ read <$> (rows !! 0) -- | Runs a query and returns a single value, if any. queryValue :: forall eff a . (IsForeign a) - => Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) (Maybe a) -queryValue (Query sql) params client = do - val <- runQueryValue sql params client + => Query a -> [SqlValue] -> Connection -> Aff (db :: DB | eff) (Maybe a) +queryValue (Query sql) params con = do + val <- runQueryValue sql params con pure $ either (const Nothing) Just (read val) -- | Just like `queryValue` but does not make any param replacement -queryValue_ :: forall eff a. (IsForeign a) => Query a -> Client -> Aff (db :: DB | eff) (Maybe a) -queryValue_ (Query sql) client = do - val <- runQueryValue_ sql client +queryValue_ :: forall eff a. (IsForeign a) => Query a -> Connection -> Aff (db :: DB | eff) (Maybe a) +queryValue_ (Query sql) con = do + val <- runQueryValue_ sql con either liftError (pure <<< Just) $ read val --- | Connects to the database, calls the provided function with the client +-- | Connects to the database, calls the provided function with the connection -- | and returns the results. withConnection :: forall eff a . ConnectionInfo - -> (Client -> Aff (db :: DB | eff) a) + -> (Connection -> Aff (db :: DB | eff) a) -> Aff (db :: DB | eff) a withConnection info p = do - client <- connect info - finally (p client) $ liftEff (end client) + con <- connect info + finally (p con) $ liftEff (end con) liftError :: forall e a. ForeignError -> Aff e a liftError err = throwError $ error (show err) @@ -142,7 +142,7 @@ foreign import connect' """ }); }; } - """ :: forall eff. ConnectionString -> Aff (db :: DB | eff) Client + """ :: forall eff. ConnectionString -> Aff (db :: DB | eff) Connection foreign import runQuery_ """ function runQuery_(queryStr) { @@ -158,7 +158,7 @@ foreign import runQuery_ """ }; }; } - """ :: forall eff. String -> Client -> Aff (db :: DB | eff) [Foreign] + """ :: forall eff. String -> Connection -> Aff (db :: DB | eff) [Foreign] foreign import runQuery """ function runQuery(queryStr) { @@ -173,7 +173,7 @@ foreign import runQuery """ }; } } - """ :: forall eff. String -> [SqlValue] -> Client -> Aff (db :: DB | eff) [Foreign] + """ :: forall eff. String -> [SqlValue] -> Connection -> Aff (db :: DB | eff) [Foreign] foreign import runQueryValue_ """ function runQueryValue_(queryStr) { @@ -186,7 +186,7 @@ foreign import runQueryValue_ """ }; }; } - """ :: forall eff. String -> Client -> Aff (db :: DB | eff) Foreign + """ :: forall eff. String -> Connection -> Aff (db :: DB | eff) Foreign foreign import runQueryValue """ function runQueryValue(queryStr) { @@ -201,7 +201,7 @@ foreign import runQueryValue """ }; } } - """ :: forall eff. String -> [SqlValue] -> Client -> Aff (db :: DB | eff) Foreign + """ :: forall eff. String -> [SqlValue] -> Connection -> Aff (db :: DB | eff) Foreign foreign import end """ function end(con) { @@ -209,4 +209,4 @@ foreign import end """ con.end(); }; } - """ :: forall eff. Client -> Eff (db :: DB | eff) Unit + """ :: forall eff. Connection -> Eff (db :: DB | eff) Unit diff --git a/test/Main.purs b/test/Main.purs index 3a76067..d5472fd 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -51,10 +51,10 @@ exampleUsingWithConnection = withConnection connectionInfo $ \c -> do exampleLowLevel :: forall eff. Aff (trace :: Trace, db :: DB | eff) Unit exampleLowLevel = do - client <- connect connectionInfo - artists <- query_ (Query "select * from artist order by name desc" :: Query Artist) client + con <- connect connectionInfo + artists <- query_ (Query "select * from artist order by name desc" :: Query Artist) con liftEff $ printRows artists - liftEff $ end client + liftEff $ end con exampleError :: forall eff. Aff (db :: DB | eff) (Maybe Artist) exampleError = withConnection connectionInfo $ \c -> do