|
|
@ -1,14 +1,16 @@ |
|
|
|
/*global require,module,process*/ |
|
|
|
/*global require,module,process*/ |
|
|
|
|
|
|
|
|
|
|
|
var winston = require('winston'); |
|
|
|
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));
|
|
|
|
// create table entries (id serial primary key, key varchar(255) not null, value text not null, expiration int, unique(key));
|
|
|
|
|
|
|
|
|
|
|
|
// A postgres document store
|
|
|
|
// A postgres document store
|
|
|
|
var PostgresDocumentStore = function (options) { |
|
|
|
var PostgresDocumentStore = function (options) { |
|
|
|
this.expireJS = options.expire; |
|
|
|
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 = { |
|
|
|
PostgresDocumentStore.prototype = { |
|
|
@ -64,20 +66,15 @@ PostgresDocumentStore.prototype = { |
|
|
|
|
|
|
|
|
|
|
|
// A connection wrapper
|
|
|
|
// A connection wrapper
|
|
|
|
safeConnect: function (callback) { |
|
|
|
safeConnect: function (callback) { |
|
|
|
const client = new Client({connectionString: this.connectionUrl}); |
|
|
|
this.pool.connect((err, client, done) => { |
|
|
|
const connectionPromise = client.connect(); |
|
|
|
if (err) { |
|
|
|
|
|
|
|
winston.error('error connecting to postgres', {error}); |
|
|
|
const done = () => {}; |
|
|
|
callback(error); |
|
|
|
|
|
|
|
} else { |
|
|
|
connectionPromise.then(() => { |
|
|
|
callback(undefined, client, done); |
|
|
|
callback(undefined, client, done); |
|
|
|
} |
|
|
|
}).catch((error) => { |
|
|
|
|
|
|
|
console.log(error); |
|
|
|
|
|
|
|
winston.error('error connecting to postgres', {error}); |
|
|
|
|
|
|
|
callback(error); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports = PostgresDocumentStore; |
|
|
|
module.exports = PostgresDocumentStore; |
|
|
|