app: flatten model config

Support flat structure of model config objects, where model options
are set as top-level properties.

Before:

    Customer: {
      dataSource: 'db',
      options: {
        base: 'User'
      }
    }

Now:

    Customer: {
      dataSource: 'db',
      base: 'User'
    }
This commit is contained in:
Miroslav Bajtoš 2014-05-25 16:27:45 +02:00
parent 25250a03d1
commit 227c2d05cd
2 changed files with 54 additions and 5 deletions

View File

@ -7,6 +7,7 @@ var DataSource = require('loopback-datasource-juggler').DataSource
, compat = require('./compat')
, assert = require('assert')
, fs = require('fs')
, extend = require('util')._extend
, _ = require('underscore')
, RemoteObjects = require('strong-remoting')
, swagger = require('strong-remoting/ext/swagger')
@ -540,7 +541,10 @@ function dataSourcesFromConfig(config) {
}
function modelFromConfig(name, config, app) {
var ModelCtor = require('./loopback').createModel(name, config.properties, config.options);
var options = buildModelOptionsFromConfig(config);
var properties = config.properties;
var ModelCtor = require('./loopback').createModel(name, properties, options);
var dataSource = config.dataSource;
if(typeof dataSource === 'string') {
@ -553,6 +557,26 @@ function modelFromConfig(name, config, app) {
return ModelCtor;
}
function buildModelOptionsFromConfig(config) {
var options = extend({}, config.options);
for (var key in config) {
if (['properties', 'options', 'dataSource'].indexOf(key) !== -1) {
// Skip items which have special meaning
continue;
}
if (options[key] !== undefined) {
// When both `config.key` and `config.options.key` are set,
// use the latter one to preserve backwards compatibility
// with loopback 1.x
continue;
}
options[key] = config[key];
}
return options;
}
function requireDir(dir, basenames) {
assert(dir, 'cannot require directory contents without directory name');

View File

@ -61,9 +61,11 @@ describe('app', function() {
});
});
describe('app.model(name, properties, options)', function () {
it('Sugar for defining a fully built model', function () {
var app = loopback();
describe('app.model(name, config)', function () {
var app;
beforeEach(function() {
app = loopback();
app.boot({
app: {port: 3000, host: '127.0.0.1'},
dataSources: {
@ -72,16 +74,39 @@ describe('app', function() {
}
}
});
});
it('Sugar for defining a fully built model', function () {
app.model('foo', {
dataSource: 'db'
});
var Foo = app.models.foo;
var f = new Foo;
var f = new Foo();
assert(f instanceof loopback.Model);
});
it('interprets extra first-level keys as options', function() {
app.model('foo', {
dataSource: 'db',
base: 'User'
});
expect(app.models.foo.definition.settings.base).to.equal('User');
});
it('prefers config.options.key over config.key', function() {
app.model('foo', {
dataSource: 'db',
base: 'User',
options: {
base: 'Application'
}
});
expect(app.models.foo.definition.settings.base).to.equal('Application');
});
});
describe('app.models', function() {