Merge pull request #1555 from strongloop/truongminh-set-datasource-name
Use more meaningful names for dataSource and connector
This commit is contained in:
commit
bbff92c13f
|
@ -329,28 +329,40 @@ DataSource.prototype.connect = function(callback) {
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up the data source
|
* Set up the data source. The following styles are supported:
|
||||||
* @param {String} name The name
|
* ```js
|
||||||
|
* ds.setup('myDataSource', {connector: 'memory'}); // ds.name -> 'myDataSource'
|
||||||
|
* ds.setup('myDataSource', {name: 'myDataSource', connector: 'memory'}); // ds.name -> 'myDataSource'
|
||||||
|
* ds.setup('myDataSource', {name: 'anotherDataSource', connector: 'memory'}); // ds.name -> 'myDataSource' and a warning will be issued
|
||||||
|
* ds.setup({name: 'myDataSource', connector: 'memory'}); // ds.name -> 'myDataSource'
|
||||||
|
* ds.setup({connector: 'memory'}); // ds.name -> 'memory'
|
||||||
|
* ```
|
||||||
|
* @param {String} dsName The name of the datasource. If not set, use
|
||||||
|
* `settings.name`
|
||||||
* @param {Object} settings The settings
|
* @param {Object} settings The settings
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.setup = function(name, settings) {
|
DataSource.prototype.setup = function(dsName, settings) {
|
||||||
var dataSource = this;
|
var dataSource = this;
|
||||||
var connector;
|
var connector;
|
||||||
|
|
||||||
// support single settings object
|
// support single settings object
|
||||||
if (name && typeof name === 'object' && !settings) {
|
if (dsName && typeof dsName === 'object' && !settings) {
|
||||||
settings = name;
|
// setup({name: 'myDataSource', connector: 'memory'})
|
||||||
name = undefined;
|
settings = dsName;
|
||||||
|
dsName = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof settings === 'object') {
|
if (typeof settings === 'object') {
|
||||||
if (settings.initialize) {
|
if (settings.initialize) {
|
||||||
|
// Settings is the resolved connector instance
|
||||||
connector = settings;
|
connector = settings;
|
||||||
} else if (settings.connector) {
|
} else if (settings.connector) {
|
||||||
|
// Use `connector`
|
||||||
connector = settings.connector;
|
connector = settings.connector;
|
||||||
} else if (settings.adapter) {
|
} else if (settings.adapter) {
|
||||||
|
// `adapter` as alias for `connector`
|
||||||
connector = settings.adapter;
|
connector = settings.adapter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -365,44 +377,45 @@ DataSource.prototype.setup = function(name, settings) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof settings === 'object' && typeof settings.name === 'string' &&
|
if (typeof settings === 'object' && typeof settings.name === 'string' &&
|
||||||
typeof name === 'string' && name !== settings.name) {
|
typeof dsName === 'string' && dsName !== settings.name) {
|
||||||
|
// setup('myDataSource', {name: 'anotherDataSource', connector: 'memory'});
|
||||||
|
// ds.name -> 'myDataSource' and a warning will be issued
|
||||||
console.warn(
|
console.warn(
|
||||||
'A datasource has a name of %j while a name of %j is specified in ' +
|
'A datasource has a name of %j while a name of %j is specified in ' +
|
||||||
'its settings. Please adjust your configuration so these names match. ' +
|
'its settings. Please adjust your configuration so these names match. ' +
|
||||||
'%j will be assigned as the actual datasource name.',
|
'%j will be assigned as the actual datasource name.',
|
||||||
name, settings.name, name);
|
dsName, settings.name, dsName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnected by default
|
// Disconnected by default
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.connecting = false;
|
this.connecting = false;
|
||||||
|
|
||||||
this.name = name || (typeof this.settings.name === 'string' && this.settings.name);
|
this.name = dsName || (typeof this.settings.name === 'string' && this.settings.name);
|
||||||
|
|
||||||
|
var connectorName;
|
||||||
if (typeof connector === 'string') {
|
if (typeof connector === 'string') {
|
||||||
name = connector;
|
// Connector needs to be resolved by name
|
||||||
|
connectorName = connector;
|
||||||
connector = undefined;
|
connector = undefined;
|
||||||
|
} else if ((typeof connector === 'object') && connector) {
|
||||||
|
connectorName = connector.name;
|
||||||
|
} else {
|
||||||
|
connectorName = dsName;
|
||||||
}
|
}
|
||||||
name = name || (connector && connector.name);
|
if (!this.name) {
|
||||||
if (typeof this.name !== 'string' || this.name.length === 0) {
|
// Fall back to connector name
|
||||||
this.name = name;
|
this.name = connectorName;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name && !connector) {
|
if ((!connector) && connectorName) {
|
||||||
if (typeof name === 'object') {
|
// The connector has not been resolved
|
||||||
// The first argument might be the connector itself
|
var result = DataSource._resolveConnector(connectorName);
|
||||||
connector = name;
|
connector = result.connector;
|
||||||
if (typeof this.name !== 'string' || this.name.length === 0) {
|
if (!connector) {
|
||||||
this.name = connector.name;
|
console.error(result.error);
|
||||||
}
|
this.emit('error', new Error(result.error));
|
||||||
} else {
|
return;
|
||||||
// The connector has not been resolved
|
|
||||||
var result = DataSource._resolveConnector(name);
|
|
||||||
connector = result.connector;
|
|
||||||
if (!connector) {
|
|
||||||
console.error(result.error);
|
|
||||||
this.emit('error', new Error(result.error));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -437,7 +450,7 @@ DataSource.prototype.setup = function(name, settings) {
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message) {
|
if (err.message) {
|
||||||
err.message = 'Cannot initialize connector ' +
|
err.message = 'Cannot initialize connector ' +
|
||||||
JSON.stringify(connector.name || name) + ': ' +
|
JSON.stringify(connector.name || dsName) + ': ' +
|
||||||
err.message;
|
err.message;
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
|
|
Loading…
Reference in New Issue