| 
									
										
										
										
											2013-12-02 16:19:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2013-12-02 16:19:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(module) { | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 14:16:04 -05:00
										 |  |  | 	var winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2013-12-04 15:11:17 -05:00
										 |  |  | 		async = require('async'), | 
					
						
							| 
									
										
										
										
											2013-12-02 16:19:30 -05:00
										 |  |  | 		nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2013-12-02 22:48:32 -05:00
										 |  |  | 		express = require('express'), | 
					
						
							| 
									
										
										
										
											2013-12-09 14:16:04 -05:00
										 |  |  | 		db, | 
					
						
							|  |  |  | 		mongoClient, | 
					
						
							|  |  |  | 		mongoStore; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 13:41:33 -04:00
										 |  |  | 	module.questions = [ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: 'mongo:host', | 
					
						
							|  |  |  | 			description: 'Host IP or address of your MongoDB instance', | 
					
						
							|  |  |  | 			'default': nconf.get('mongo:host') || '127.0.0.1' | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: 'mongo:port', | 
					
						
							|  |  |  | 			description: 'Host port of your MongoDB instance', | 
					
						
							|  |  |  | 			'default': nconf.get('mongo:port') || 27017 | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: 'mongo:username', | 
					
						
							|  |  |  | 			description: 'MongoDB username' | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: 'mongo:password', | 
					
						
							|  |  |  | 			description: 'Password of your MongoDB database' | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "mongo:database", | 
					
						
							|  |  |  | 			description: "Which database to use", | 
					
						
							|  |  |  | 			'default': nconf.get('mongo:database') || 0 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	]; | 
					
						
							| 
									
										
										
										
											2013-12-02 16:19:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 	module.init = function(callback) { | 
					
						
							| 
									
										
										
										
											2014-04-14 13:41:33 -04:00
										 |  |  | 		try { | 
					
						
							|  |  |  | 			mongoClient = require('mongodb').MongoClient; | 
					
						
							|  |  |  | 			mongoStore = require('connect-mongo')(express); | 
					
						
							|  |  |  | 		} catch (err) { | 
					
						
							|  |  |  | 			winston.error('Unable to initialize MongoDB! Is MongoDB installed? Error :' + err.message); | 
					
						
							|  |  |  | 			process.exit(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-04-14 13:51:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 14:16:04 -05:00
										 |  |  | 		mongoClient.connect('mongodb://'+ nconf.get('mongo:host') + ':' + nconf.get('mongo:port') + '/' + nconf.get('mongo:database'), function(err, _db) { | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				winston.error("NodeBB could not connect to your Mongo database. Mongo returned the following error: " + err.message); | 
					
						
							|  |  |  | 				process.exit(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-03 13:36:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:25 -05:00
										 |  |  | 			db = _db; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 14:22:31 -05:00
										 |  |  | 			module.client = db; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 			module.sessionStore = new mongoStore({ | 
					
						
							|  |  |  | 				db: db | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-12-03 13:36:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 			require('./mongo/main')(db, module); | 
					
						
							|  |  |  | 			require('./mongo/hash')(db, module); | 
					
						
							|  |  |  | 			require('./mongo/sets')(db, module); | 
					
						
							|  |  |  | 			require('./mongo/sorted')(db, module); | 
					
						
							|  |  |  | 			require('./mongo/list')(db, module); | 
					
						
							| 
									
										
										
										
											2013-12-03 13:36:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 14:31:11 -05:00
										 |  |  | 			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.message); | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:25 -05:00
										 |  |  | 						process.exit(); | 
					
						
							| 
									
										
										
										
											2013-12-06 14:31:11 -05:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-01-05 19:08:36 -05:00
										 |  |  | 					createIndices(); | 
					
						
							| 
									
										
										
										
											2013-12-05 21:07:35 -05:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:25 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2014-02-08 14:05:45 -05:00
										 |  |  | 				winston.warn('You have no mongo password setup!'); | 
					
						
							| 
									
										
										
										
											2014-01-05 19:08:36 -05:00
										 |  |  | 				createIndices(); | 
					
						
							| 
									
										
										
										
											2013-12-06 14:31:11 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-05 21:07:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-05 19:08:36 -05:00
										 |  |  | 			function createIndices() { | 
					
						
							|  |  |  | 				db.collection('objects').ensureIndex({_key :1}, {background:true}, function(err) { | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:25 -05:00
										 |  |  | 					if(err) { | 
					
						
							| 
									
										
										
										
											2014-01-05 19:08:36 -05:00
										 |  |  | 						winston.error('Error creating index ' + err.message); | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:25 -05:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-01-05 19:08:36 -05:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-12-31 19:08:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-05 19:08:36 -05:00
										 |  |  | 				db.collection('objects').ensureIndex({'expireAt':1}, {expireAfterSeconds:0, background:true}, function(err) { | 
					
						
							|  |  |  | 					if(err) { | 
					
						
							|  |  |  | 						winston.error('Error creating index ' + err.message); | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:25 -05:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-12-05 21:07:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-05 19:08:36 -05:00
										 |  |  | 				db.collection('search').ensureIndex({content:'text'}, {background:true}, function(err) { | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:25 -05:00
										 |  |  | 					if(err) { | 
					
						
							| 
									
										
										
										
											2014-01-05 19:08:36 -05:00
										 |  |  | 						winston.error('Error creating index ' + err.message); | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:25 -05:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-12-05 20:06:36 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-07 13:25:14 -05:00
										 |  |  | 				if(typeof callback === 'function') { | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 					callback(); | 
					
						
							| 
									
										
										
										
											2014-02-07 13:25:14 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:25 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-12-02 22:48:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 13:51:45 -04:00
										 |  |  | 	module.close = function() { | 
					
						
							|  |  |  | 		db.close(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	module.helpers = module.helpers || {}; | 
					
						
							| 
									
										
										
										
											2014-04-14 17:09:03 -04:00
										 |  |  | 	module.helpers.mongo = require('./mongo/helpers'); | 
					
						
							| 
									
										
										
										
											2013-12-02 16:19:30 -05:00
										 |  |  | }(exports)); | 
					
						
							|  |  |  | 
 |