fix: redis pubsub not being required correctly

split connection logic into separate module
This commit is contained in:
Barış Soner Uşaklı
2019-08-06 18:27:00 -04:00
parent 186321e646
commit 8d4f20865f
9 changed files with 239 additions and 213 deletions

View File

@@ -0,0 +1,45 @@
'use strict';
const nconf = require('nconf');
const winston = require('winston');
const _ = require('lodash');
const connection = module.exports;
connection.getConnectionOptions = function (postgres) {
postgres = postgres || nconf.get('postgres');
// Sensible defaults for PostgreSQL, if not set
if (!postgres.host) {
postgres.host = '127.0.0.1';
}
if (!postgres.port) {
postgres.port = 5432;
}
const dbName = postgres.database;
if (dbName === undefined || dbName === '') {
winston.warn('You have no database name, using "nodebb"');
postgres.database = 'nodebb';
}
var connOptions = {
host: postgres.host,
port: postgres.port,
user: postgres.username,
password: postgres.password,
database: postgres.database,
};
return _.merge(connOptions, postgres.options || {});
};
connection.connect = function (options, callback) {
const Pool = require('pg').Pool;
const connOptions = connection.getConnectionOptions(options);
const db = new Pool(connOptions);
db.connect(function (err) {
callback(err, db);
});
};

View File

@@ -1,15 +1,15 @@
'use strict';
var util = require('util');
var winston = require('winston');
var EventEmitter = require('events').EventEmitter;
var pg = require('pg');
var db = require('../postgres');
const util = require('util');
const winston = require('winston');
const EventEmitter = require('events').EventEmitter;
const pg = require('pg');
const connection = require('./connection');
var PubSub = function () {
var self = this;
const PubSub = function () {
const self = this;
var subClient = new pg.Client(db.getConnectionOptions());
const subClient = new pg.Client(connection.getConnectionOptions());
subClient.connect(function (err) {
if (err) {
@@ -41,6 +41,7 @@ var PubSub = function () {
util.inherits(PubSub, EventEmitter);
PubSub.prototype.publish = function (event, data) {
const db = require('../postgres');
db.pool.query({
name: 'pubSubPublish',
text: `SELECT pg_notify('pubsub', $1::TEXT)`,