mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 02:25:55 +01:00
fix: redis pubsub not being required correctly
split connection logic into separate module
This commit is contained in:
45
src/database/postgres/connection.js
Normal file
45
src/database/postgres/connection.js
Normal 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);
|
||||
});
|
||||
};
|
||||
@@ -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)`,
|
||||
|
||||
Reference in New Issue
Block a user