Skip to content

Commit ba8151a

Browse files
committed
improving warnings stack + docs.
1 parent 21c8f5b commit ba8151a

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

lib/database.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var $npm = {
1111
task: require('./task')
1212
};
1313

14-
var $arr = require('./array');
14+
var $arr = require('./array'),
15+
EOL = require('os').EOL;
1516

1617
/**
1718
* @constructor Database
@@ -22,16 +23,28 @@ var $arr = require('./array');
2223
*
2324
* **IMPORTANT:**
2425
*
25-
* For any given connection, you should only create a single database object in a separate module,
26-
* to be shared in your application. If instead you keep creating the database object dynamically,
27-
* your application will suffer from loss in performance and memory leaks because of event handlers
28-
* that each database object needs to set up.
26+
* For any given connection, you should only create a single {@link Database} object in a separate module,
27+
* to be shared in your application (see the code example below). If instead you keep creating the {@link Database}
28+
* object dynamically, your application will suffer from loss in performance and memory leaks because of event
29+
* handlers that each {@link Database} object needs to set up.
2930
*
30-
* Starting with version 4.7.0, if you create more than one database object for the same connection,
31-
* you will see the following warning in a development environment (`NODE_ENV` = `development`):
32-
* `WARNING: Creating a duplicate database object for the same connection.`
31+
* **Starting with v.4.7.0:**
32+
*
33+
* If you create more than one {@link Database} object for the same connection, you will see the following warning
34+
* in a development environment (when `NODE_ENV` = `development`):
35+
*
36+
* `WARNING: Creating a duplicate database object for the same connection`.
37+
*
38+
* And since every {@link Database} object needs to set up its own event listeners, you are likely to see the following
39+
* warning in the console also:
40+
*
41+
* `Warning: Possible EventEmitter memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit`.
42+
*
43+
* If you ever see any of those warnings, do not try increasing the limit, this won't help you.
44+
* Instead, rectify your {@link Database} object initialization, so there is only one object per connection details.
45+
* See the example provided below.
3346
*
34-
* See also property `noWarnings` in {@link module:pg-promise Initialization Options}.
47+
* See also: property `noWarnings` in {@link module:pg-promise Initialization Options}.
3548
*
3649
* @param {String|Object} cn
3750
* Database connection details, which can be:
@@ -73,7 +86,7 @@ var $arr = require('./array');
7386
* {@link event:extend extend}
7487
*
7588
* @example
76-
* 'use strict';
89+
* // Proper way to initialize and share the Database object
7790
*
7891
* // Loading and initializing the library:
7992
* var pgp = require('pg-promise')({
@@ -86,7 +99,7 @@ var $arr = require('./array');
8699
* // Creating a new database instance from the connection details:
87100
* var db = pgp(cn);
88101
*
89-
* // Exporting the database object:
102+
* // Exporting the database object for shared use:
90103
* module.exports = db;
91104
*/
92105
function Database(cn, dc, config) {
@@ -1201,8 +1214,11 @@ function checkForDuplicates(cn, config) {
12011214
var cnKey = JSON.stringify(cn);
12021215
if (cnKey in dbObjects) {
12031216
if (!config.options.noWarnings) {
1204-
var msg = "WARNING: Creating a duplicate database object for the same connection.\n" +
1205-
new Error().stack.split('\n').slice(4, 9).join('\n') + '\n';
1217+
var stack = new Error().stack.split('\n').slice(4).filter(function (line) {
1218+
return line.match(/\(.*(\\+|\/+).*\)/);
1219+
}).join('\n');
1220+
var msg = "WARNING: Creating a duplicate database object for the same connection." +
1221+
EOL + stack + EOL;
12061222
$npm.utils.printWarning(msg);
12071223
}
12081224
} else {

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var $npm = {
7171
* @param {boolean} [options.noWarnings]
7272
* **Added in v.4.7.0**
7373
*
74-
* Disables all diagnostic warnings in the library.
74+
* Disables all diagnostic warnings in the library (which is ill-advised).
7575
*
7676
* If it is not set (missing or `undefined`), it will default to the following:
7777
* - `true`, if `NODE_ENV` != `development`

0 commit comments

Comments
 (0)