Skip to content

Commit 3ae70bf

Browse files
authored
Merge pull request #5 from billwashere/pooling
Add Connection Pooling
2 parents f61be56 + 39a0c15 commit 3ae70bf

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ function PostgresDB(options) {
99

1010
this.closed = false;
1111

12-
this.pg_config = options;
12+
this.pool = new pg.Pool(options);
1313
};
1414
module.exports = PostgresDB;
1515

1616
PostgresDB.prototype = Object.create(DB.prototype);
1717

1818
PostgresDB.prototype.close = function(callback) {
1919
this.closed = true;
20+
this.pool.end();
21+
2022
if (callback) callback();
2123
};
2224

@@ -39,7 +41,7 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca
3941
* }
4042
* snapshot: PostgresSnapshot
4143
*/
42-
pg.connect(this.pg_config, function(err, client, done) {
44+
this.pool.connect(function(err, client, done) {
4345
if (err) {
4446
done(client);
4547
callback(err);
@@ -123,7 +125,7 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca
123125
// snapshot). A snapshot with a version of zero is returned if the docuemnt
124126
// has never been created in the database.
125127
PostgresDB.prototype.getSnapshot = function(collection, id, fields, options, callback) {
126-
pg.connect(this.pg_config, function(err, client, done) {
128+
this.pool.connect(function(err, client, done) {
127129
if (err) {
128130
done(client);
129131
callback(err);
@@ -173,7 +175,7 @@ PostgresDB.prototype.getSnapshot = function(collection, id, fields, options, cal
173175
//
174176
// Callback should be called as callback(error, [list of ops]);
175177
PostgresDB.prototype.getOps = function(collection, id, from, to, options, callback) {
176-
pg.connect(this.pg_config, function(err, client, done) {
178+
this.pool.connect(function(err, client, done) {
177179
if (err) {
178180
done(client);
179181
callback(err);

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"keywords": ["sharedb", "sharejs", "share", "postgres"],
1212
"repository": "share/sharedb-postgres",
1313
"dependencies": {
14-
"pg": "^4.5.1",
14+
"pg": "^7.4.1",
1515
"sharedb": "^1.0.0-beta.7"
1616
}
17-
}
17+
}

0 commit comments

Comments
 (0)