pg-promise
Advanced tools
Comparing version 0.5.0 to 0.5.1
41
index.js
@@ -11,6 +11,6 @@ // Cannot declare 'use strict' here, because queryResult | ||
queryResult = { | ||
one: 1, // single-row result is expected; | ||
many: 2, // multi-row result is expected; | ||
none: 4, // no rows expected; | ||
any: 6 // (default) = many|none = any result. | ||
one: 1, // single-row result is expected; | ||
many: 2, // multi-row result is expected; | ||
none: 4, // no rows expected; | ||
any: 6 // (default) = many|none = any result. | ||
}; | ||
@@ -27,3 +27,2 @@ | ||
// client has connected; | ||
// client - pg connection object. | ||
// }, | ||
@@ -33,13 +32,14 @@ // | ||
// client is disconnecting; | ||
// client - pg connection object. | ||
// }, | ||
// | ||
// query: function(client, query, params){ | ||
// query is executing; | ||
// }, | ||
// | ||
// pgFormatting: false, | ||
// - Redirects query formatting into PG library; | ||
// - Default is false, and all queries are formatted | ||
// - within 'pg-promise'. | ||
// - Default is false, and all queries are formatted within 'pg-promise'. | ||
// | ||
// promiseLib: null | ||
// - Overrides the promise library instance used by | ||
// the library. | ||
// - Overrides the promise library instance used by the library. | ||
// } | ||
@@ -51,6 +51,12 @@ module.exports = function (options) { | ||
} else { | ||
var promiseLib; | ||
var pmCostructor; | ||
if (options && options.promiseLib) { | ||
if (typeof(options.promiseLib) === 'function') { | ||
promiseLib = options.promiseLib; | ||
// 'Promise' object is supported by libraries 'bluebird', 'when' and 'q', | ||
// while our default 'promise' library uses its library function instead: | ||
if (typeof(options.promiseLib.Promise) === 'function') { | ||
pmCostructor = options.promiseLib.Promise; | ||
} else { | ||
pmCostructor = options.promiseLib; | ||
} | ||
} else { | ||
@@ -60,7 +66,7 @@ throw new Error('Invalid or unsupported promise library override.'); | ||
} else { | ||
promiseLib = require('promise'); | ||
pmCostructor = require('promise'); | ||
} | ||
npm = { | ||
pg: require('pg'), | ||
promise: promiseLib | ||
promise: pmCostructor | ||
}; | ||
@@ -413,2 +419,9 @@ } | ||
var params = pgFormatting ? values : null; | ||
if (options && options.query) { | ||
var func = options.query; | ||
if (typeof(func) !== 'function') { | ||
throw new Error("Function was expected for 'options.query'"); | ||
} | ||
func(client, req.query, params); // notify the client; | ||
} | ||
try { | ||
@@ -415,0 +428,0 @@ client.query(req.query, params, function (err, result) { |
{ | ||
"name": "pg-promise", | ||
"version": "0.5.0", | ||
"description": "PG + Promise made easy, with transactions support.", | ||
"version": "0.5.1", | ||
"description": "PG + Promises/A+, with transactions support.", | ||
"main": "index.js", | ||
@@ -20,2 +20,3 @@ "scripts": { | ||
"promise", | ||
"promises-aplus", | ||
"transaction", | ||
@@ -22,0 +23,0 @@ "postgres" |
pg-promise | ||
=========== | ||
Complete access layer to [PG] with any [Promises/A+] library. | ||
Complete access layer to [PG] via [Promises/A+]. | ||
@@ -14,3 +14,3 @@ [![Build Status](https://travis-ci.org/vitaly-t/pg-promise.svg?branch=master)](https://travis-ci.org/vitaly-t/pg-promise) | ||
* Declarative result handling for queries; | ||
* Use your favourite [Promises/A+] library; | ||
* Choice of [Promises/A+] libraries; | ||
* Automatic database connections. | ||
@@ -314,3 +314,7 @@ | ||
var options = { | ||
// override properties; | ||
// pgFormatting - redirects query formatting to PG; | ||
// promiseLib - overrides default promise library; | ||
// connect - database 'connect' notification; | ||
// disconnect - database 'disconnect' notification; | ||
// query - query execution notification. | ||
}; | ||
@@ -320,5 +324,6 @@ var pgp = pgpLib(options); | ||
Below is the list of all such properties that are currently supported. | ||
Below is the list of all the properties that are currently supported. | ||
##### pgFormatting | ||
--- | ||
* `pgFormatting` | ||
@@ -343,5 +348,6 @@ By default, **pg-promise** provides its own implementation of the query value formatting, | ||
##### promiseLib | ||
--- | ||
* `promiseLib` | ||
Set this property to any promise library that's compliant with the [Promises/A+] standard. | ||
Set this property to an alternative promise library compliant with the [Promises/A+] standard. | ||
@@ -361,9 +367,15 @@ By default, **pg-promise** uses version of [Promises/A+] provided by [Promise]. If you want to override | ||
As of this writing, [Bluebird] is the only alternative [Promises/A+] library that **pg-promise** | ||
has been tested against. Compatibility with other [Promises/A+] though expected, hasn't been tested, and thus unknown. | ||
[Promises/A+] libraries that passed our compatibility test and are currently supported: | ||
##### connect | ||
* [Promise] (used by default) | ||
* [Bluebird] | ||
* [When] | ||
* [Q] | ||
This property represents a global `connect` event handler: whenever a new connection has been established with the database, | ||
this event function is called: | ||
Compatibility with other [Promises/A+] libraries though possible, is an unknown. | ||
--- | ||
* `connect` | ||
Global notification function of acquiring a new database connection. | ||
```javascript | ||
@@ -382,6 +394,6 @@ var options = { | ||
##### disconnect | ||
--- | ||
* `disconnect` | ||
This property represents a global `disconnect` event handler: whenever a connection is about to be released, | ||
this event function is called: | ||
Global notification function of releasing a database connection. | ||
```javascript | ||
@@ -400,2 +412,23 @@ var options = { | ||
--- | ||
* `query` | ||
Global notification of a query that's being executed. | ||
```javascript | ||
var options = { | ||
query: function(client, query, params){ | ||
console.log("Executing query: " + query); | ||
} | ||
} | ||
``` | ||
It can be useful for diagnostics / logging within your application. | ||
The function receives the following parameters: | ||
* `client` - object from the [PG] library that represents the connection; | ||
* `query` - query that's being executed; | ||
* `params` - `null` by default, it may contains query parameters, if `pgFormatting` was set to be `true`. | ||
Please note, that should you set property `pgFormatting` to be `true`, the library no longer formats | ||
the queries, and the `query` arrives pre-formatted. This is why extra parameter `params` was added. | ||
### Library de-initialization | ||
@@ -410,2 +443,3 @@ When exiting your application, make the following call: | ||
# History | ||
* Version 0.5.1 included wider support for alternative promise libraries. Released: March 12, 2015. | ||
* Version 0.5.0 introduces many new features and fixes, such as properties **pgFormatting** and **promiseLib**. Released on March 11, 2015. | ||
@@ -421,6 +455,8 @@ * Version 0.4.9 represents a solid code base, backed up by comprehensive testing. Released on March 10, 2015. | ||
[PG]:https://github.com/brianc/node-postgres | ||
[Promise]:https://github.com/then/promise | ||
[ConnectionParameters]:https://github.com/brianc/node-postgres/blob/master/lib/connection-parameters.js | ||
[Promises/A+]:https://promisesaplus.com/ | ||
[Promise]:https://github.com/then/promise | ||
[Bluebird]:https://github.com/petkaantonov/bluebird | ||
[When]:https://github.com/cujojs/when | ||
[Q]:https://github.com/kriskowal/q | ||
@@ -427,0 +463,0 @@ # License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51743
762
468