From ac76666b7bc17c630476716d375c4247d44894e0 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 23 Jul 2013 13:19:35 -0700 Subject: [PATCH] Set up connector from the data source --- lib/datasource.js | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index de841670..d90e6795 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -62,8 +62,7 @@ function DataSource(name, settings) { this.setup(name, settings); - this.connector = this.connector || this.adapter; - this.adapter = this.connector; // Keep the adapter as an alias to connector + this._setupConnector(); // 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) { var dataSource = this; var connector; @@ -175,26 +200,12 @@ DataSource.prototype.setup = function(name, settings) { if (connector) { var postInit = function postInit() { - this.connector = this.connector || this.adapter; - this.adapter = this.connector; // Keep the adapter as an alias to connector - + this._setupConnector(); // we have an connector now? if (!this.connector) { 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.emit('connected');