Skip to content

Commit d4ab7cc

Browse files
committed
Add Connection Pooling
1 parent f61be56 commit d4ab7cc

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ function PostgresDB(options) {
1010
this.closed = false;
1111

1212
this.pg_config = options;
13+
this.pool = new pg.Pool(this.pg_config)
1314
};
1415
module.exports = PostgresDB;
1516

1617
PostgresDB.prototype = Object.create(DB.prototype);
1718

1819
PostgresDB.prototype.close = function(callback) {
1920
this.closed = true;
21+
this.pool.end()
22+
2023
if (callback) callback();
2124
};
2225

@@ -39,7 +42,7 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca
3942
* }
4043
* snapshot: PostgresSnapshot
4144
*/
42-
pg.connect(this.pg_config, function(err, client, done) {
45+
this.pool.connect(function(err, client, done) {
4346
if (err) {
4447
done(client);
4548
callback(err);
@@ -123,7 +126,7 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca
123126
// snapshot). A snapshot with a version of zero is returned if the docuemnt
124127
// has never been created in the database.
125128
PostgresDB.prototype.getSnapshot = function(collection, id, fields, options, callback) {
126-
pg.connect(this.pg_config, function(err, client, done) {
129+
this.pool.connect(function(err, client, done) {
127130
if (err) {
128131
done(client);
129132
callback(err);
@@ -173,7 +176,7 @@ PostgresDB.prototype.getSnapshot = function(collection, id, fields, options, cal
173176
//
174177
// Callback should be called as callback(error, [list of ops]);
175178
PostgresDB.prototype.getOps = function(collection, id, from, to, options, callback) {
176-
pg.connect(this.pg_config, function(err, client, done) {
179+
this.pool.connect(function(err, client, done) {
177180
if (err) {
178181
done(client);
179182
callback(err);

package.json

Lines changed: 2 additions & 2 deletions
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)