only one connection to the database across all modules. master db driver in dbType of main config.json

This commit is contained in:
reality 2015-02-09 16:54:59 +00:00
parent 76a59dee55
commit 10c300ff2b
3 changed files with 7 additions and 12 deletions

View File

@ -19,6 +19,7 @@
"language": "en",
"debugMode": false,
"debugLevel": 1,
"dbType": "redis",
"redisPort": 6379,
"timezone": "Europe/London",
"repoRoot": "https://github.com/reality/depressionbot/",

View File

@ -8,30 +8,26 @@ var databank = require('databank'),
*/
var DatabaseDriver = function(config) {
this.config = config;
this.databanks = {};
this.databank = null;
};
/**
* Connect to or create a new DataBank
*/
DatabaseDriver.prototype.createDB = function(name, driver, schema, callback) {
if(!_.has(this.databanks, name)) {
var params = { 'schema': schema };
if(driver == 'redis' && _.has(this.config, 'redisPort')) params.port = this.config.redisPort;
if(driver == 'disk') params.dir = 'db';
this.databanks[name] = Databank.get(driver, params);
this.databanks[name].connect({}, function(err) {
this.databank = Databank.get(driver, params);
this.databank.connect({}, function(err) {
if(err) {
console.log('Didn\'t manage to connect to the data source - ' + err);
} else {
callback(this.databanks[name]);
callback(this.databank);
}
}.bind(this));
} else {
callback(this.databanks[name]);
}
};
exports.DatabaseDriver = DatabaseDriver;

6
run.js
View File

@ -147,6 +147,7 @@ DBot.prototype.reloadModules = function() {
this.stringMap = {};
this.usage = {};
this.reloadConfig();
this.ddb.createDB(name, this.config.dbType, {}, function(db) {});
try {
this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8'));
@ -214,9 +215,6 @@ DBot.prototype.reloadModules = function() {
this.db[dbKey] = {};
}
}, this);
} else {
// Just use the name of the module for now, add dbKey iteration later
this.ddb.createDB(name, config.dbType, {}, function(db) {});
}
}
@ -235,7 +233,7 @@ DBot.prototype.reloadModules = function() {
}
module.name = name;
module.db = this.ddb.databanks[name];
module.db = this.ddb.databank;
module.config = this.config.modules[name];
// Load the module data