Skip to content

Commit

Permalink
registry: warn when dataSource is not specified
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Miroslav Bajtoš committed Aug 26, 2014
1 parent 7dde646 commit 26b67ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
};

Expand Down
5 changes: 5 additions & 0 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 2 additions & 0 deletions test/loopback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ describe('loopback', function() {
var model = loopback.Model.extend(uniqueModelName);

loopback.configureModel(model, {
dataSource: null,
relations: {
owner: {
type: 'belongsTo',
Expand All @@ -207,6 +208,7 @@ describe('loopback', function() {
});

loopback.configureModel(model, {
dataSource: null,
relations: {
owner: {
model: 'Application'
Expand Down

0 comments on commit 26b67ba

Please sign in to comment.