2016-06-24 23:56:43 +08:00
|
|
|
/**
|
2017-05-18 05:47:07 +08:00
|
|
|
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
2016-06-24 23:56:43 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* README.md file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2010-09-29 12:18:46 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-10-11 08:40:52 +08:00
|
|
|
var util = require('util');
|
2015-10-10 17:10:51 +08:00
|
|
|
var Client = require('./client');
|
|
|
|
var defaults = require('./defaults');
|
|
|
|
var Connection = require('./connection');
|
2016-06-21 22:53:09 +08:00
|
|
|
var ConnectionParameters = require('./connection-parameters');
|
2016-06-24 13:52:28 +08:00
|
|
|
var poolFactory = require('./pool-factory');
|
2011-08-12 09:59:56 +08:00
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
var PG = function(clientConstructor) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
this.defaults = defaults;
|
2016-04-09 07:46:10 +08:00
|
|
|
this.Client = clientConstructor;
|
2013-02-21 06:08:48 +08:00
|
|
|
this.Query = this.Client.Query;
|
2016-06-24 13:52:28 +08:00
|
|
|
this.Pool = poolFactory(this.Client);
|
2016-06-22 11:03:25 +08:00
|
|
|
this._pools = [];
|
2013-02-21 06:08:48 +08:00
|
|
|
this.Connection = Connection;
|
2014-03-16 04:41:36 +08:00
|
|
|
this.types = require('pg-types');
|
2011-08-30 11:48:17 +08:00
|
|
|
};
|
|
|
|
|
2011-10-11 08:40:52 +08:00
|
|
|
util.inherits(PG, EventEmitter);
|
2011-08-12 09:59:56 +08:00
|
|
|
|
2018-05-23 22:13:14 +08:00
|
|
|
PG.prototype.end = function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
var self = this;
|
2016-06-22 11:03:25 +08:00
|
|
|
var keys = Object.keys(this._pools);
|
2014-04-17 16:18:49 +08:00
|
|
|
var count = keys.length;
|
2014-05-13 19:57:01 +08:00
|
|
|
if(count === 0) {
|
|
|
|
self.emit('end');
|
|
|
|
} else {
|
|
|
|
keys.forEach(function(key) {
|
2016-06-22 11:03:25 +08:00
|
|
|
var pool = self._pools[key];
|
|
|
|
delete self._pools[key];
|
2016-06-21 22:53:09 +08:00
|
|
|
pool.pool.drain(function() {
|
|
|
|
pool.pool.destroyAllNow(function() {
|
2014-05-13 19:57:01 +08:00
|
|
|
count--;
|
|
|
|
if(count === 0) {
|
|
|
|
self.emit('end');
|
|
|
|
}
|
|
|
|
});
|
2014-04-17 16:25:59 +08:00
|
|
|
});
|
2011-08-12 09:59:56 +08:00
|
|
|
});
|
2014-05-13 19:57:01 +08:00
|
|
|
}
|
2018-05-23 22:13:14 +08:00
|
|
|
};
|
2011-03-12 02:29:27 +08:00
|
|
|
|
2018-05-23 22:13:14 +08:00
|
|
|
PG.prototype.connect = function(config, callback) {
|
2013-02-21 06:08:48 +08:00
|
|
|
if(typeof config == "function") {
|
|
|
|
callback = config;
|
|
|
|
config = null;
|
|
|
|
}
|
2016-06-21 22:53:09 +08:00
|
|
|
if (typeof config == 'string') {
|
|
|
|
config = new ConnectionParameters(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
//for backwards compatibility
|
|
|
|
config.max = config.max || config.poolSize || defaults.poolSize;
|
|
|
|
config.idleTimeoutMillis = config.idleTimeoutMillis || config.poolIdleTimeout || defaults.poolIdleTimeout;
|
|
|
|
config.log = config.log || config.poolLog || defaults.poolLog;
|
|
|
|
|
2017-05-24 22:04:50 +08:00
|
|
|
var poolName = JSON.stringify(config);
|
2016-06-24 13:52:28 +08:00
|
|
|
this._pools[poolName] = this._pools[poolName] || new this.Pool(config);
|
2016-06-22 11:03:25 +08:00
|
|
|
var pool = this._pools[poolName];
|
2013-02-21 06:08:48 +08:00
|
|
|
if(!pool.listeners('error').length) {
|
|
|
|
//propagate errors up to pg object
|
2016-06-21 22:53:09 +08:00
|
|
|
pool.on('error', function(e) {
|
|
|
|
this.emit('error', e, e.client);
|
|
|
|
}.bind(this));
|
2011-08-30 11:48:17 +08:00
|
|
|
}
|
2016-07-19 23:16:48 +08:00
|
|
|
return pool.connect(callback);
|
2018-05-23 22:13:14 +08:00
|
|
|
};
|
2011-08-29 15:35:46 +08:00
|
|
|
|
2016-06-24 13:52:28 +08:00
|
|
|
// cancel the query running on the given client
|
2018-05-23 22:13:14 +08:00
|
|
|
PG.prototype.cancel = function(config, client, query) {
|
2014-10-10 09:12:17 +08:00
|
|
|
if(client.native) {
|
|
|
|
return client.cancel(query);
|
|
|
|
}
|
2011-11-02 23:07:14 +08:00
|
|
|
var c = config;
|
|
|
|
//allow for no config to be passed
|
2013-01-21 21:52:00 +08:00
|
|
|
if(typeof c === 'function') {
|
2011-11-02 23:07:14 +08:00
|
|
|
c = defaults;
|
2013-01-21 21:52:00 +08:00
|
|
|
}
|
2011-11-02 23:07:14 +08:00
|
|
|
var cancellingClient = new this.Client(c);
|
|
|
|
cancellingClient.cancel(client, query);
|
2018-05-23 22:13:14 +08:00
|
|
|
};
|
2011-11-02 23:07:14 +08:00
|
|
|
|
2014-10-18 12:20:18 +08:00
|
|
|
if(typeof process.env.NODE_PG_FORCE_NATIVE != 'undefined') {
|
|
|
|
module.exports = new PG(require('./native'));
|
2013-06-27 04:32:07 +08:00
|
|
|
} else {
|
|
|
|
module.exports = new PG(Client);
|
2012-09-19 01:30:50 +08:00
|
|
|
|
2013-06-27 04:32:07 +08:00
|
|
|
//lazy require native module...the native module may not have installed
|
|
|
|
module.exports.__defineGetter__("native", function() {
|
|
|
|
delete module.exports.native;
|
2016-06-08 05:02:07 +08:00
|
|
|
var native = null;
|
|
|
|
try {
|
|
|
|
native = new PG(require('./native'));
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
|
|
|
module.exports.native = native;
|
|
|
|
return native;
|
2013-06-27 04:32:07 +08:00
|
|
|
});
|
|
|
|
}
|