2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2016-04-01 22:25:16 +00:00
|
|
|
|
2013-08-13 16:37:09 +00:00
|
|
|
/*!
|
2013-05-17 17:54:14 +00:00
|
|
|
* Module dependencies
|
|
|
|
*/
|
2013-05-24 15:02:58 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const g = require('strong-globalize')();
|
|
|
|
const inflection = require('inflection');
|
|
|
|
const EventEmitter = require('events').EventEmitter;
|
|
|
|
const util = require('util');
|
|
|
|
const assert = require('assert');
|
|
|
|
const deprecated = require('depd')('loopback-datasource-juggler');
|
|
|
|
const DefaultModelBaseClass = require('./model.js');
|
|
|
|
const List = require('./list.js');
|
|
|
|
const ModelDefinition = require('./model-definition.js');
|
|
|
|
const deepMerge = require('./utils').deepMerge;
|
|
|
|
const deepMergeProperty = require('./utils').deepMergeProperty;
|
|
|
|
const rankArrayElements = require('./utils').rankArrayElements;
|
|
|
|
const MixinProvider = require('./mixins');
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-08-07 21:51:32 +00:00
|
|
|
// Set up types
|
|
|
|
require('./types')(ModelBuilder);
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const introspect = require('./introspection')(ModelBuilder);
|
2013-08-07 21:51:32 +00:00
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
/*!
|
2013-05-17 17:54:14 +00:00
|
|
|
* Export public API
|
|
|
|
*/
|
2013-08-13 16:37:09 +00:00
|
|
|
exports.ModelBuilder = exports.Schema = ModelBuilder;
|
2013-05-20 23:05:59 +00:00
|
|
|
|
2013-08-13 16:37:09 +00:00
|
|
|
/*!
|
2013-05-17 17:54:14 +00:00
|
|
|
* Helpers
|
|
|
|
*/
|
2018-12-07 14:54:29 +00:00
|
|
|
const slice = Array.prototype.slice;
|
2013-05-17 17:54:14 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* ModelBuilder - A builder to define data models.
|
2015-01-19 12:39:31 +00:00
|
|
|
*
|
2014-06-17 20:18:18 +00:00
|
|
|
* @property {Object} definitions Definitions of the models.
|
|
|
|
* @property {Object} models Model constructors
|
2014-03-12 23:28:46 +00:00
|
|
|
* @class
|
2013-05-17 17:54:14 +00:00
|
|
|
*/
|
2013-05-24 05:20:20 +00:00
|
|
|
function ModelBuilder() {
|
2014-01-24 17:09:53 +00:00
|
|
|
// create blank models pool
|
|
|
|
this.models = {};
|
|
|
|
this.definitions = {};
|
2016-05-27 22:21:53 +00:00
|
|
|
this.settings = {};
|
2014-08-08 08:20:57 +00:00
|
|
|
this.mixins = new MixinProvider(this);
|
2014-07-08 21:04:20 +00:00
|
|
|
this.defaultModelBaseClass = DefaultModelBaseClass;
|
2013-08-09 22:16:32 +00:00
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
// Inherit from EventEmitter
|
2013-05-24 05:20:20 +00:00
|
|
|
util.inherits(ModelBuilder, EventEmitter);
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
// Create a default instance
|
|
|
|
ModelBuilder.defaultInstance = new ModelBuilder();
|
|
|
|
|
2013-11-08 01:11:17 +00:00
|
|
|
function isModelClass(cls) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!cls) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return cls.prototype instanceof DefaultModelBaseClass;
|
2013-11-08 01:11:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 00:13:41 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Get a model by name.
|
2015-01-19 12:39:31 +00:00
|
|
|
*
|
2013-12-19 00:13:41 +00:00
|
|
|
* @param {String} name The model name
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {Boolean} forceCreate Whether the create a stub for the given name if a model doesn't exist.
|
2017-05-19 17:31:24 +00:00
|
|
|
* @returns {ModelClass} The model class
|
2013-12-19 00:13:41 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBuilder.prototype.getModel = function(name, forceCreate) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let model = this.models[name];
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!model && forceCreate) {
|
2016-08-19 17:46:59 +00:00
|
|
|
model = this.define(name, {}, {unresolved: true});
|
2013-12-19 00:13:41 +00:00
|
|
|
}
|
|
|
|
return model;
|
2013-11-09 01:13:00 +00:00
|
|
|
};
|
|
|
|
|
2014-01-28 22:23:48 +00:00
|
|
|
/**
|
|
|
|
* Get the model definition by name
|
|
|
|
* @param {String} name The model name
|
|
|
|
* @returns {ModelDefinition} The model definition
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBuilder.prototype.getModelDefinition = function(name) {
|
2013-11-09 01:13:00 +00:00
|
|
|
return this.definitions[name];
|
|
|
|
};
|
|
|
|
|
2013-05-17 17:54:14 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Define a model class.
|
|
|
|
* Simple example:
|
2013-05-17 17:54:14 +00:00
|
|
|
* ```
|
2013-11-15 19:12:33 +00:00
|
|
|
* var User = modelBuilder.define('User', {
|
2013-05-17 17:54:14 +00:00
|
|
|
* email: String,
|
|
|
|
* password: String,
|
|
|
|
* birthDate: Date,
|
|
|
|
* activated: Boolean
|
|
|
|
* });
|
|
|
|
* ```
|
2014-03-12 23:28:46 +00:00
|
|
|
* More advanced example:
|
2013-05-17 17:54:14 +00:00
|
|
|
* ```
|
2013-11-15 19:12:33 +00:00
|
|
|
* var User = modelBuilder.define('User', {
|
2013-05-17 17:54:14 +00:00
|
|
|
* email: { type: String, limit: 150, index: true },
|
|
|
|
* password: { type: String, limit: 50 },
|
|
|
|
* birthDate: Date,
|
|
|
|
* registrationDate: {type: Date, default: function () { return new Date }},
|
|
|
|
* activated: { type: Boolean, default: false }
|
|
|
|
* });
|
|
|
|
* ```
|
2014-03-12 23:28:46 +00:00
|
|
|
*
|
2015-01-19 12:39:31 +00:00
|
|
|
* @param {String} className Name of class
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {Object} properties Hash of class properties in format `{property: Type, property2: Type2, ...}` or `{property: {type: Type}, property2: {type: Type2}, ...}`
|
|
|
|
* @param {Object} settings Other configuration of class
|
2015-02-07 19:15:28 +00:00
|
|
|
* @param {Function} parent Parent model
|
2017-05-19 17:31:24 +00:00
|
|
|
* @return {ModelClass} The class constructor.
|
2014-03-12 23:28:46 +00:00
|
|
|
*
|
2013-05-17 17:54:14 +00:00
|
|
|
*/
|
2013-07-01 23:49:43 +00:00
|
|
|
ModelBuilder.prototype.define = function defineClass(className, properties, settings, parent) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelBuilder = this;
|
|
|
|
const args = slice.call(arguments);
|
|
|
|
const pluralName = (settings && settings.plural) ||
|
2014-01-24 17:09:53 +00:00
|
|
|
inflection.pluralize(className);
|
2015-01-19 12:39:31 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const httpOptions = (settings && settings.http) || {};
|
|
|
|
let pathName = httpOptions.path || pluralName;
|
2015-01-19 12:39:31 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!className) {
|
2016-07-22 19:26:07 +00:00
|
|
|
throw new Error(g.f('Class name required'));
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
if (args.length === 1) {
|
|
|
|
properties = {};
|
|
|
|
args.push(properties);
|
|
|
|
}
|
|
|
|
if (args.length === 2) {
|
|
|
|
settings = {};
|
|
|
|
args.push(settings);
|
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
properties = properties || {};
|
|
|
|
settings = settings || {};
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Set the strict mode to be false by default
|
|
|
|
if (settings.strict === undefined || settings.strict === null) {
|
|
|
|
settings.strict = false;
|
|
|
|
}
|
2013-08-26 20:38:24 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Set up the base model class
|
2018-12-07 14:54:29 +00:00
|
|
|
let ModelBaseClass = parent || this.defaultModelBaseClass;
|
|
|
|
const baseClass = settings.base || settings['super'];
|
2014-01-24 17:09:53 +00:00
|
|
|
if (baseClass) {
|
2014-10-10 11:42:59 +00:00
|
|
|
// Normalize base model property
|
|
|
|
settings.base = baseClass;
|
|
|
|
delete settings['super'];
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (isModelClass(baseClass)) {
|
|
|
|
ModelBaseClass = baseClass;
|
|
|
|
} else {
|
|
|
|
ModelBaseClass = this.models[baseClass];
|
|
|
|
assert(ModelBaseClass, 'Base model is not found: ' + baseClass);
|
2013-11-08 01:11:17 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-11-08 01:11:17 +00:00
|
|
|
|
2017-03-27 22:30:29 +00:00
|
|
|
// Assert current model's base class provides method `getMergePolicy()`.
|
|
|
|
assert(ModelBaseClass.getMergePolicy, `Base class ${ModelBaseClass.modelName}
|
|
|
|
does not provide method getMergePolicy(). Most likely it is not inheriting
|
|
|
|
from datasource-juggler's built-in default ModelBaseClass, which is an
|
|
|
|
incorrect usage of the framework.`);
|
|
|
|
|
|
|
|
// Initialize base model inheritance rank if not set already
|
|
|
|
ModelBaseClass.__rank = ModelBaseClass.__rank || 1;
|
|
|
|
|
2015-02-07 19:15:28 +00:00
|
|
|
// Make sure base properties are inherited
|
|
|
|
// See https://github.com/strongloop/loopback-datasource-juggler/issues/293
|
|
|
|
if ((parent && !settings.base) || (!parent && settings.base)) {
|
|
|
|
return ModelBaseClass.extend(className, properties, settings);
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Check if there is a unresolved model with the same name
|
2018-12-07 14:54:29 +00:00
|
|
|
let ModelClass = this.models[className];
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
// Create the ModelClass if it doesn't exist or it's resolved (override)
|
|
|
|
// TODO: [rfeng] We need to decide what names to use for built-in models such as User.
|
|
|
|
if (!ModelClass || !ModelClass.settings.unresolved) {
|
2018-01-08 08:25:31 +00:00
|
|
|
ModelClass = createModelClassCtor(className, ModelBaseClass);
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// mix in EventEmitter (don't inherit from)
|
2018-12-07 14:54:29 +00:00
|
|
|
const events = new EventEmitter();
|
2014-07-28 20:02:00 +00:00
|
|
|
// The model can have more than 10 listeners for lazy relationship setup
|
|
|
|
// See https://github.com/strongloop/loopback/issues/404
|
|
|
|
events.setMaxListeners(32);
|
2018-12-07 14:54:29 +00:00
|
|
|
for (const f in EventEmitter.prototype) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (typeof EventEmitter.prototype[f] === 'function') {
|
2016-06-06 17:33:01 +00:00
|
|
|
ModelClass[f] = EventEmitter.prototype[f].bind(events);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-11-05 06:53:02 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
hiddenProperty(ModelClass, 'modelName', className);
|
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2017-03-27 22:30:29 +00:00
|
|
|
// Iterate sub model inheritance rank over base model rank
|
|
|
|
ModelClass.__rank = ModelBaseClass.__rank + 1;
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
util.inherits(ModelClass, ModelBaseClass);
|
2013-12-11 22:45:27 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// store class in model pool
|
|
|
|
this.models[className] = ModelClass;
|
2013-11-05 06:53:02 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Return the unresolved model
|
|
|
|
if (settings.unresolved) {
|
2016-08-19 17:46:59 +00:00
|
|
|
ModelClass.settings = {unresolved: true};
|
2014-01-24 17:09:53 +00:00
|
|
|
return ModelClass;
|
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Add metadata to the ModelClass
|
|
|
|
hiddenProperty(ModelClass, 'modelBuilder', modelBuilder);
|
2014-08-08 08:20:57 +00:00
|
|
|
hiddenProperty(ModelClass, 'dataSource', null); // Keep for back-compatibility
|
2014-01-24 17:09:53 +00:00
|
|
|
hiddenProperty(ModelClass, 'pluralModelName', pluralName);
|
|
|
|
hiddenProperty(ModelClass, 'relations', {});
|
2015-04-24 15:23:13 +00:00
|
|
|
if (pathName[0] !== '/') {
|
|
|
|
// Support both flavors path: 'x' and path: '/x'
|
|
|
|
pathName = '/' + pathName;
|
|
|
|
}
|
2016-08-19 17:46:59 +00:00
|
|
|
hiddenProperty(ModelClass, 'http', {path: pathName});
|
2014-07-21 17:21:30 +00:00
|
|
|
hiddenProperty(ModelClass, 'base', ModelBaseClass);
|
2015-01-19 12:39:31 +00:00
|
|
|
hiddenProperty(ModelClass, '_observers', {});
|
2016-05-31 22:10:44 +00:00
|
|
|
hiddenProperty(ModelClass, '_warned', {});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
// inherit ModelBaseClass static methods
|
2018-12-07 15:22:36 +00:00
|
|
|
for (const i in ModelBaseClass) {
|
2014-01-24 17:09:53 +00:00
|
|
|
// We need to skip properties that are already in the subclass, for example, the event emitter methods
|
|
|
|
if (i !== '_mixins' && !(i in ModelClass)) {
|
|
|
|
ModelClass[i] = ModelBaseClass[i];
|
2013-05-17 17:54:14 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-12-19 00:13:41 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Load and inject the model classes
|
|
|
|
if (settings.models) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Object.keys(settings.models).forEach(function(m) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const model = settings.models[m];
|
2014-01-24 17:09:53 +00:00
|
|
|
ModelClass[m] = typeof model === 'string' ? modelBuilder.getModel(model, true) : model;
|
|
|
|
});
|
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
ModelClass.getter = {};
|
|
|
|
ModelClass.setter = {};
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
for (const p in properties) {
|
2017-05-09 23:31:54 +00:00
|
|
|
// e.g excludePropertyList = ['id'] - base properties listed in excludePropertyList will be excluded from the model.
|
|
|
|
// excludeBaseProperties is introduced in SOAP model generation only for now and below logic
|
|
|
|
// handles excludeBaseProperties. Generated SOAP model has base as 'Model' which means 'id' property gets added
|
|
|
|
// automatically and 'id' property shouldn't be there for SOAP models. idInjection = false will not work
|
|
|
|
// for SOAP generator case, since base 'Model' has already id property. 'id: false' at the property level will not
|
|
|
|
// work either for SOAP generator case since generators use ModelDefinition.create to create property in the model
|
|
|
|
// dynamically, that execution path has strict validation where doesn't accept 'id: false' in a property.
|
|
|
|
// See https://github.com/strongloop/loopback-workspace/issues/486 for some more details.
|
2018-12-07 14:54:29 +00:00
|
|
|
const excludePropertyList = settings['excludeBaseProperties'];
|
2017-05-09 23:31:54 +00:00
|
|
|
// Remove properties that reverted by the subclass of the property from excludePropertyList
|
|
|
|
if (properties[p] === null || properties[p] === false ||
|
|
|
|
(excludePropertyList != null && excludePropertyList.indexOf(p) != -1)) {
|
2015-03-03 18:27:22 +00:00
|
|
|
// Hide the base property
|
|
|
|
delete properties[p];
|
|
|
|
}
|
2015-04-02 07:49:04 +00:00
|
|
|
|
2016-05-26 21:21:37 +00:00
|
|
|
// Throw error for properties with unsupported names
|
2015-04-02 07:49:04 +00:00
|
|
|
if (/\./.test(p)) {
|
2016-07-22 19:26:07 +00:00
|
|
|
throw new Error(g.f('Property names containing dot(s) are not supported. ' +
|
|
|
|
'Model: %s, property: %s', className, p));
|
2015-04-02 07:49:04 +00:00
|
|
|
}
|
2016-04-01 11:48:17 +00:00
|
|
|
|
2016-01-19 02:48:58 +00:00
|
|
|
// Warn if property name is 'constructor'
|
2016-04-01 11:48:17 +00:00
|
|
|
if (p === 'constructor') {
|
2016-07-22 19:26:07 +00:00
|
|
|
deprecated(g.f('Property name should not be "{{constructor}}" in Model: %s', className));
|
2016-01-19 02:48:58 +00:00
|
|
|
}
|
2015-03-03 18:27:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelDefinition = new ModelDefinition(this, className, properties, settings);
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
this.definitions[className] = modelDefinition;
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// expose properties on the ModelClass
|
|
|
|
ModelClass.definition = modelDefinition;
|
|
|
|
// keep a pointer to settings as models can use it for configuration
|
|
|
|
ModelClass.settings = modelDefinition.settings;
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let idInjection = settings.idInjection;
|
2014-01-24 17:09:53 +00:00
|
|
|
if (idInjection !== false) {
|
|
|
|
// Default to true if undefined
|
|
|
|
idInjection = true;
|
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let idNames = modelDefinition.idNames();
|
2014-01-24 17:09:53 +00:00
|
|
|
if (idNames.length > 0) {
|
|
|
|
// id already exists
|
|
|
|
idInjection = false;
|
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Add the id property
|
|
|
|
if (idInjection) {
|
|
|
|
// Set up the id property
|
2016-08-19 17:46:59 +00:00
|
|
|
ModelClass.definition.defineProperty('id', {type: Number, id: 1, generated: true});
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-10-07 04:13:52 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
idNames = modelDefinition.idNames(); // Reload it after rebuild
|
|
|
|
// Create a virtual property 'id'
|
|
|
|
if (idNames.length === 1) {
|
2018-12-07 15:22:36 +00:00
|
|
|
const idProp = idNames[0];
|
2014-01-24 17:09:53 +00:00
|
|
|
if (idProp !== 'id') {
|
|
|
|
Object.defineProperty(ModelClass.prototype, 'id', {
|
2016-04-01 11:48:17 +00:00
|
|
|
get: function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const idProp = ModelClass.definition.idNames()[0];
|
2014-01-24 17:09:53 +00:00
|
|
|
return this.__data[idProp];
|
|
|
|
},
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
enumerable: false,
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Now the id property is an object that consists of multiple keys
|
|
|
|
Object.defineProperty(ModelClass.prototype, 'id', {
|
2016-04-01 11:48:17 +00:00
|
|
|
get: function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const compositeId = {};
|
|
|
|
const idNames = ModelClass.definition.idNames();
|
2018-12-07 15:22:36 +00:00
|
|
|
for (let i = 0, p; i < idNames.length; i++) {
|
2015-01-22 02:39:47 +00:00
|
|
|
p = idNames[i];
|
2014-01-24 17:09:53 +00:00
|
|
|
compositeId[p] = this.__data[p];
|
2013-10-07 04:13:52 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
return compositeId;
|
|
|
|
},
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
enumerable: false,
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
}
|
2013-10-07 04:13:52 +00:00
|
|
|
|
2017-08-22 20:09:45 +00:00
|
|
|
// updateOnly property is added to indicate that this property will appear in
|
|
|
|
// the model for update/updateorcreate operations but and not for create operation.
|
2018-12-07 14:54:29 +00:00
|
|
|
let forceId = ModelClass.settings.forceId;
|
2017-08-22 20:09:45 +00:00
|
|
|
if (idNames.length > 0) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const idName = modelDefinition.idName();
|
2018-12-07 15:22:36 +00:00
|
|
|
const idProp = ModelClass.definition.rawProperties[idName];
|
2017-08-22 20:09:45 +00:00
|
|
|
if (idProp.generated && forceId !== false) {
|
|
|
|
forceId = 'auto';
|
|
|
|
} else if (!idProp.generated && forceId === 'auto') {
|
|
|
|
// One of our parent models has enabled forceId because
|
|
|
|
// it uses an auto-generated id property. However,
|
|
|
|
// this particular model does not use auto-generated id,
|
|
|
|
// therefore we need to disable `forceId`.
|
|
|
|
forceId = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forceId) {
|
|
|
|
ModelClass.validatesAbsenceOf(idName, {if: 'isNewRecord'});
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelClass.definition.properties[idName].updateOnly = !!forceId;
|
|
|
|
ModelClass.definition.rawProperties[idName].updateOnly = !!forceId;
|
|
|
|
|
|
|
|
ModelClass.settings.forceId = forceId;
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// A function to loop through the properties
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelClass.forEachProperty = function(cb) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const props = ModelClass.definition.properties;
|
|
|
|
const keys = Object.keys(props);
|
|
|
|
for (let i = 0, n = keys.length; i < n; i++) {
|
2014-06-13 06:35:20 +00:00
|
|
|
cb(keys[i], props[keys[i]]);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A function to attach the model class to a data source
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelClass.attachTo = function(dataSource) {
|
2014-01-24 17:09:53 +00:00
|
|
|
dataSource.attach(this);
|
|
|
|
};
|
|
|
|
|
2014-06-17 20:18:18 +00:00
|
|
|
/** Extend the model with the specified model, properties, and other settings.
|
|
|
|
* For example, to extend an existing model, for example, a built-in model:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* var Customer = User.extend('customer', {
|
|
|
|
* accountId: String,
|
|
|
|
* vip: Boolean
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* To extend the base model, essentially creating a new model:
|
|
|
|
* ```js
|
|
|
|
* var user = loopback.Model.extend('user', properties, options);
|
|
|
|
* ```
|
2015-01-19 12:39:31 +00:00
|
|
|
*
|
2014-06-17 20:18:18 +00:00
|
|
|
* @param {String} className Name of the new model being defined.
|
2017-03-27 22:30:29 +00:00
|
|
|
* @options {Object} subClassProperties child model properties, added to base model
|
|
|
|
* properties.
|
|
|
|
* @options {Object} subClassSettings child model settings such as relations and acls,
|
|
|
|
* merged with base model settings.
|
2015-01-19 12:39:31 +00:00
|
|
|
*/
|
2017-03-27 22:30:29 +00:00
|
|
|
ModelClass.extend = function(className, subClassProperties, subClassSettings) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const baseClassProperties = ModelClass.definition.properties;
|
|
|
|
const baseClassSettings = ModelClass.definition.settings;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2017-03-27 22:30:29 +00:00
|
|
|
subClassProperties = subClassProperties || {};
|
|
|
|
subClassSettings = subClassSettings || {};
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
// Check if subclass redefines the ids
|
2018-12-07 14:54:29 +00:00
|
|
|
let idFound = false;
|
|
|
|
for (const k in subClassProperties) {
|
2017-03-27 22:30:29 +00:00
|
|
|
if (subClassProperties[k] && subClassProperties[k].id) {
|
2014-01-24 17:09:53 +00:00
|
|
|
idFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Merging the properties
|
2018-12-07 14:54:29 +00:00
|
|
|
const keys = Object.keys(baseClassProperties);
|
|
|
|
for (let i = 0, n = keys.length; i < n; i++) {
|
|
|
|
const key = keys[i];
|
2014-06-13 06:35:20 +00:00
|
|
|
|
2017-03-27 22:30:29 +00:00
|
|
|
if (idFound && baseClassProperties[key].id) {
|
2014-01-24 17:09:53 +00:00
|
|
|
// don't inherit id properties
|
2014-06-13 06:35:20 +00:00
|
|
|
continue;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2017-03-27 22:30:29 +00:00
|
|
|
if (subClassProperties[key] === undefined) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const baseProp = baseClassProperties[key];
|
|
|
|
let basePropCopy = baseProp;
|
2015-07-10 17:03:51 +00:00
|
|
|
if (baseProp && typeof baseProp === 'object') {
|
2017-03-27 22:30:29 +00:00
|
|
|
// Deep clone the base properties
|
|
|
|
basePropCopy = deepMerge(baseProp);
|
2015-07-10 17:03:51 +00:00
|
|
|
}
|
2017-03-27 22:30:29 +00:00
|
|
|
subClassProperties[key] = basePropCopy;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
}
|
2013-12-06 23:52:39 +00:00
|
|
|
|
2017-03-27 22:30:29 +00:00
|
|
|
// Merging the settings
|
2018-12-07 14:54:29 +00:00
|
|
|
const originalSubclassSettings = subClassSettings;
|
|
|
|
const mergePolicy = ModelClass.getMergePolicy(subClassSettings);
|
2017-03-27 22:30:29 +00:00
|
|
|
subClassSettings = mergeSettings(baseClassSettings, subClassSettings, mergePolicy);
|
2013-10-02 22:18:50 +00:00
|
|
|
|
2014-10-10 11:42:59 +00:00
|
|
|
// Ensure 'base' is not inherited. Note we don't have to delete 'super'
|
|
|
|
// as that is removed from settings by modelBuilder.define and thus
|
|
|
|
// it is never inherited
|
2015-02-07 19:15:28 +00:00
|
|
|
if (!originalSubclassSettings.base) {
|
2017-03-27 22:30:29 +00:00
|
|
|
subClassSettings.base = ModelClass;
|
2015-02-07 19:15:28 +00:00
|
|
|
}
|
2014-10-10 11:42:59 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Define the subclass
|
2018-12-07 14:54:29 +00:00
|
|
|
const subClass = modelBuilder.define(className, subClassProperties, subClassSettings, ModelClass);
|
2013-06-12 22:45:31 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Calling the setup function
|
|
|
|
if (typeof subClass.setup === 'function') {
|
|
|
|
subClass.setup.call(subClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
return subClass;
|
|
|
|
};
|
|
|
|
|
2017-03-27 22:30:29 +00:00
|
|
|
/*
|
|
|
|
* Merge parent and child model settings according to the provided merge policy.
|
|
|
|
*
|
|
|
|
* Below is presented the expected merge behaviour for each option of the policy.
|
|
|
|
* NOTE: This applies to top-level settings properties
|
|
|
|
*
|
|
|
|
* - Any
|
|
|
|
* - `{replace: true}` (default): child replaces the value from parent
|
2017-05-19 17:31:24 +00:00
|
|
|
* - assigning `null` on child setting deletes the inherited setting
|
2017-03-27 22:30:29 +00:00
|
|
|
*
|
|
|
|
* - Arrays:
|
|
|
|
* - `{replace: false}`: unique elements of parent and child cumulate
|
|
|
|
* - `{rank: true}` adds the model inheritance rank to array
|
|
|
|
* elements of type Object {} as internal property `__rank`
|
|
|
|
*
|
|
|
|
* - Object {}:
|
|
|
|
* - `{replace: false}`: deep merges parent and child objects
|
|
|
|
* - `{patch: true}`: child replaces inner properties from parent
|
|
|
|
*
|
|
|
|
* Here is an example of merge policy:
|
|
|
|
* ```
|
|
|
|
* {
|
|
|
|
* description: {replace: true}, // string or array
|
|
|
|
* properties: {patch: true}, // object
|
|
|
|
* hidden: {replace: false}, // array
|
|
|
|
* protected: {replace: false}, // array
|
|
|
|
* relations: {acls: true}, // object
|
|
|
|
* acls: {rank: true}, // array
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {Object} baseClassSettings parent model settings.
|
|
|
|
* @param {Object} subClassSettings child model settings.
|
|
|
|
* @param {Object} mergePolicy merge policy, as defined in `ModelClass.getMergePolicy()`
|
|
|
|
* @return {Object} mergedSettings merged parent and child models settings.
|
|
|
|
*/
|
|
|
|
function mergeSettings(baseClassSettings, subClassSettings, mergePolicy) {
|
|
|
|
// deep clone base class settings
|
2018-12-07 14:54:29 +00:00
|
|
|
const mergedSettings = deepMerge(baseClassSettings);
|
2017-03-27 22:30:29 +00:00
|
|
|
|
|
|
|
Object.keys(baseClassSettings).forEach(function(key) {
|
|
|
|
// rank base class settings arrays elements where required
|
|
|
|
if (mergePolicy[key] && mergePolicy[key].rank) {
|
|
|
|
baseClassSettings[key] = rankArrayElements(baseClassSettings[key], ModelBaseClass.__rank);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(subClassSettings).forEach(function(key) {
|
|
|
|
// assign default merge policy to unknown settings if specified
|
|
|
|
// if none specified, a deep merge will be applied eventually
|
|
|
|
if (mergePolicy[key] == null) { // undefined or null
|
|
|
|
mergePolicy[key] = mergePolicy.__default || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow null value to remove unwanted settings from base class settings
|
|
|
|
if (subClassSettings[key] === mergePolicy.__delete) {
|
|
|
|
delete mergedSettings[key];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// rank sub class settings arrays elements where required
|
|
|
|
if (mergePolicy[key].rank) {
|
|
|
|
subClassSettings[key] = rankArrayElements(subClassSettings[key], ModelBaseClass.__rank + 1);
|
|
|
|
}
|
|
|
|
// replace base class settings where required
|
|
|
|
if (mergePolicy[key].replace) {
|
|
|
|
mergedSettings[key] = subClassSettings[key];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// patch inner properties of base class settings where required
|
|
|
|
if (mergePolicy[key].patch) {
|
|
|
|
// mergedSettings[key] might not be initialized
|
|
|
|
mergedSettings[key] = mergedSettings[key] || {};
|
|
|
|
Object.keys(subClassSettings[key]).forEach(function(innerKey) {
|
|
|
|
mergedSettings[key][innerKey] = subClassSettings[key][innerKey];
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in case no merge policy matched, apply a deep merge
|
|
|
|
// this for example handles {replace: false} and {rank: true}
|
|
|
|
mergedSettings[key] = deepMergeProperty(baseClassSettings[key], subClassSettings[key]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return mergedSettings;
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
/**
|
|
|
|
* Register a property for the model class
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {String} propertyName Name of the property.
|
2014-01-24 17:09:53 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelClass.registerProperty = function(propertyName) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const properties = modelDefinition.build();
|
|
|
|
const prop = properties[propertyName];
|
|
|
|
const DataType = prop.type;
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!DataType) {
|
2016-07-22 19:26:07 +00:00
|
|
|
throw new Error(g.f('Invalid type for property %s', propertyName));
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prop.required) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const requiredOptions = typeof prop.required === 'object' ? prop.required : undefined;
|
2014-01-24 17:09:53 +00:00
|
|
|
ModelClass.validatesPresenceOf(propertyName, requiredOptions);
|
|
|
|
}
|
2017-04-21 18:12:04 +00:00
|
|
|
if (DataType === Date) ModelClass.validatesDateOf(propertyName);
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
Object.defineProperty(ModelClass.prototype, propertyName, {
|
2016-04-01 11:48:17 +00:00
|
|
|
get: function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (ModelClass.getter[propertyName]) {
|
|
|
|
return ModelClass.getter[propertyName].call(this); // Try getter first
|
|
|
|
} else {
|
|
|
|
return this.__data && this.__data[propertyName]; // Try __data
|
2013-05-17 17:54:14 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
},
|
2016-04-01 11:48:17 +00:00
|
|
|
set: function(value) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let DataType = ModelClass.definition.properties[propertyName].type;
|
2014-02-11 06:38:59 +00:00
|
|
|
if (Array.isArray(DataType) || DataType === Array) {
|
|
|
|
DataType = List;
|
2014-06-13 06:35:20 +00:00
|
|
|
} else if (DataType === Date) {
|
|
|
|
DataType = DateType;
|
2015-02-08 18:54:42 +00:00
|
|
|
} else if (DataType === Boolean) {
|
|
|
|
DataType = BooleanType;
|
2014-02-11 06:38:59 +00:00
|
|
|
} else if (typeof DataType === 'string') {
|
|
|
|
DataType = modelBuilder.resolveType(DataType);
|
|
|
|
}
|
2015-03-27 09:45:14 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const persistUndefinedAsNull = ModelClass.definition.settings.persistUndefinedAsNull;
|
2015-03-27 09:45:14 +00:00
|
|
|
if (value === undefined && persistUndefinedAsNull) {
|
|
|
|
value = null;
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (ModelClass.setter[propertyName]) {
|
|
|
|
ModelClass.setter[propertyName].call(this, value); // Try setter first
|
|
|
|
} else {
|
2014-06-13 06:35:20 +00:00
|
|
|
this.__data = this.__data || {};
|
2014-01-24 17:09:53 +00:00
|
|
|
if (value === null || value === undefined) {
|
|
|
|
this.__data[propertyName] = value;
|
|
|
|
} else {
|
|
|
|
if (DataType === List) {
|
|
|
|
this.__data[propertyName] = DataType(value, properties[propertyName].type, this.__data);
|
|
|
|
} else {
|
|
|
|
// Assume the type constructor handles Constructor() call
|
|
|
|
// If not, we should call new DataType(value).valueOf();
|
2014-02-26 20:10:15 +00:00
|
|
|
this.__data[propertyName] = (value instanceof DataType) ? value : DataType(value);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-28 20:17:12 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
},
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
enumerable: true,
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// FIXME: [rfeng] Do we need to keep the raw data?
|
|
|
|
// Use $ as the prefix to avoid conflicts with properties such as _id
|
|
|
|
Object.defineProperty(ModelClass.prototype, '$' + propertyName, {
|
2016-04-01 11:48:17 +00:00
|
|
|
get: function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
return this.__data && this.__data[propertyName];
|
|
|
|
},
|
2016-04-01 11:48:17 +00:00
|
|
|
set: function(value) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!this.__data) {
|
|
|
|
this.__data = {};
|
|
|
|
}
|
|
|
|
this.__data[propertyName] = value;
|
|
|
|
},
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
enumerable: false,
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
};
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const props = ModelClass.definition.properties;
|
|
|
|
let keys = Object.keys(props);
|
|
|
|
let size = keys.length;
|
2018-12-07 15:22:36 +00:00
|
|
|
for (let i = 0; i < size; i++) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const propertyName = keys[i];
|
2014-06-13 06:35:20 +00:00
|
|
|
ModelClass.registerProperty(propertyName);
|
|
|
|
}
|
2013-11-05 06:53:02 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const mixinSettings = settings.mixins || {};
|
2014-08-08 08:20:57 +00:00
|
|
|
keys = Object.keys(mixinSettings);
|
|
|
|
size = keys.length;
|
2018-12-07 15:22:36 +00:00
|
|
|
for (let i = 0; i < size; i++) {
|
|
|
|
const name = keys[i];
|
2018-12-07 14:54:29 +00:00
|
|
|
let mixin = mixinSettings[name];
|
2014-08-08 08:20:57 +00:00
|
|
|
if (mixin === true) {
|
|
|
|
mixin = {};
|
|
|
|
}
|
2014-08-17 10:29:04 +00:00
|
|
|
if (Array.isArray(mixin)) {
|
|
|
|
mixin.forEach(function(m) {
|
|
|
|
if (m === true) m = {};
|
|
|
|
if (typeof m === 'object') {
|
|
|
|
modelBuilder.mixins.applyMixin(ModelClass, name, m);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (typeof mixin === 'object') {
|
2014-08-08 08:20:57 +00:00
|
|
|
modelBuilder.mixins.applyMixin(ModelClass, name, mixin);
|
2014-08-05 14:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-19 12:39:31 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
ModelClass.emit('defined', ModelClass);
|
|
|
|
|
|
|
|
return ModelClass;
|
2013-05-17 17:54:14 +00:00
|
|
|
};
|
|
|
|
|
2018-01-08 08:25:31 +00:00
|
|
|
function createModelClassCtor(name, ModelBaseClass) {
|
|
|
|
// A simple sanitization to handle most common characters
|
|
|
|
// that are used in model names but cannot be used as a function/class name.
|
|
|
|
// Note that the rules for valid JS indentifiers are way too complex,
|
|
|
|
// implementing a fully spec-compliant sanitization is not worth the effort.
|
|
|
|
// See https://mathiasbynens.be/notes/javascript-identifiers-es6
|
|
|
|
name = name.replace(/[-.:]/g, '_');
|
|
|
|
|
|
|
|
try {
|
|
|
|
// It is not possible to access closure variables like "ModelBaseClass"
|
|
|
|
// from a dynamically defined function. The solution is to
|
|
|
|
// create a dynamically defined factory function that accepts
|
|
|
|
// closure variables as arguments.
|
|
|
|
const factory = new Function('ModelBaseClass', `
|
|
|
|
// every class can receive hash of data as optional param
|
|
|
|
return function ${name}(data, options) {
|
|
|
|
if (!(this instanceof ${name})) {
|
|
|
|
return new ${name}(data, options);
|
|
|
|
}
|
|
|
|
if (${name}.settings.unresolved) {
|
|
|
|
throw new Error(g.f('Model %s is not defined.', ${JSON.stringify(name)}));
|
|
|
|
}
|
|
|
|
ModelBaseClass.apply(this, arguments);
|
|
|
|
};`);
|
|
|
|
|
|
|
|
return factory(ModelBaseClass);
|
|
|
|
} catch (err) {
|
|
|
|
// modelName is not a valid function/class name, e.g. 'grand-child'
|
|
|
|
// and our simple sanitization was not good enough.
|
|
|
|
// Falling back to legacy 'ModelConstructor' name.
|
|
|
|
if (err.name === 'SyntaxError') {
|
|
|
|
return createModelClassCtor('ModelConstructor', ModelBaseClass);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
// DataType for Date
|
|
|
|
function DateType(arg) {
|
2017-04-21 18:12:04 +00:00
|
|
|
if (arg === null) return null;
|
2018-12-07 14:54:29 +00:00
|
|
|
const d = new Date(arg);
|
2015-02-08 18:54:42 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relax the Boolean coercision
|
|
|
|
function BooleanType(arg) {
|
|
|
|
if (typeof arg === 'string') {
|
|
|
|
switch (arg) {
|
|
|
|
case 'true':
|
|
|
|
case '1':
|
|
|
|
return true;
|
|
|
|
case 'false':
|
|
|
|
case '0':
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (arg == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return Boolean(arg);
|
2014-06-13 06:35:20 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 17:54:14 +00:00
|
|
|
/**
|
2013-10-02 05:14:21 +00:00
|
|
|
* Define single property named `propertyName` on `model`
|
2013-05-17 17:54:14 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {String} model Name of model
|
|
|
|
* @param {String} propertyName Name of property
|
|
|
|
* @param {Object} propertyDefinition Property settings
|
2013-05-17 17:54:14 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBuilder.prototype.defineProperty = function(model, propertyName, propertyDefinition) {
|
2014-01-24 17:09:53 +00:00
|
|
|
this.definitions[model].defineProperty(propertyName, propertyDefinition);
|
|
|
|
this.models[model].registerProperty(propertyName);
|
2013-05-17 17:54:14 +00:00
|
|
|
};
|
|
|
|
|
2014-05-23 11:18:06 +00:00
|
|
|
/**
|
|
|
|
* Define a new value type that can be used in model schemas as a property type.
|
|
|
|
* @param {function()} type Type constructor.
|
|
|
|
* @param {string[]=} aliases Optional list of alternative names for this type.
|
|
|
|
*/
|
|
|
|
ModelBuilder.prototype.defineValueType = function(type, aliases) {
|
|
|
|
ModelBuilder.registerType(type, aliases);
|
|
|
|
};
|
|
|
|
|
2013-05-17 17:54:14 +00:00
|
|
|
/**
|
2014-06-17 20:18:18 +00:00
|
|
|
* Extend existing model with specified properties
|
2013-05-17 17:54:14 +00:00
|
|
|
*
|
|
|
|
* Example:
|
2014-06-17 20:18:18 +00:00
|
|
|
* Instead of extending a model with attributes like this (for example):
|
2015-01-19 12:39:31 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* ```js
|
2014-06-17 20:18:18 +00:00
|
|
|
* db.defineProperty('Content', 'competitionType',
|
|
|
|
* { type: String });
|
|
|
|
* db.defineProperty('Content', 'expiryDate',
|
|
|
|
* { type: Date, index: true });
|
|
|
|
* db.defineProperty('Content', 'isExpired',
|
|
|
|
* { type: Boolean, index: true });
|
2014-03-12 23:28:46 +00:00
|
|
|
*```
|
2014-06-17 20:18:18 +00:00
|
|
|
* This method enables you to extend a model as follows (for example):
|
2014-03-12 23:28:46 +00:00
|
|
|
* ```js
|
2013-05-17 17:54:14 +00:00
|
|
|
* db.extendModel('Content', {
|
|
|
|
* competitionType: String,
|
|
|
|
* expiryDate: { type: Date, index: true },
|
|
|
|
* isExpired: { type: Boolean, index: true }
|
|
|
|
* });
|
2014-03-12 23:28:46 +00:00
|
|
|
*```
|
|
|
|
*
|
|
|
|
* @param {String} model Name of model
|
2015-01-19 12:39:31 +00:00
|
|
|
* @options {Object} properties JSON object specifying properties. Each property is a key whos value is
|
2014-08-11 18:42:21 +00:00
|
|
|
* either the [type](http://docs.strongloop.com/display/LB/LoopBack+types) or `propertyName: {options}`
|
2014-06-17 20:18:18 +00:00
|
|
|
* where the options are described below.
|
2014-08-11 18:42:21 +00:00
|
|
|
* @property {String} type Datatype of property: Must be an [LDL type](http://docs.strongloop.com/display/LB/LoopBack+types).
|
2014-06-17 20:18:18 +00:00
|
|
|
* @property {Boolean} index True if the property is an index; false otherwise.
|
2013-05-17 17:54:14 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBuilder.prototype.extendModel = function(model, props) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const t = this;
|
|
|
|
const keys = Object.keys(props);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const definition = props[keys[i]];
|
2014-06-13 06:35:20 +00:00
|
|
|
t.defineProperty(model, keys[i], definition);
|
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
};
|
|
|
|
|
2013-05-24 05:20:20 +00:00
|
|
|
ModelBuilder.prototype.copyModel = function copyModel(Master) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelBuilder = this;
|
|
|
|
const className = Master.modelName;
|
|
|
|
const md = Master.modelBuilder.definitions[className];
|
|
|
|
const Slave = function SlaveModel() {
|
2014-01-24 17:09:53 +00:00
|
|
|
Master.apply(this, [].slice.call(arguments));
|
|
|
|
};
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
util.inherits(Slave, Master);
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
Slave.__proto__ = Master;
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
hiddenProperty(Slave, 'modelBuilder', modelBuilder);
|
|
|
|
hiddenProperty(Slave, 'modelName', className);
|
|
|
|
hiddenProperty(Slave, 'relations', Master.relations);
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!(className in modelBuilder.models)) {
|
|
|
|
// store class in model pool
|
|
|
|
modelBuilder.models[className] = Slave;
|
|
|
|
modelBuilder.definitions[className] = {
|
|
|
|
properties: md.properties,
|
2016-04-01 11:48:17 +00:00
|
|
|
settings: md.settings,
|
2014-01-24 17:09:53 +00:00
|
|
|
};
|
|
|
|
}
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
return Slave;
|
2013-05-17 17:54:14 +00:00
|
|
|
};
|
|
|
|
|
2018-04-16 11:28:17 +00:00
|
|
|
/**
|
|
|
|
* Remove a model from the registry.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
*/
|
|
|
|
ModelBuilder.prototype.deleteModelByName = function(modelName) {
|
|
|
|
delete this.models[modelName];
|
|
|
|
delete this.definitions[modelName];
|
|
|
|
};
|
|
|
|
|
2013-08-13 16:37:09 +00:00
|
|
|
/*!
|
2013-05-17 17:54:14 +00:00
|
|
|
* Define hidden property
|
|
|
|
*/
|
|
|
|
function hiddenProperty(where, property, value) {
|
2014-01-24 17:09:53 +00:00
|
|
|
Object.defineProperty(where, property, {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: value,
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-05-17 17:54:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 05:20:20 +00:00
|
|
|
/**
|
2017-05-19 17:31:24 +00:00
|
|
|
* Get the schema name. If no parameter is given, then an anonymous model name
|
|
|
|
* is generated and returned.
|
|
|
|
* @param {string=} name The optional name parameter.
|
|
|
|
* @returns {string} The schema name.
|
2013-05-24 05:20:20 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBuilder.prototype.getSchemaName = function(name) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (name) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
if (typeof this._nameCount !== 'number') {
|
|
|
|
this._nameCount = 0;
|
|
|
|
} else {
|
|
|
|
this._nameCount++;
|
|
|
|
}
|
|
|
|
return 'AnonymousModel_' + this._nameCount;
|
2013-08-09 22:16:32 +00:00
|
|
|
};
|
2013-05-24 05:20:20 +00:00
|
|
|
|
2013-10-17 21:23:29 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Resolve the type string to be a function, for example, 'String' to String.
|
|
|
|
* Returns {Function} if the type is resolved
|
2017-05-19 17:31:24 +00:00
|
|
|
* @param {String} type The type string, such as 'number', 'Number', 'boolean',
|
|
|
|
* or 'String'. This parameter is case insensitive.
|
2013-10-17 21:23:29 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBuilder.prototype.resolveType = function(type) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!type) {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
if (Array.isArray(type) && type.length > 0) {
|
|
|
|
// For array types, the first item should be the type string
|
2018-12-07 14:54:29 +00:00
|
|
|
const itemType = this.resolveType(type[0]);
|
2014-01-24 17:09:53 +00:00
|
|
|
if (typeof itemType === 'function') {
|
|
|
|
return [itemType];
|
2016-04-01 13:23:42 +00:00
|
|
|
} else {
|
2014-01-24 17:09:53 +00:00
|
|
|
return itemType; // Not resolved, return the type string
|
2013-10-17 21:23:29 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
if (typeof type === 'string') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const schemaType = ModelBuilder.schemaTypes[type.toLowerCase()] || this.models[type];
|
2014-01-24 17:09:53 +00:00
|
|
|
if (schemaType) {
|
|
|
|
return schemaType;
|
|
|
|
} else {
|
|
|
|
// The type cannot be resolved, let's create a place holder
|
2016-08-19 17:46:59 +00:00
|
|
|
type = this.define(type, {}, {unresolved: true});
|
2014-01-24 17:09:53 +00:00
|
|
|
return type;
|
2013-10-17 21:23:29 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
} else if (type.constructor.name === 'Object') {
|
|
|
|
// We also support the syntax {type: 'string', ...}
|
|
|
|
if (type.type) {
|
|
|
|
return this.resolveType(type.type);
|
|
|
|
} else {
|
|
|
|
return this.define(this.getSchemaName(null),
|
2016-05-27 22:21:53 +00:00
|
|
|
type, {
|
|
|
|
anonymous: true,
|
|
|
|
idInjection: false,
|
|
|
|
strict: this.settings.strictEmbeddedModels || false,
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
} else if ('function' === typeof type) {
|
2013-10-17 21:23:29 +00:00
|
|
|
return type;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
return type;
|
2013-10-17 21:23:29 +00:00
|
|
|
};
|
|
|
|
|
2013-05-24 05:20:20 +00:00
|
|
|
/**
|
2013-11-15 19:12:33 +00:00
|
|
|
* Build models from schema definitions
|
2013-08-13 16:37:09 +00:00
|
|
|
*
|
|
|
|
* `schemas` can be one of the following:
|
|
|
|
*
|
2013-11-15 19:12:33 +00:00
|
|
|
* 1. An array of named schema definition JSON objects
|
|
|
|
* 2. A schema definition JSON object
|
2013-05-24 05:20:20 +00:00
|
|
|
* 3. A list of property definitions (anonymous)
|
2013-08-13 16:37:09 +00:00
|
|
|
*
|
|
|
|
* @param {*} schemas The schemas
|
2017-05-19 17:31:24 +00:00
|
|
|
* @returns {Object.<string, ModelClass>} A map of model constructors keyed by
|
|
|
|
* model name.
|
2013-05-24 05:20:20 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBuilder.prototype.buildModels = function(schemas, createModel) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const models = {};
|
2013-05-24 05:20:20 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Normalize the schemas to be an array of the schema objects {name: <name>, properties: {}, options: {}}
|
|
|
|
if (!Array.isArray(schemas)) {
|
|
|
|
if (schemas.properties && schemas.name) {
|
|
|
|
// Only one item
|
|
|
|
schemas = [schemas];
|
|
|
|
} else {
|
|
|
|
// Anonymous schema
|
|
|
|
schemas = [
|
|
|
|
{
|
|
|
|
name: this.getSchemaName(),
|
|
|
|
properties: schemas,
|
2016-08-19 17:46:59 +00:00
|
|
|
options: {anonymous: true},
|
2016-04-01 11:48:17 +00:00
|
|
|
},
|
2014-01-24 17:09:53 +00:00
|
|
|
];
|
2013-05-24 05:20:20 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-05-24 05:20:20 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let relations = [];
|
|
|
|
for (let s = 0, n = schemas.length; s < n; s++) {
|
|
|
|
const name = this.getSchemaName(schemas[s].name);
|
2014-01-24 17:09:53 +00:00
|
|
|
schemas[s].name = name;
|
2018-12-07 15:22:36 +00:00
|
|
|
const model = typeof createModel === 'function' ?
|
|
|
|
createModel(schemas[s].name, schemas[s].properties, schemas[s].options) :
|
|
|
|
this.define(schemas[s].name, schemas[s].properties, schemas[s].options);
|
2014-01-24 17:09:53 +00:00
|
|
|
models[name] = model;
|
|
|
|
relations = relations.concat(model.definition.relations);
|
|
|
|
}
|
2013-05-24 05:20:20 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Connect the models based on the relations
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0; i < relations.length; i++) {
|
|
|
|
const relation = relations[i];
|
|
|
|
const sourceModel = models[relation.source];
|
|
|
|
const targetModel = models[relation.target];
|
2014-01-24 17:09:53 +00:00
|
|
|
if (sourceModel && targetModel) {
|
|
|
|
if (typeof sourceModel[relation.type] === 'function') {
|
2016-08-19 17:46:59 +00:00
|
|
|
sourceModel[relation.type](targetModel, {as: relation.as});
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-05-24 05:20:20 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
return models;
|
2013-08-09 22:16:32 +00:00
|
|
|
};
|
2013-05-24 05:20:20 +00:00
|
|
|
|
2013-08-07 21:51:32 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Introspect the JSON document to build a corresponding model.
|
2013-08-13 16:37:09 +00:00
|
|
|
* @param {String} name The model name
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {Object} json The JSON object
|
|
|
|
* @param {Object} options The options
|
2017-05-19 17:31:24 +00:00
|
|
|
* @returns {ModelClass} The generated model class constructor.
|
2013-08-07 21:51:32 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) {
|
2014-01-24 17:09:53 +00:00
|
|
|
// Introspect the JSON document to generate a schema
|
2018-12-07 14:54:29 +00:00
|
|
|
const schema = introspect(json);
|
2013-08-07 21:51:32 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Create a model for the generated schema
|
|
|
|
return this.define(name, schema, options);
|
2013-08-13 16:37:09 +00:00
|
|
|
};
|