Merge pull request #483 from strongloop/fix/undefined-datasource

Only validate dataSource when defined
This commit is contained in:
Ritchie Martori 2014-08-26 09:55:59 -07:00
commit 94049d75f7
4 changed files with 32 additions and 6 deletions

View File

@ -390,13 +390,15 @@ function configureModel(ModelCtor, config, app) {
var dataSource = config.dataSource;
if(typeof dataSource === 'string') {
dataSource = app.dataSources[dataSource];
}
if(dataSource) {
if(typeof dataSource === 'string') {
dataSource = app.dataSources[dataSource];
}
assert(dataSource instanceof DataSource,
ModelCtor.modelName + ' is referencing a dataSource that does not exist: "' +
config.dataSource +'"');
assert(dataSource instanceof DataSource,
ModelCtor.modelName + ' is referencing a dataSource that does not exist: "' +
config.dataSource +'"');
}
config = extend({}, config);
config.dataSource = dataSource;

View File

@ -10,6 +10,7 @@
var assert = require('assert');
var extend = require('util')._extend;
var juggler = require('loopback-datasource-juggler');
var debug = require('debug')('loopback:registry');
var DataSource = juggler.DataSource;
var ModelBuilder = juggler.ModelBuilder;
@ -171,6 +172,18 @@ registry.configureModel = function(ModelCtor, config) {
'Cannot configure ' + ModelCtor.modelName +
': config.dataSource must be an instance of DataSource');
ModelCtor.attachTo(config.dataSource);
debug('Attached model `%s` to dataSource `%s`',
ModelCtor.definition.name, config.dataSource.name);
} else if (config.dataSource === null) {
debug('Model `%s` is not attached to any DataSource by configuration.',
ModelCtor.definition.name);
} else {
debug('Model `%s` is not attached to any DataSource, possibly by a mistake.',
ModelCtor.definition.name);
console.warn(
'The configuration of `%s` is missing `dataSource` property.\n' +
'Use `null` or `false` to mark models not attached to any data source.',
ModelCtor.definition.name);
}
};

View File

@ -69,6 +69,15 @@ describe('app', function() {
request(app).get('/colors').expect(200, done);
});
});
it('accepts null dataSource', function() {
app.model('MyTestModel', { dataSource: null });
});
it('should not require dataSource', function() {
app.model('MyTestModel', {});
});
});
describe('app.model(name, config)', function () {

View File

@ -185,6 +185,7 @@ describe('loopback', function() {
var model = loopback.Model.extend(uniqueModelName);
loopback.configureModel(model, {
dataSource: null,
relations: {
owner: {
type: 'belongsTo',
@ -207,6 +208,7 @@ describe('loopback', function() {
});
loopback.configureModel(model, {
dataSource: null,
relations: {
owner: {
model: 'Application'