fix: allow `new DataSource(connector, settings)`

This commit is contained in:
Raymond Feng 2018-02-12 22:02:17 -08:00
parent 310b9489d3
commit 87e1e217c3
2 changed files with 43 additions and 5 deletions

View File

@ -89,6 +89,21 @@ var slice = Array.prototype.slice;
* @property {String} password Database password.
* @property {String} database Name of the database to use.
* @property {Boolean} debug Display debugging information. Default is false.
*
* The constructor allows the following styles:
*
* 1. new DataSource(dataSourceName, settings). For example:
* - new DataSource('myDataSource', {connector: 'memory'});
* - new DataSource('myDataSource', {name: 'myDataSource', connector: 'memory'});
* - new DataSource('myDataSource', {name: 'anotherDataSource', connector: 'memory'});
*
* 2. new DataSource(settings). For example:
* - new DataSource({name: 'myDataSource', connector: 'memory'});
* - new DataSource({connector: 'memory'});
*
* 3. new DataSource(connectorModule, settings). For example:
* - new DataSource(connectorModule, {name: 'myDataSource})
* - new DataSource(connectorModule)
*/
function DataSource(name, settings, modelBuilder) {
if (!(this instanceof DataSource)) {
@ -347,11 +362,17 @@ DataSource.prototype.setup = function(dsName, settings) {
var dataSource = this;
var connector;
// support single settings object
if (dsName && typeof dsName === 'object' && !settings) {
// setup({name: 'myDataSource', connector: 'memory'})
settings = dsName;
dsName = undefined;
// First argument is an `object`
if (dsName && typeof dsName === 'object') {
if (settings === undefined) {
// setup({name: 'myDataSource', connector: 'memory'})
settings = dsName;
dsName = undefined;
} else {
// setup(connector, {name: 'myDataSource', host: 'localhost'})
connector = dsName;
dsName = undefined;
}
}
if (typeof dsName !== 'string') {

View File

@ -137,4 +137,21 @@ describe('DataSource', function() {
dataSource.name.should.equal('myDataSource');
dataSource.connector.should.equal(mockConnector);
});
/**
* new DataSource(connectorInstance, settings)
*/
it('should accept resolved connector and settings', function() {
var mockConnector = {
name: 'loopback-connector-mock',
initialize: function(ds, cb) {
ds.connector = mockConnector;
return cb(null);
},
};
var dataSource = new DataSource(mockConnector, {name: 'myDataSource'});
dataSource.name.should.equal('myDataSource');
dataSource.connector.should.equal(mockConnector);
});
});