@@ -11,7 +11,8 @@ var $npm = {
11
11
task : require ( './task' )
12
12
} ;
13
13
14
- var $arr = require ( './array' ) ;
14
+ var $arr = require ( './array' ) ,
15
+ EOL = require ( 'os' ) . EOL ;
15
16
16
17
/**
17
18
* @constructor Database
@@ -22,16 +23,28 @@ var $arr = require('./array');
22
23
*
23
24
* **IMPORTANT:**
24
25
*
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.
29
30
*
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.
33
46
*
34
- * See also property `noWarnings` in {@link module:pg-promise Initialization Options}.
47
+ * See also: property `noWarnings` in {@link module:pg-promise Initialization Options}.
35
48
*
36
49
* @param {String|Object } cn
37
50
* Database connection details, which can be:
@@ -73,7 +86,7 @@ var $arr = require('./array');
73
86
* {@link event:extend extend}
74
87
*
75
88
* @example
76
- * 'use strict';
89
+ * // Proper way to initialize and share the Database object
77
90
*
78
91
* // Loading and initializing the library:
79
92
* var pgp = require('pg-promise')({
@@ -86,7 +99,7 @@ var $arr = require('./array');
86
99
* // Creating a new database instance from the connection details:
87
100
* var db = pgp(cn);
88
101
*
89
- * // Exporting the database object:
102
+ * // Exporting the database object for shared use :
90
103
* module.exports = db;
91
104
*/
92
105
function Database ( cn , dc , config ) {
@@ -1201,8 +1214,11 @@ function checkForDuplicates(cn, config) {
1201
1214
var cnKey = JSON . stringify ( cn ) ;
1202
1215
if ( cnKey in dbObjects ) {
1203
1216
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 ;
1206
1222
$npm . utils . printWarning ( msg ) ;
1207
1223
}
1208
1224
} else {
0 commit comments