all files / keystone/lib/core/ openDatabaseConnection.js

5.13% Statements 2/39
0% Branches 0/18
0% Functions 0/5
5.26% Lines 2/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                                                                                                                                    
var debug = require('debug')('keystone:core:openDatabaseConnection');
 
module.exports = function openDatabaseConnection (callback) {
 
	var keystone = this;
	var mongoConnectionOpen = false;
 
	// support replica sets for mongoose
	if (keystone.get('mongo replica set')) {
 
		if (keystone.get('logger')) {
			console.log('\nWarning: using the `mongo replica set` option has been deprecated and will be removed in'
				+ ' a future version.\nInstead set the `mongo` connection string with your host details, e.g.'
				+ ' mongodb://username:password@host:port,host:port,host:port/database and set any replica set options'
				+ ' in `mongo options`.\n\nRefer to https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html'
				+ ' for more details on the connection settings.');
		}
 
		debug('setting up mongo replica set');
		var replicaData = keystone.get('mongo replica set');
		var replica = '';
 
		var credentials = (replicaData.username && replicaData.password) ? replicaData.username + ':' + replicaData.password + '@' : '';
 
		replicaData.db.servers.forEach(function (server) {
			replica += 'mongodb://' + credentials + server.host + ':' + server.port + '/' + replicaData.db.name + ',';
		});
 
		var options = {
			auth: { authSource: replicaData.authSource },
			replset: {
				rs_name: replicaData.db.replicaSetOptions.rs_name,
				readPreference: replicaData.db.replicaSetOptions.readPreference,
			},
		};
 
		debug('connecting to replicate set');
		keystone.mongoose.connect(replica, options);
 
	} else {
		debug('connecting to mongo');
		keystone.mongoose.connect(keystone.get('mongo'), keystone.get('mongo options'));
	}
 
	keystone.mongoose.connection.on('error', function (err) {
 
		if (keystone.get('logger')) {
			console.log('------------------------------------------------');
			console.log('Mongo Error:\n');
			console.log(err);
		}
 
		if (mongoConnectionOpen) {
			if (err.name === 'ValidationError') return;
			throw err;
		} else {
			throw new Error('KeystoneJS (' + keystone.get('name') + ') failed to start - Check that you are running `mongod` in a separate process.');
		}
 
	}).on('open', function () {
 
		debug('mongo connection open');
		mongoConnectionOpen = true;
 
		var connected = function () {
			if (keystone.get('auto update')) {
				debug('applying auto update');
				keystone.applyUpdates(callback);
			} else {
				callback();
			}
		};
 
		if (keystone.sessionStorePromise) {
			keystone.sessionStorePromise.then(connected);
		} else {
			connected();
		}
 
	});
 
	return this;
};