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:
parent
25250a03d1
commit
227c2d05cd
|
@ -7,6 +7,7 @@ var DataSource = require('loopback-datasource-juggler').DataSource
|
||||||
, compat = require('./compat')
|
, compat = require('./compat')
|
||||||
, assert = require('assert')
|
, assert = require('assert')
|
||||||
, fs = require('fs')
|
, fs = require('fs')
|
||||||
|
, extend = require('util')._extend
|
||||||
, _ = require('underscore')
|
, _ = require('underscore')
|
||||||
, RemoteObjects = require('strong-remoting')
|
, RemoteObjects = require('strong-remoting')
|
||||||
, swagger = require('strong-remoting/ext/swagger')
|
, swagger = require('strong-remoting/ext/swagger')
|
||||||
|
@ -540,7 +541,10 @@ function dataSourcesFromConfig(config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function modelFromConfig(name, config, app) {
|
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;
|
var dataSource = config.dataSource;
|
||||||
|
|
||||||
if(typeof dataSource === 'string') {
|
if(typeof dataSource === 'string') {
|
||||||
|
@ -553,6 +557,26 @@ function modelFromConfig(name, config, app) {
|
||||||
return ModelCtor;
|
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) {
|
function requireDir(dir, basenames) {
|
||||||
assert(dir, 'cannot require directory contents without directory name');
|
assert(dir, 'cannot require directory contents without directory name');
|
||||||
|
|
||||||
|
|
|
@ -61,9 +61,11 @@ describe('app', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('app.model(name, properties, options)', function () {
|
describe('app.model(name, config)', function () {
|
||||||
it('Sugar for defining a fully built model', function () {
|
var app;
|
||||||
var app = loopback();
|
|
||||||
|
beforeEach(function() {
|
||||||
|
app = loopback();
|
||||||
app.boot({
|
app.boot({
|
||||||
app: {port: 3000, host: '127.0.0.1'},
|
app: {port: 3000, host: '127.0.0.1'},
|
||||||
dataSources: {
|
dataSources: {
|
||||||
|
@ -72,16 +74,39 @@ describe('app', function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Sugar for defining a fully built model', function () {
|
||||||
app.model('foo', {
|
app.model('foo', {
|
||||||
dataSource: 'db'
|
dataSource: 'db'
|
||||||
});
|
});
|
||||||
|
|
||||||
var Foo = app.models.foo;
|
var Foo = app.models.foo;
|
||||||
var f = new Foo;
|
var f = new Foo();
|
||||||
|
|
||||||
assert(f instanceof loopback.Model);
|
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() {
|
describe('app.models', function() {
|
||||||
|
|
Loading…
Reference in New Issue