node-postgres/lib/index.js

107 lines
2.9 KiB
JavaScript
Raw Normal View History

var EventEmitter = require('events').EventEmitter;
2011-10-11 08:40:52 +08:00
var util = require('util');
var Client = require('./client');
var defaults = require('./defaults');
var Connection = require('./connection');
var ConnectionParameters = require('./connection-parameters');
var poolFactory = require('./pool-factory');
2011-08-30 11:48:17 +08:00
var PG = function(clientConstructor) {
EventEmitter.call(this);
this.defaults = defaults;
this.Client = clientConstructor;
this.Query = this.Client.Query;
this.Pool = poolFactory(this.Client);
this._pools = [];
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-30 11:48:17 +08:00
PG.prototype.end = function() {
var self = this;
var keys = Object.keys(this._pools);
var count = keys.length;
if(count === 0) {
self.emit('end');
} else {
keys.forEach(function(key) {
var pool = self._pools[key];
delete self._pools[key];
pool.pool.drain(function() {
pool.pool.destroyAllNow(function() {
count--;
if(count === 0) {
self.emit('end');
}
});
2014-04-17 16:25:59 +08:00
});
});
}
2013-01-21 21:52:00 +08:00
};
2011-08-30 11:48:17 +08:00
PG.prototype.connect = function(config, callback) {
if(typeof config == "function") {
callback = config;
config = null;
}
var poolName = JSON.stringify(config || {});
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;
this._pools[poolName] = this._pools[poolName] || new this.Pool(config);
var pool = this._pools[poolName];
pool.connect(callback);
if(!pool.listeners('error').length) {
//propagate errors up to pg object
pool.on('error', function(e) {
this.emit('error', e, e.client);
}.bind(this));
2011-08-30 11:48:17 +08:00
}
2013-01-21 21:52:00 +08:00
};
// cancel the query running on the given client
2011-11-02 23:07: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);
2013-01-21 21:52:00 +08:00
};
2011-11-02 23:07:14 +08:00
if(typeof process.env.NODE_PG_FORCE_NATIVE != 'undefined') {
module.exports = new PG(require('./native'));
} else {
module.exports = new PG(Client);
2012-09-19 01:30:50 +08:00
//lazy require native module...the native module may not have installed
module.exports.__defineGetter__("native", function() {
delete module.exports.native;
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;
});
}