Set up connector from the data source

This commit is contained in:
Raymond Feng 2013-07-23 13:19:35 -07:00
parent 1970c99424
commit ac76666b7b
1 changed files with 28 additions and 17 deletions

View File

@ -62,8 +62,7 @@ function DataSource(name, settings) {
this.setup(name, settings); this.setup(name, settings);
this.connector = this.connector || this.adapter; this._setupConnector();
this.adapter = this.connector; // Keep the adapter as an alias to connector
// connector // connector
var connector = this.connector; var connector = this.connector;
@ -119,6 +118,32 @@ for (var m in ModelBuilder) {
} }
} }
/**
* Set up the connector instance for backward compatibility with JugglingDB schema/adapter
* @private
*/
DataSource.prototype._setupConnector = function () {
this.connector = this.connector || this.adapter; // The legacy JugglingDB adapter will set up `adapter` property
this.adapter = this.connector; // Keep the adapter as an alias to connector
if (this.connector) {
if (!this.connector.dataSource) {
// Set up the dataSource if the connector doesn't do so
this.connector.dataSource = this;
}
this.connector.log = function (query, start) {
dataSource.log(query, start);
};
this.connector.logger = function (query) {
var t1 = Date.now();
var log = this.log;
return function (q) {
log(q || query, t1);
};
};
}
}
DataSource.prototype.setup = function(name, settings) { DataSource.prototype.setup = function(name, settings) {
var dataSource = this; var dataSource = this;
var connector; var connector;
@ -175,26 +200,12 @@ DataSource.prototype.setup = function(name, settings) {
if (connector) { if (connector) {
var postInit = function postInit() { var postInit = function postInit() {
this.connector = this.connector || this.adapter; this._setupConnector();
this.adapter = this.connector; // Keep the adapter as an alias to connector
// we have an connector now? // we have an connector now?
if (!this.connector) { if (!this.connector) {
throw new Error('Connector is not defined correctly: it should create `connector` member of dataSource'); throw new Error('Connector is not defined correctly: it should create `connector` member of dataSource');
} }
this.connector.log = function (query, start) {
dataSource.log(query, start);
};
this.connector.logger = function (query) {
var t1 = Date.now();
var log = this.log;
return function (q) {
log(q || query, t1);
};
};
this.connected = true; this.connected = true;
this.emit('connected'); this.emit('connected');