Merge createModelFromConfig with createModel
Merge the two methods `loopback.createModel` and `loopback.createModelFromConfig` into a single method `createModel`.
This commit is contained in:
parent
63843b41fc
commit
56aab8dbdd
|
@ -141,7 +141,7 @@ app.model = function (Model, config) {
|
||||||
// modeller does not understand `dataSource` option
|
// modeller does not understand `dataSource` option
|
||||||
delete modelConfig.dataSource;
|
delete modelConfig.dataSource;
|
||||||
|
|
||||||
Model = registry.createModelFromConfig(modelConfig);
|
Model = registry.createModel(modelConfig);
|
||||||
|
|
||||||
// delete config options already applied
|
// delete config options already applied
|
||||||
['relations', 'base', 'acls', 'hidden'].forEach(function(prop) {
|
['relations', 'base', 'acls', 'hidden'].forEach(function(prop) {
|
||||||
|
|
105
lib/registry.js
105
lib/registry.js
|
@ -18,6 +18,59 @@ var registry = module.exports;
|
||||||
* Create a named vanilla JavaScript class constructor with an attached
|
* Create a named vanilla JavaScript class constructor with an attached
|
||||||
* set of properties and options.
|
* set of properties and options.
|
||||||
*
|
*
|
||||||
|
* This function comes with two variants:
|
||||||
|
* * `loopback.createModel(name, properties, options)`
|
||||||
|
* * `loopback.createModel(config)`
|
||||||
|
*
|
||||||
|
* In the second variant, the parameters `name`, `properties` and `options`
|
||||||
|
* are provided in the config object. Any additional config entries are
|
||||||
|
* interpreted as `options`, i.e. the following two configs are identical:
|
||||||
|
*
|
||||||
|
* ```js
|
||||||
|
* { name: 'Customer', base: 'User' }
|
||||||
|
* { name: 'Customer', options: { base: 'User' } }
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* **Example**
|
||||||
|
*
|
||||||
|
* Create an `Author` model using the three-parameter variant:
|
||||||
|
*
|
||||||
|
* ```js
|
||||||
|
* loopback.createModel(
|
||||||
|
* 'Author',
|
||||||
|
* {
|
||||||
|
* firstName: 'string',
|
||||||
|
* lastName: 'string
|
||||||
|
* },
|
||||||
|
* {
|
||||||
|
* relations: {
|
||||||
|
* books: {
|
||||||
|
* model: 'Book',
|
||||||
|
* type: 'hasAndBelongsToMany'
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Create the same model using a config object:
|
||||||
|
*
|
||||||
|
* ```js
|
||||||
|
* loopback.createModel({
|
||||||
|
* name: 'Author',
|
||||||
|
* properties: {
|
||||||
|
* firstName: 'string',
|
||||||
|
* lastName: 'string
|
||||||
|
* },
|
||||||
|
* relations: {
|
||||||
|
* books: {
|
||||||
|
* model: 'Book',
|
||||||
|
* type: 'hasAndBelongsToMany'
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*
|
||||||
* @param {String} name Unique name.
|
* @param {String} name Unique name.
|
||||||
* @param {Object} properties
|
* @param {Object} properties
|
||||||
* @param {Object} options (optional)
|
* @param {Object} options (optional)
|
||||||
|
@ -26,6 +79,16 @@ var registry = module.exports;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
registry.createModel = function (name, properties, options) {
|
registry.createModel = function (name, properties, options) {
|
||||||
|
if (arguments.length === 1 && typeof name === 'object') {
|
||||||
|
var config = name;
|
||||||
|
name = config.name;
|
||||||
|
properties = config.properties;
|
||||||
|
options = buildModelOptionsFromConfig(config);
|
||||||
|
|
||||||
|
assert(typeof name === 'string',
|
||||||
|
'The model-config property `name` must be a string');
|
||||||
|
}
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
var BaseModel = options.base || options.super;
|
var BaseModel = options.base || options.super;
|
||||||
|
|
||||||
|
@ -57,48 +120,6 @@ registry.createModel = function (name, properties, options) {
|
||||||
return model;
|
return model;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a model as described by the configuration object.
|
|
||||||
*
|
|
||||||
* **Example**
|
|
||||||
*
|
|
||||||
* ```js
|
|
||||||
* loopback.createModelFromConfig({
|
|
||||||
* name: 'Author',
|
|
||||||
* properties: {
|
|
||||||
* firstName: 'string',
|
|
||||||
* lastName: 'string
|
|
||||||
* },
|
|
||||||
* relations: {
|
|
||||||
* books: {
|
|
||||||
* model: 'Book',
|
|
||||||
* type: 'hasAndBelongsToMany'
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
* });
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* @options {Object} config model configuration
|
|
||||||
* @property {String} name Unique name.
|
|
||||||
* @property {Object} [properties] Model properties
|
|
||||||
* @property {Object} [options] Model options. Options can be specified on the
|
|
||||||
* top level config object too. E.g. `{ base: 'User' }` is the same as
|
|
||||||
* `{ options: { base: 'User' } }`.
|
|
||||||
*
|
|
||||||
* @header loopback.createModelFromConfig(config)
|
|
||||||
*/
|
|
||||||
|
|
||||||
registry.createModelFromConfig = function(config) {
|
|
||||||
var name = config.name;
|
|
||||||
var properties = config.properties;
|
|
||||||
var options = buildModelOptionsFromConfig(config);
|
|
||||||
|
|
||||||
assert(typeof name === 'string',
|
|
||||||
'The model-config property `name` must be a string');
|
|
||||||
|
|
||||||
return this.createModel(name, properties, options);
|
|
||||||
};
|
|
||||||
|
|
||||||
function buildModelOptionsFromConfig(config) {
|
function buildModelOptionsFromConfig(config) {
|
||||||
var options = extend({}, config.options);
|
var options = extend({}, config.options);
|
||||||
for (var key in config) {
|
for (var key in config) {
|
||||||
|
|
|
@ -127,9 +127,9 @@ describe('loopback', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('loopback.createModelFromConfig(config)', function() {
|
describe('loopback.createModel(config)', function() {
|
||||||
it('creates the model', function() {
|
it('creates the model', function() {
|
||||||
var model = loopback.createModelFromConfig({
|
var model = loopback.createModel({
|
||||||
name: uniqueModelName
|
name: uniqueModelName
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -137,7 +137,7 @@ describe('loopback', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('interprets extra first-level keys as options', function() {
|
it('interprets extra first-level keys as options', function() {
|
||||||
var model = loopback.createModelFromConfig({
|
var model = loopback.createModel({
|
||||||
name: uniqueModelName,
|
name: uniqueModelName,
|
||||||
base: 'User'
|
base: 'User'
|
||||||
});
|
});
|
||||||
|
@ -146,7 +146,7 @@ describe('loopback', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('prefers config.options.key over config.key', function() {
|
it('prefers config.options.key over config.key', function() {
|
||||||
var model = loopback.createModelFromConfig({
|
var model = loopback.createModel({
|
||||||
name: uniqueModelName,
|
name: uniqueModelName,
|
||||||
base: 'User',
|
base: 'User',
|
||||||
options: {
|
options: {
|
||||||
|
|
Loading…
Reference in New Issue