From acd02bf48277e73a07be2d2bf215247c3a1ccf65 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 9 Jan 2014 17:03:37 -0800 Subject: [PATCH 1/3] Allows the full module name for connectors --- lib/datasource.js | 65 +++++++++++++++++++++++++++++----------- test/loopback-dl.test.js | 26 ++++++++++++++++ 2 files changed, 74 insertions(+), 17 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index a667e4c3..50424f72 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -165,6 +165,47 @@ DataSource.prototype._setupConnector = function () { } }; +/*! + * Resolve a connector by name + * @param name The connector name + * @returns {*} + * @private + */ +DataSource._resolveConnector = function(name) { + var connector = null; + var error = null; + if (name.match(/^\//)) { + // try absolute path + connector = require(name); + } else if (existsSync(__dirname + '/connectors/' + name + '.js')) { + // try built-in connector + connector = require('./connectors/' + name); + } else { + // try foreign connector + try { + connector = require(name); + } catch(e) { + try { + if(name.indexOf('loopback-connector-') === -1) { + name = 'loopback-connector-' + name; + connector = require(name); + } + } catch (e) { + // ignore + } + if(!connector) { + error = '\nWARNING: LoopBack connector "' + name + + '" is not installed,\nso your models would not work, to fix run:\n\n npm install ' + + name, '\n'; + } + } + } + return { + connector: connector, + error: error + }; +}; + /** * Set up the data source * @param {String} name The name @@ -218,24 +259,14 @@ DataSource.prototype.setup = function(name, settings) { // The first argument might be the connector itself connector = name; this.name = connector.name; - } - // The connector has not been resolved - else if (name.match(/^\//)) { - // try absolute path - connector = require(name); - } else if (existsSync(__dirname + '/connectors/' + name + '.js')) { - // try built-in connector - connector = require('./connectors/' + name); } else { - // try foreign connector - try { - if(name.indexOf('loopback-connector-') === -1) { - name = 'loopback-connector-' + name; - } - connector = require(name); - } catch (e) { - return console.log('\nWARNING: LoopBack connector "' + name + '" is not installed,\nso your models would not work, to fix run:\n\n npm install ' + name, '\n'); - } + // The connector has not been resolved + var result = DataSource._resolveConnector(name); + connector = result.connector; + if(!connector) { + console.error(result.error); + return; + } } } diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index f4214df5..dc643921 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -444,6 +444,32 @@ describe('Load models with base', function () { }); }); +describe('DataSource constructor', function() { + it('should resolve connector by path', function() { + var connector = DataSource._resolveConnector(__dirname + '/../lib/connectors/memory'); + assert(connector.connector); + }); + it('should resolve connector by internal name', function() { + var connector = DataSource._resolveConnector('memory'); + assert(connector.connector); + }); + it('should try to resolve connector by module name starts with loopback-connector-', function() { + var connector = DataSource._resolveConnector('loopback-connector-xyz'); + assert(!connector.connector); + assert(connector.error.indexOf('loopback-connector-xyz') !== -1); + }); + it('should try to resolve connector by short module name', function() { + var connector = DataSource._resolveConnector('xyz'); + assert(!connector.connector); + assert(connector.error.indexOf('loopback-connector-xyz') !== -1); + }); + it('should try to resolve connector by full module name', function() { + var connector = DataSource._resolveConnector('loopback-xyz'); + assert(!connector.connector); + assert(connector.error.indexOf('loopback-connector-loopback-xyz') !== -1); + }); +}); + describe('Load models with relations', function () { it('should set up relations', function (done) { var ds = new DataSource('memory'); From 2a50388894d30e7cff0cb6fa223993bf648b28e6 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 13 Jan 2014 11:06:02 -0800 Subject: [PATCH 2/3] Make the code testable following the review comments --- lib/datasource.js | 64 +++++++++++++++++++++++----------------- test/loopback-dl.test.js | 23 +++++++++++++-- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index 50424f72..f3a239ca 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -165,40 +165,50 @@ DataSource.prototype._setupConnector = function () { } }; +// List possible connector module names +function connectorModuleNames(name) { + var names = [name]; // Check the name as is + if (!name.match(/^\//)) { + names.push('./connectors/' + name); // Check built-in connectors + if (name.indexOf('loopback-connector-') !== 0) { + names.push('loopback-connector-' + name); // Try loopback-connector- + } + } + return names; +} + +// testable with DI +function tryModules(names, loader) { + var mod; + loader = loader || require; + for(var m =0; m Date: Mon, 13 Jan 2014 13:21:13 -0800 Subject: [PATCH 3/3] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c4dca516..7e86399b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-datasource-juggler", - "version": "1.2.11", + "version": "1.2.12", "description": "LoopBack DataSoure Juggler", "keywords": [ "StrongLoop",