Skip to content

Commit f888f3b

Browse files
committed
cleanup code
1 parent 844831f commit f888f3b

File tree

1 file changed

+68
-68
lines changed

1 file changed

+68
-68
lines changed

lib/index.js

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,91 @@
11
var EventEmitter = require('events').EventEmitter;
22
var sys = require('sys');
3+
34
var Client = require(__dirname+'/client');
45
var defaults = require(__dirname + '/defaults');
6+
7+
//external genericPool module
58
var genericPool = require('generic-pool');
69

710
//cache of existing client pools
811
var pools = {};
912

10-
//returns connect function using supplied client constructor
11-
var makeConnectFunction = function(pg) {
12-
var ClientConstructor = pg.Client;
13-
return function(config, callback) {
14-
var c = config;
15-
var cb = callback;
16-
//allow for no config to be passed
17-
if(typeof c === 'function') {
18-
cb = c;
19-
c = defaults;
20-
}
21-
//get unique pool name if using a config object instead of config string
22-
var poolName = typeof(c) === 'string' ? c : c.user+c.host+c.port+c.database;
23-
var pool = pools[poolName];
24-
if(pool) return pool.acquire(cb);
25-
var pool = pools[poolName] = genericPool.Pool({
26-
name: poolName,
27-
create: function(callback) {
28-
var client = new ClientConstructor(c);
29-
client.connect();
30-
var connectError = function(err) {
31-
client.removeListener('connect', connectSuccess);
32-
callback(err, null);
33-
};
34-
var connectSuccess = function() {
35-
client.removeListener('error', connectError);
36-
37-
//handle connected client background errors by emitting event
38-
//via the pg object and then removing errored client from the pool
39-
client.on('error', function(e) {
40-
pg.emit('error', e, client);
41-
pool.destroy(client);
42-
});
43-
callback(null, client);
44-
};
45-
client.once('connect', connectSuccess);
46-
client.once('error', connectError);
47-
client.on('drain', function() {
48-
pool.release(client);
49-
});
50-
},
51-
destroy: function(client) {
52-
client.end();
53-
},
54-
max: defaults.poolSize,
55-
idleTimeoutMillis: defaults.poolIdleTimeout
56-
});
57-
return pool.acquire(cb);
58-
}
59-
}
13+
var PG = function(clientConstructor) {
14+
EventEmitter.call(this);
15+
this.Client = clientConstructor;
16+
this.Connection = require(__dirname + '/connection');
17+
this.defaults = defaults;
18+
};
19+
20+
sys.inherits(PG, EventEmitter);
6021

61-
var end = function() {
22+
PG.prototype.end = function() {
6223
Object.keys(pools).forEach(function(name) {
6324
var pool = pools[name];
6425
pool.drain(function() {
6526
pool.destroyAllNow();
6627
});
6728
})
68-
};
29+
}
6930

70-
var PG = function(clientConstructor) {
71-
EventEmitter.call(this);
72-
this.Client = clientConstructor;
73-
this.Connection = require(__dirname + '/connection');
74-
this.connect = makeConnectFunction(this);
75-
this.end = end;
76-
this.defaults = defaults;
77-
};
31+
PG.prototype.connect = function(config, callback) {
32+
var self = this;
33+
var c = config;
34+
var cb = callback;
35+
//allow for no config to be passed
36+
if(typeof c === 'function') {
37+
cb = c;
38+
c = defaults;
39+
}
7840

79-
sys.inherits(PG, EventEmitter);
41+
//get unique pool name even if object was used as config
42+
var poolName = typeof(c) === 'string' ? c : c.user+c.host+c.port+c.database;
43+
var pool = pools[poolName];
44+
45+
if(pool) return pool.acquire(cb);
46+
47+
var pool = pools[poolName] = genericPool.Pool({
48+
name: poolName,
49+
create: function(callback) {
50+
var client = new self.Client(c);
51+
client.connect();
52+
53+
var connectError = function(err) {
54+
client.removeListener('connect', connectSuccess);
55+
callback(err, null);
56+
};
57+
58+
var connectSuccess = function() {
59+
client.removeListener('error', connectError);
60+
61+
//handle connected client background errors by emitting event
62+
//via the pg object and then removing errored client from the pool
63+
client.on('error', function(e) {
64+
self.emit('error', e, client);
65+
pool.destroy(client);
66+
});
67+
callback(null, client);
68+
};
69+
70+
client.once('connect', connectSuccess);
71+
client.once('error', connectError);
72+
client.on('drain', function() {
73+
pool.release(client);
74+
});
75+
},
76+
destroy: function(client) {
77+
client.end();
78+
},
79+
max: defaults.poolSize,
80+
idleTimeoutMillis: defaults.poolIdleTimeout
81+
});
82+
return pool.acquire(cb);
83+
}
8084

8185
module.exports = new PG(Client);
8286

83-
var nativeExport = null;
84-
//lazy require native module...the c++ may not have been compiled
87+
//lazy require native module...the native module may not have installed
8588
module.exports.__defineGetter__("native", function() {
86-
if(nativeExport === null) {
87-
var NativeClient = require(__dirname + '/native');
88-
nativeExport = new PG(NativeClient);
89-
}
90-
return nativeExport;
89+
delete module.exports.native;
90+
return (module.exports.native = new PG(require(__dirname + '/native')));
9191
})

0 commit comments

Comments
 (0)