diff --git a/lib/application.js b/lib/application.js index b9facad0..7a3abef5 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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; diff --git a/lib/registry.js b/lib/registry.js index 3cad9469..f7a03a1f 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -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); } }; diff --git a/test/app.test.js b/test/app.test.js index 2832a7b3..6d5b2937 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -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 () { diff --git a/test/loopback.test.js b/test/loopback.test.js index 17f9bc5b..6529d69c 100644 --- a/test/loopback.test.js +++ b/test/loopback.test.js @@ -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'