Merge pull request #1558 from strongloop/fix-ds-constructor
fix: allow `new DataSource(connector, settings)`
This commit is contained in:
commit
5dc6a1cd13
|
@ -89,6 +89,21 @@ var slice = Array.prototype.slice;
|
||||||
* @property {String} password Database password.
|
* @property {String} password Database password.
|
||||||
* @property {String} database Name of the database to use.
|
* @property {String} database Name of the database to use.
|
||||||
* @property {Boolean} debug Display debugging information. Default is false.
|
* @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) {
|
function DataSource(name, settings, modelBuilder) {
|
||||||
if (!(this instanceof DataSource)) {
|
if (!(this instanceof DataSource)) {
|
||||||
|
@ -347,11 +362,17 @@ DataSource.prototype.setup = function(dsName, settings) {
|
||||||
var dataSource = this;
|
var dataSource = this;
|
||||||
var connector;
|
var connector;
|
||||||
|
|
||||||
// support single settings object
|
// First argument is an `object`
|
||||||
if (dsName && typeof dsName === 'object' && !settings) {
|
if (dsName && typeof dsName === 'object') {
|
||||||
|
if (settings === undefined) {
|
||||||
// setup({name: 'myDataSource', connector: 'memory'})
|
// setup({name: 'myDataSource', connector: 'memory'})
|
||||||
settings = dsName;
|
settings = dsName;
|
||||||
dsName = undefined;
|
dsName = undefined;
|
||||||
|
} else {
|
||||||
|
// setup(connector, {name: 'myDataSource', host: 'localhost'})
|
||||||
|
connector = dsName;
|
||||||
|
dsName = undefined;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof dsName !== 'string') {
|
if (typeof dsName !== 'string') {
|
||||||
|
|
|
@ -137,4 +137,21 @@ describe('DataSource', function() {
|
||||||
dataSource.name.should.equal('myDataSource');
|
dataSource.name.should.equal('myDataSource');
|
||||||
dataSource.connector.should.equal(mockConnector);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue