diff --git a/README.md b/README.md index fb26a3f3..5aa84485 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Introduction -This library unifies [Promise] and [PG] to facilitate writing easy-to-read database code that relies on promises: +This library unifies [Promise] and [PG] to help writing easy-to-read database code that relies on promises: * Simplistic approach to organizing streamlined database code, thanks to full [Promise] integration; * Database connections are managed automatically, in every usage case; * Functions, Procedures and Transactions are all fully supported; * Robust approach to handling results from every single query. # Install -```javascript +``` $ npm install pg-promise ``` @@ -180,8 +180,8 @@ pgp.connect().then(function(db){ * It reached first Beta version 0.1.0 on March 4th, 2015. * The first draft v0.0.1 was published on March 3rd, 2015, and then rapidly incremented due to many initial changes that had to come in, mostly documentation. -[PG]:https://www.npmjs.com/package/pg -[Promise]:https://www.npmjs.com/package/promise +[PG]:https://github.com/brianc/node-postgres +[Promise]:https://github.com/then/promise # License diff --git a/index.js b/index.js index 5e0cd395..015470ef 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,6 @@ +// Cannot declare 'use strict' here, because queryResult +// needs to be exported into the global namespace. + var npm = { promise: require('promise'), pg: require('pg') @@ -22,16 +25,15 @@ queryResult = { // 1. config (required) - either configuration // object or connection string. Either way, // it is merely passed on to PG and not used -// by this library in any way. -// 2. options (optional) - object with optional -// configuration properties: +// by this library. +// 2. options (optional) - // { // connect: function(client){ -// connection-notify event; +// on-connect event; // client - pg connection object. // }, // disconnect: function(client){ -// disconnection-notify event; +// on-disconnect event; // client - pg connection object. // } // } @@ -47,6 +49,7 @@ module.exports = function (config, options) { }; var $self = { + // IMPORTANT: The caller must invoke done() after requests are finished. connect: function () { return $p(function (resolve, reject) { @@ -62,10 +65,13 @@ module.exports = function (config, options) { }); }); }, + + // Terminates pg library; call it when exiting the application. end: function(){ - npm.pg.end(); // Terminates pg library; + npm.pg.end(); }, - // Simple query, with opening and closing connection; + + // Generic query request; query: function (query, qr) { return $p(function (resolve, reject) { $self.connect() @@ -113,6 +119,7 @@ module.exports = function (config, options) { return this.oneOrNone($prop.createFuncQuery(procName, params)); }, + // Namespace for type conversion helpers; as: { bool: function (val) { if ($prop.isNull(val)) { @@ -138,6 +145,7 @@ module.exports = function (config, options) { } }, + // Transaction class; tx: function () { var tx = this; @@ -317,30 +325,35 @@ module.exports = function (config, options) { }, query: function (client, query, qr) { return $p(function (resolve, reject) { - client.query(query, function (err, result) { - if (err) { - reject(err.message); - } else { - var data = result.rows; - var l = result.rows.length; - if (l) { - if (l > 1 && !(qr & queryResult.many)) { - reject("Single record was expected from query: '" + query + "'"); - } else { - if (!(qr & queryResult.many)) { - data = result.rows[0]; - } - } + var badMask = queryResult.one | queryResult.many; + if((qr & badMask) === badMask){ + reject("Invalid query result mask: one + many"); + }else { + client.query(query, function (err, result) { + if (err) { + reject(err.message); } else { - if (qr & queryResult.none) { - data = null; + var data = result.rows; + var l = result.rows.length; + if (l) { + if (l > 1 && !(qr & queryResult.many)) { + reject("Single row was expected from query: '" + query + "'"); + } else { + if (!(qr & queryResult.many)) { + data = result.rows[0]; + } + } } else { - reject("No records returned from query: '" + query + "'"); + if (qr & queryResult.none) { + data = null; + } else { + reject("No rows returned from query: '" + query + "'"); + } } + resolve(data); } - resolve(data); - } - }); + }); + } }); } }; diff --git a/package.json b/package.json index d47b329c..82c15503 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, + "homepage": "https://github.com/VitalyTomilov/pg-promise", "repository": { "type": "git", "url": "https://github.com/VitalyTomilov/pg-promise.git"