From f147acb51cf9d62993fe2337406dfa7f4f8bc300 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 6 Oct 2020 20:58:33 -0400 Subject: [PATCH] Switch to using pg.Pool --- lib/document_stores/postgres.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/document_stores/postgres.js b/lib/document_stores/postgres.js index bda7e83..83e13f0 100644 --- a/lib/document_stores/postgres.js +++ b/lib/document_stores/postgres.js @@ -1,14 +1,16 @@ /*global require,module,process*/ var winston = require('winston'); -const {Client} = require('pg'); +const {Pool} = require('pg'); // create table entries (id serial primary key, key varchar(255) not null, value text not null, expiration int, unique(key)); // A postgres document store var PostgresDocumentStore = function (options) { this.expireJS = options.expire; - this.connectionUrl = process.env.DATABASE_URL || options.connectionUrl; + + const connectionString = process.env.DATABASE_URL || options.connectionUrl; + this.pool = new Pool({connectionString}); }; PostgresDocumentStore.prototype = { @@ -64,20 +66,15 @@ PostgresDocumentStore.prototype = { // A connection wrapper safeConnect: function (callback) { - const client = new Client({connectionString: this.connectionUrl}); - const connectionPromise = client.connect(); - - const done = () => {}; - - connectionPromise.then(() => { - callback(undefined, client, done); - }).catch((error) => { - console.log(error); - winston.error('error connecting to postgres', {error}); - callback(error); + this.pool.connect((err, client, done) => { + if (err) { + winston.error('error connecting to postgres', {error}); + callback(error); + } else { + callback(undefined, client, done); + } }); } - }; module.exports = PostgresDocumentStore;