Skip to content

Commit

Permalink
Merge pull request #40 from martin-krcmar/remove-singleton
Browse files Browse the repository at this point in the history
Singletons removed, not needed anymore
  • Loading branch information
martin-krcmar authored Sep 11, 2018
2 parents 04723af + edb778a commit adca892
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
13 changes: 5 additions & 8 deletions db/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ const Promise = require('bluebird');
const fs = require('fs');
const Logger = require('mongodb').Logger;

const singletons = require('../util/singletons');
const SINGLETON_NAME = 'appmixer-lib.db.db.client';
let db = null;

module.exports.db = function() {

const db = singletons.get(SINGLETON_NAME);
if (!db) {
if (db === null) {
throw new Error('Mongo DB not connected!');
}
return db;
Expand All @@ -39,8 +37,7 @@ module.exports.connect = async function(connection) {
Logger.setLevel(process.env.LOG_LEVEL.toLowerCase());
}

let db = singletons.get(SINGLETON_NAME);
if (db) {
if (db !== null) {
return db;
}

Expand All @@ -56,7 +53,8 @@ module.exports.connect = async function(connection) {
let uri = connection.uri || 'mongodb://' + connection.host + ':' + connection.port + '/' + connection.dbName;

let options = {
promiseLibrary: Promise
promiseLibrary: Promise,
useNewUrlParser: true
};

// file to cert
Expand All @@ -74,6 +72,5 @@ module.exports.connect = async function(connection) {

const client = await MongoClient.connect(uri, options);
db = client.db(connection.dbName);
singletons.set(SINGLETON_NAME, db);
return db;
};
11 changes: 3 additions & 8 deletions db/redis.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
'use strict';

const Promise = require('bluebird');
const redis = Promise.promisifyAll(require('redis'));
const check = require('check-types');
const fs = require('fs');

const SINGLETON_NAME = 'appmixer-lib.db.redis.client';
const singletons = require('../util/singletons');
let client = null;

module.exports.client = function() {

const client = singletons.get(SINGLETON_NAME);
if (!client) {
if (client === null) {
throw new Error('Redis DB not connected!');
}
return client;
Expand All @@ -27,8 +24,7 @@ module.exports.client = function() {
*/
module.exports.connect = async function(connection) {

let client = singletons.get(SINGLETON_NAME);
if (client) {
if (client !== null) {
return client;
}

Expand All @@ -46,6 +42,5 @@ module.exports.connect = async function(connection) {
}

client = connection.uri ? redis.createClient(connection.uri, options) : redis.createClient();
singletons.set(SINGLETON_NAME, client);
return client;
};

0 comments on commit adca892

Please sign in to comment.