From 7dde6466e5f9715c54795850eefb72b2dff1bedf Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Wed, 20 Aug 2014 16:05:45 -0700 Subject: [PATCH 1/2] Only validate dataSource when defined (Fixes #482) --- lib/application.js | 14 ++++++++------ test/app.test.js | 4 ++++ 2 files changed, 12 insertions(+), 6 deletions(-) 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/test/app.test.js b/test/app.test.js index 2832a7b3..935019b8 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -69,6 +69,10 @@ describe('app', function() { request(app).get('/colors').expect(200, done); }); }); + + it('should not require dataSource', function() { + app.model('MyTestModel', {}); + }); }); describe('app.model(name, config)', function () { From 26b67ba7570482f08dd24e87fbb1324791d816d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 26 Aug 2014 17:58:59 +0200 Subject: [PATCH 2/2] registry: warn when dataSource is not specified Modify `registry.configureModel()` to log a warning when `dataSource` optiont is not specified at all. Users should provide `dataSource: null` when the model is intentionally not attached to any data-source. --- lib/registry.js | 13 +++++++++++++ test/app.test.js | 5 +++++ test/loopback.test.js | 2 ++ 3 files changed, 20 insertions(+) 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 935019b8..6d5b2937 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -70,9 +70,14 @@ describe('app', function() { }); }); + 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'