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] 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'