Merge pull request #57 from strongloop/feature/require-connector
Allows the full module name for connectors
This commit is contained in:
commit
a81c2a4b41
|
@ -165,6 +165,57 @@ 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-<name>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
|
// testable with DI
|
||||||
|
function tryModules(names, loader) {
|
||||||
|
var mod;
|
||||||
|
loader = loader || require;
|
||||||
|
for(var m =0; m<names.length; m++) {
|
||||||
|
try {
|
||||||
|
mod = loader(names[m]);
|
||||||
|
} catch (e) {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
if (mod) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Resolve a connector by name
|
||||||
|
* @param name The connector name
|
||||||
|
* @returns {*}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
DataSource._resolveConnector = function(name, loader) {
|
||||||
|
var names = connectorModuleNames(name);
|
||||||
|
var connector = tryModules(names , loader);
|
||||||
|
var error = null;
|
||||||
|
if(!connector) {
|
||||||
|
error = '\nWARNING: LoopBack connector "' + name
|
||||||
|
+ '" is not installed at any of the locations ' + names
|
||||||
|
+ '. To fix, run:\n\n npm install '
|
||||||
|
+ name + '\n';
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
connector: connector,
|
||||||
|
error: error
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up the data source
|
* Set up the data source
|
||||||
* @param {String} name The name
|
* @param {String} name The name
|
||||||
|
@ -218,24 +269,14 @@ DataSource.prototype.setup = function(name, settings) {
|
||||||
// The first argument might be the connector itself
|
// The first argument might be the connector itself
|
||||||
connector = name;
|
connector = name;
|
||||||
this.name = 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 {
|
} else {
|
||||||
// try foreign connector
|
// The connector has not been resolved
|
||||||
try {
|
var result = DataSource._resolveConnector(name);
|
||||||
if(name.indexOf('loopback-connector-') === -1) {
|
connector = result.connector;
|
||||||
name = 'loopback-connector-' + name;
|
if(!connector) {
|
||||||
}
|
console.error(result.error);
|
||||||
connector = require(name);
|
return;
|
||||||
} 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');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -444,6 +444,51 @@ describe('Load models with base', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('DataSource constructor', function() {
|
||||||
|
// Mocked require
|
||||||
|
var loader = function(name) {
|
||||||
|
return {
|
||||||
|
name: name
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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', loader);
|
||||||
|
assert(connector.connector);
|
||||||
|
});
|
||||||
|
it('should try to resolve connector by short module name', function() {
|
||||||
|
var connector = DataSource._resolveConnector('xyz', loader);
|
||||||
|
assert(connector.connector);
|
||||||
|
});
|
||||||
|
it('should try to resolve connector by full module name', function() {
|
||||||
|
var connector = DataSource._resolveConnector('loopback-xyz', loader);
|
||||||
|
assert(connector.connector);
|
||||||
|
});
|
||||||
|
it('should fail 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 fail 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 fail 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 () {
|
describe('Load models with relations', function () {
|
||||||
it('should set up relations', function (done) {
|
it('should set up relations', function (done) {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
Loading…
Reference in New Issue