From b288487f239f0787a30b868431feffab227d8df4 Mon Sep 17 00:00:00 2001 From: reality Date: Tue, 12 Feb 2013 19:15:32 +0000 Subject: [PATCH] Provisional framework for database module using databank multiplexing. [#272] --- database.js | 28 ++++++++++++++++++++++++++++ run.js | 28 ++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 database.js diff --git a/database.js b/database.js new file mode 100644 index 0000000..4f191ea --- /dev/null +++ b/database.js @@ -0,0 +1,28 @@ +var databank = require('databank'), + Databank = databank.Databank, + DatabankObject = databank.DatabankObject; + //DatabankStore = require('connect-databank')(express); + +/** + * Multiplex databank objects + */ +var DatabaseDriver = function() { + this.databanks = {}; +}; + +/** + * Connect to or create a new DataBank + */ +DatabaseDriver.prototype.createDB = function(name, driver, callback, schema) { + var params = { 'schema': schema }; + this.databanks[name] = Databank.get(driver, params); + this.databanks[name].connect({}, function(err) { + if(err) { + console.log('Didn\'t manage to connect to the data source.'); + } else { + callback(this.databanks[name]); + } + }.bind(this)); +}; + +exports.DatabaseDriver = DatabaseDriver; diff --git a/run.js b/run.js index 48ad4d5..a5296ad 100644 --- a/run.js +++ b/run.js @@ -1,6 +1,7 @@ var fs = require('fs'), _ = require('underscore')._, - jsbot = require('./jsbot/jsbot'); + jsbot = require('./jsbot/jsbot'), + DatabaseDriver = require('./database').DatabaseDriver; require('./snippets'); var DBot = function() { @@ -22,6 +23,9 @@ var DBot = function() { this.db.config = {}; } + /*** Load the fancy DB ***/ + this.ddb = new DatabaseDriver(); + /*** Load the Config ***/ if(!fs.existsSync('config.json')) { @@ -198,15 +202,23 @@ DBot.prototype.reloadModules = function() { } }, this); } - - // Generate missing DB keys this.config[name] = config; - _.each(config.dbKeys, function(dbKey) { - if(!_.has(this.db, dbKey)) { - this.db[dbKey] = {}; - } - }, this); + // Groovy funky database shit + if(!_.has(config, 'dbType') || config.dbType == 'olde') { + // Generate missing DB keys + _.each(config.dbKeys, function(dbKey) { + if(!_.has(this.db, dbKey)) { + 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) { + module.db = db; + }.bind(this), {}); + } + // Load the module itself var rawModule = require(moduleDir + name); var module = rawModule.fetch(this);