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", "language": "en",
"debugMode": false, "debugMode": false,
"debugLevel": 1, "debugLevel": 1,
"dbType": "redis",
"redisPort": 6379, "redisPort": 6379,
"timezone": "Europe/London", "timezone": "Europe/London",
"repoRoot": "https://github.com/reality/depressionbot/", "repoRoot": "https://github.com/reality/depressionbot/",

View File

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

6
run.js
View File

@ -147,6 +147,7 @@ DBot.prototype.reloadModules = function() {
this.stringMap = {}; this.stringMap = {};
this.usage = {}; this.usage = {};
this.reloadConfig(); this.reloadConfig();
this.ddb.createDB(name, this.config.dbType, {}, function(db) {});
try { try {
this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8')); this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8'));
@ -214,9 +215,6 @@ DBot.prototype.reloadModules = function() {
this.db[dbKey] = {}; this.db[dbKey] = {};
} }
}, this); }, 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.name = name;
module.db = this.ddb.databanks[name]; module.db = this.ddb.databank;
module.config = this.config.modules[name]; module.config = this.config.modules[name];
// Load the module data // Load the module data