You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.0 KiB
JavaScript

11 years ago
11 years ago
'use strict';
11 years ago
(function(module) {
11 years ago
11 years ago
var winston = require('winston'),
async = require('async'),
11 years ago
nconf = require('nconf'),
11 years ago
session = require('express-session'),
11 years ago
db,
mongoClient,
mongoStore;
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',
hidden: true
},
{
name: "mongo:database",
description: "Which database to use",
'default': nconf.get('mongo:database') || 0
}
];
11 years ago
module.init = function(callback) {
try {
mongoClient = require('mongodb').MongoClient;
11 years ago
mongoStore = require('connect-mongo')({session: session});
} catch (err) {
winston.error('Unable to initialize MongoDB! Is MongoDB installed? Error :' + err.message);
process.exit();
}
11 years ago
mongoClient.connect('mongodb://'+ nconf.get('mongo:host') + ':' + nconf.get('mongo:port') + '/' + nconf.get('mongo:database'), function(err, _db) {
if(err) {
winston.error("NodeBB could not connect to your Mongo database. Mongo returned the following error: " + err.message);
process.exit();
}
11 years ago
db = _db;
module.client = db;
module.sessionStore = new mongoStore({
db: db
});
11 years ago
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);
11 years ago
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);
process.exit();
}
createIndices();
11 years ago
});
} else {
winston.warn('You have no mongo password setup!');
createIndices();
}
11 years ago
function createIndices() {
db.collection('objects').ensureIndex({_key :1, score: -1}, {background:true}, function(err) {
if(err) {
winston.error('Error creating index ' + err.message);
}
});
db.collection('objects').ensureIndex({_key :1, value: -1}, {background:true}, function(err) {
if(err) {
winston.error('Error creating index ' + err.message);
}
});
db.collection('objects').ensureIndex({'expireAt':1}, {expireAfterSeconds:0, background:true}, function(err) {
if(err) {
winston.error('Error creating index ' + err.message);
}
});
11 years ago
db.collection('search').ensureIndex({content:'text'}, {background:true}, function(err) {
if(err) {
winston.error('Error creating index ' + err.message);
}
});
if(typeof callback === 'function') {
11 years ago
callback();
}
}
});
11 years ago
};
11 years ago
module.close = function() {
db.close();
};
module.helpers = module.helpers || {};
module.helpers.mongo = require('./mongo/helpers');
11 years ago
}(exports));