From d4ab7ccea506846fd658266a086921c24c57f8fc Mon Sep 17 00:00:00 2001 From: William Claydon Date: Sat, 17 Mar 2018 17:02:26 +1100 Subject: [PATCH 1/2] Add Connection Pooling --- index.js | 9 ++++++--- package.json | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 3f7dc57..4a3cbbf 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ function PostgresDB(options) { this.closed = false; this.pg_config = options; + this.pool = new pg.Pool(this.pg_config) }; module.exports = PostgresDB; @@ -17,6 +18,8 @@ PostgresDB.prototype = Object.create(DB.prototype); PostgresDB.prototype.close = function(callback) { this.closed = true; + this.pool.end() + if (callback) callback(); }; @@ -39,7 +42,7 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca * } * snapshot: PostgresSnapshot */ - pg.connect(this.pg_config, function(err, client, done) { + this.pool.connect(function(err, client, done) { if (err) { done(client); callback(err); @@ -123,7 +126,7 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca // snapshot). A snapshot with a version of zero is returned if the docuemnt // has never been created in the database. PostgresDB.prototype.getSnapshot = function(collection, id, fields, options, callback) { - pg.connect(this.pg_config, function(err, client, done) { + this.pool.connect(function(err, client, done) { if (err) { done(client); callback(err); @@ -173,7 +176,7 @@ PostgresDB.prototype.getSnapshot = function(collection, id, fields, options, cal // // Callback should be called as callback(error, [list of ops]); PostgresDB.prototype.getOps = function(collection, id, from, to, options, callback) { - pg.connect(this.pg_config, function(err, client, done) { + this.pool.connect(function(err, client, done) { if (err) { done(client); callback(err); diff --git a/package.json b/package.json index 3b1f446..bd89bc9 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "keywords": ["sharedb", "sharejs", "share", "postgres"], "repository": "share/sharedb-postgres", "dependencies": { - "pg": "^4.5.1", + "pg": "^7.4.1", "sharedb": "^1.0.0-beta.7" } -} +} \ No newline at end of file From 39a0c15f1cc84e781402553518cea79bea1a75f3 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Mon, 19 Mar 2018 20:51:13 -0700 Subject: [PATCH 2/2] Minor cleanup Added some semicolons, also PostgresDB.pg_config no longer needs to be saved --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4a3cbbf..442bd84 100644 --- a/index.js +++ b/index.js @@ -9,8 +9,7 @@ function PostgresDB(options) { this.closed = false; - this.pg_config = options; - this.pool = new pg.Pool(this.pg_config) + this.pool = new pg.Pool(options); }; module.exports = PostgresDB; @@ -18,7 +17,7 @@ PostgresDB.prototype = Object.create(DB.prototype); PostgresDB.prototype.close = function(callback) { this.closed = true; - this.pool.end() + this.pool.end(); if (callback) callback(); };