From 6d97c4cec8d16dd5f63b1f81d2d24fd470d60328 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 14 Oct 2016 22:18:28 +0300 Subject: [PATCH] up mongo deps, create session store after db.authenticate --- package.json | 4 +-- src/database/mongo.js | 63 +++++++++++++++++++++---------------------- src/database/redis.js | 2 +- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index b92da13af7..d2ed886a91 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "compression": "^1.1.0", "connect-ensure-login": "^0.1.1", "connect-flash": "^0.1.1", - "connect-mongo": "~1.1.0", + "connect-mongo": "1.3.2", "connect-multiparty": "^2.0.0", "connect-redis": "~3.1.0", "cookie-parser": "^1.3.3", @@ -48,7 +48,7 @@ "mime": "^1.3.4", "minimist": "^1.1.1", "mkdirp": "~0.5.0", - "mongodb": "~2.1.3", + "mongodb": "2.2.10", "morgan": "^1.3.2", "mousetrap": "^1.5.3", "nconf": "~0.8.2", diff --git a/src/database/mongo.js b/src/database/mongo.js index 92bfd264dc..b5af8b57ab 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -3,13 +3,13 @@ (function (module) { - var winston = require('winston'), - async = require('async'), - nconf = require('nconf'), - session = require('express-session'), - _ = require('underscore'), - semver = require('semver'), - db, mongoClient; + var winston = require('winston'); + var async = require('async'); + var nconf = require('nconf'); + var session = require('express-session'); + var _ = require('underscore'); + var semver = require('semver'); + var db; _.mixin(require('underscore.deep')); @@ -48,15 +48,9 @@ module.init = function (callback) { callback = callback || function () {}; + var mongoClient; try { - var sessionStore; mongoClient = require('mongodb').MongoClient; - - if (!nconf.get('redis')) { - sessionStore = require('connect-mongo/es5')(session); - } else { - sessionStore = require('connect-redis')(session); - } } catch (err) { winston.error('Unable to initialize MongoDB! Is MongoDB installed? Error :' + err.message); return callback(err); @@ -106,22 +100,6 @@ module.client = db; - if (!nconf.get('redis')) { - module.sessionStore = new sessionStore({ - db: db - }); - } else { - // Initial Redis database - var rdb = require('./redis'); - // Create a new redis connection and store it in module (skeleton) - rdb.client = rdb.connect(); - - module.sessionStore = new sessionStore({ - client: rdb.client, - ttl: 60 * 60 * 24 * 14 - }); - } - require('./mongo/main')(db, module); require('./mongo/hash')(db, module); require('./mongo/sets')(db, module); @@ -131,16 +109,36 @@ if (nconf.get('mongo:password') && nconf.get('mongo:username')) { db.authenticate(nconf.get('mongo:username'), nconf.get('mongo:password'), function (err) { if (err) { - winston.error(err.stack); - process.exit(); + return callback(err); } + createSessionStore(); createIndices(); }); } else { winston.warn('You have no mongo password setup!'); + createSessionStore(); createIndices(); } + function createSessionStore() { + var sessionStore; + if (nconf.get('redis')) { + sessionStore = require('connect-redis')(session); + var rdb = require('./redis'); + rdb.client = rdb.connect(); + + module.sessionStore = new sessionStore({ + client: rdb.client, + ttl: 60 * 60 * 24 * 14 + }); + } else if (nconf.get('mongo')) { + sessionStore = require('connect-mongo')(session); + module.sessionStore = new sessionStore({ + db: db + }); + } + } + function createIndices() { winston.info('[database] Checking database indices.'); async.parallel([ @@ -151,6 +149,7 @@ if (err) { winston.error('Error creating index ' + err.message); } + winston.info('[database] Checking database indices done!'); callback(err); }); } diff --git a/src/database/redis.js b/src/database/redis.js index 3b05148c9e..46c1748fc3 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -59,7 +59,7 @@ require('./redis/sorted')(redisClient, module); require('./redis/list')(redisClient, module); - if(typeof callback === 'function') { + if (typeof callback === 'function') { callback(); } };