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,6 +390,7 @@ function configureModel(ModelCtor, config, app) {
var dataSource = config.dataSource; var dataSource = config.dataSource;
if(dataSource) {
if(typeof dataSource === 'string') { if(typeof dataSource === 'string') {
dataSource = app.dataSources[dataSource]; dataSource = app.dataSources[dataSource];
} }
@ -397,6 +398,7 @@ function configureModel(ModelCtor, config, app) {
assert(dataSource instanceof DataSource, assert(dataSource instanceof DataSource,
ModelCtor.modelName + ' is referencing a dataSource that does not exist: "' + ModelCtor.modelName + ' is referencing a dataSource that does not exist: "' +
config.dataSource +'"'); config.dataSource +'"');
}
config = extend({}, config); config = extend({}, config);
config.dataSource = dataSource; config.dataSource = dataSource;

View File

@ -10,6 +10,7 @@
var assert = require('assert'); var assert = require('assert');
var extend = require('util')._extend; var extend = require('util')._extend;
var juggler = require('loopback-datasource-juggler'); var juggler = require('loopback-datasource-juggler');
var debug = require('debug')('loopback:registry');
var DataSource = juggler.DataSource; var DataSource = juggler.DataSource;
var ModelBuilder = juggler.ModelBuilder; var ModelBuilder = juggler.ModelBuilder;
@ -171,6 +172,18 @@ registry.configureModel = function(ModelCtor, config) {
'Cannot configure ' + ModelCtor.modelName + 'Cannot configure ' + ModelCtor.modelName +
': config.dataSource must be an instance of DataSource'); ': config.dataSource must be an instance of DataSource');
ModelCtor.attachTo(config.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); 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 () { describe('app.model(name, config)', function () {

View File

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