Skip to content

Commit 8394f53

Browse files
committed
Support ending single pool
Call `pg.end(connString)` to do it.
1 parent 8fc2aaf commit 8394f53

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

lib/index.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,37 @@ var PG = function(clientConstructor) {
1717

1818
util.inherits(PG, EventEmitter);
1919

20-
PG.prototype.end = function() {
20+
PG.prototype.end = function(config) {
2121
var self = this;
22-
var keys = Object.keys(self.pools.all);
22+
var pool;
23+
if (config) {
24+
pool = self.pools.get(config);
25+
if (pool) pool.end();
26+
} else {
27+
forEachOf(self.pools.all, function (pool, name, callback) {
28+
pool.end(callback);
29+
}, function () {
30+
self.emit('end');
31+
});
32+
}
33+
};
34+
35+
function forEachOf(object, iterator, callback) {
36+
var keys = Object.keys(object);
2337
var count = keys.length;
24-
if(count === 0) {
25-
self.emit('end');
38+
if (count === 0) {
39+
callback();
2640
} else {
27-
keys.forEach(function(key) {
28-
var pool = self.pools.all[key];
29-
delete self.pools.all[key];
30-
pool.drain(function() {
31-
pool.destroyAllNow(function() {
32-
count--;
33-
if(count === 0) {
34-
self.emit('end');
35-
}
36-
});
41+
keys.forEach(function (key) {
42+
iterator(object[key], key, function () {
43+
count--;
44+
if (count === 0) {
45+
callback();
46+
}
3747
});
3848
});
3949
}
40-
};
50+
}
4151

4252

4353
PG.prototype.connect = function(config, callback) {

lib/pool.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ var pools = {
88
all: {},
99
//reference to the client constructor - can override in tests or for require('pg').native
1010
Client: require(__dirname + '/client'),
11+
get: function(clientConfig) {
12+
var name = JSON.stringify(clientConfig || {});
13+
return pools.all[name];
14+
},
1115
getOrCreate: function(clientConfig) {
1216
clientConfig = clientConfig || {};
1317
var name = JSON.stringify(clientConfig);
@@ -83,6 +87,12 @@ var pools = {
8387
});
8488
});
8589
};
90+
pool.end = function(cb) {
91+
delete pools.all[name];
92+
pool.drain(function() {
93+
pool.destroyAllNow(cb);
94+
});
95+
};
8696
return pool;
8797
}
8898
};

0 commit comments

Comments
 (0)