Skip to content

Commit

Permalink
Preparing 0.1.12 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Mar 4, 2015
1 parent 707913f commit 097ad71
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```

Expand Down Expand Up @@ -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

Expand Down
67 changes: 40 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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.
// }
// }
Expand All @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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)) {
Expand All @@ -138,6 +145,7 @@ module.exports = function (config, options) {
}
},

// Transaction class;
tx: function () {

var tx = this;
Expand Down Expand Up @@ -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);
}
});
});
}
});
}
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 097ad71

Please sign in to comment.