3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-18 00:22:35 +01:00
dbot/database.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

var databank = require('databank'),
Databank = databank.Databank,
2013-07-08 12:06:34 +02:00
DatabankObject = databank.DatabankObject,
_ = require('underscore')._;
/**
* Multiplex databank objects
*/
2013-07-08 12:06:34 +02:00
var DatabaseDriver = function(config) {
this.config = config;
this.databanks = {};
};
/**
* Connect to or create a new DataBank
*/
DatabaseDriver.prototype.createDB = function(name, driver, schema, callback) {
2013-11-02 22:40:10 +01:00
if(!_.has(this.databanks, name)) {
var params = { 'schema': schema };
2013-11-02 22:40:10 +01:00
if(driver == 'redis' && _.has(this.config, 'redisPort')) params.port = this.config.redisPort;
if(driver == 'disk') params.dir = 'db';
2013-11-02 22:40:10 +01:00
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 - ' + err);
} else {
callback(this.databanks[name]);
}
}.bind(this));
} else {
callback(this.databanks[name]);
}
};
exports.DatabaseDriver = DatabaseDriver;