2019-05-08 15:45:37 +00:00
|
|
|
// Copyright IBM Corp. 2013,2019. All Rights Reserved.
|
2016-04-01 22:25:16 +00:00
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-26 13:49:34 +00:00
|
|
|
// Turning on strict for this file breaks lots of test cases;
|
|
|
|
// disabling strict for this file
|
|
|
|
/* eslint-disable strict */
|
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
/*!
|
2013-04-11 23:23:34 +00:00
|
|
|
* Module exports class Model
|
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
module.exports = ModelBaseClass;
|
2013-04-11 23:23:34 +00:00
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Module dependencies
|
2011-10-10 13:22:51 +00:00
|
|
|
*/
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const g = require('strong-globalize')();
|
|
|
|
const util = require('util');
|
|
|
|
const jutil = require('./jutil');
|
|
|
|
const List = require('./list');
|
|
|
|
const DataAccessUtils = require('./model-utils');
|
|
|
|
const Observer = require('./observer');
|
|
|
|
const Hookable = require('./hooks');
|
|
|
|
const validations = require('./validations');
|
|
|
|
const _extend = util._extend;
|
|
|
|
const utils = require('./utils');
|
|
|
|
const fieldsToArray = utils.fieldsToArray;
|
|
|
|
const uuid = require('uuid');
|
|
|
|
const shortid = require('shortid');
|
2011-10-10 13:22:51 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
// Set up an object for quick lookup
|
2018-12-07 14:54:29 +00:00
|
|
|
const BASE_TYPES = {
|
2014-06-13 06:35:20 +00:00
|
|
|
'String': true,
|
|
|
|
'Boolean': true,
|
|
|
|
'Number': true,
|
|
|
|
'Date': true,
|
|
|
|
'Text': true,
|
2016-04-01 11:48:17 +00:00
|
|
|
'ObjectID': true,
|
2014-06-13 06:35:20 +00:00
|
|
|
};
|
2011-10-10 13:22:51 +00:00
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Model class: base class for all persistent objects.
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2013-05-17 15:49:57 +00:00
|
|
|
* `ModelBaseClass` mixes `Validatable` and `Hookable` classes methods
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* @class
|
|
|
|
* @param {Object} data Initial object data
|
2017-05-03 15:10:31 +00:00
|
|
|
* @param {Object} options An object to control the instantiation
|
|
|
|
* @returns {ModelBaseClass} an instance of the ModelBaseClass
|
2011-10-08 17:11:26 +00:00
|
|
|
*/
|
2014-02-11 06:38:59 +00:00
|
|
|
function ModelBaseClass(data, options) {
|
|
|
|
options = options || {};
|
2016-04-01 11:48:17 +00:00
|
|
|
if (!('applySetters' in options)) {
|
2014-02-11 06:38:59 +00:00
|
|
|
// Default to true
|
|
|
|
options.applySetters = true;
|
|
|
|
}
|
2015-06-18 22:19:45 +00:00
|
|
|
if (!('applyDefaultValues' in options)) {
|
|
|
|
options.applyDefaultValues = true;
|
|
|
|
}
|
2014-02-11 06:38:59 +00:00
|
|
|
this._initProperties(data, options);
|
2012-04-19 15:01:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 20:06:43 +00:00
|
|
|
/**
|
2014-02-11 06:38:59 +00:00
|
|
|
* Initialize the model instance with a list of properties
|
|
|
|
* @param {Object} data The data object
|
|
|
|
* @param {Object} options An object to control the instantiation
|
|
|
|
* @property {Boolean} applySetters Controls if the setters will be applied
|
2015-06-18 22:19:45 +00:00
|
|
|
* @property {Boolean} applyDefaultValues Default attributes and values will be applied
|
2014-02-11 06:38:59 +00:00
|
|
|
* @property {Boolean} strict Set the instance level strict mode
|
2014-09-05 14:35:01 +00:00
|
|
|
* @property {Boolean} persisted Whether the instance has been persisted
|
2013-07-26 20:06:43 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.prototype._initProperties = function(data, options) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
|
|
|
const ctor = this.constructor;
|
2017-05-03 15:10:31 +00:00
|
|
|
|
2017-09-28 00:12:30 +00:00
|
|
|
if (typeof data !== 'undefined' && data !== null && data.constructor &&
|
2016-01-19 02:48:58 +00:00
|
|
|
typeof (data.constructor) !== 'function') {
|
2016-07-22 19:26:07 +00:00
|
|
|
throw new Error(g.f('Property name "{{constructor}}" is not allowed in %s data', ctor.modelName));
|
2016-01-19 02:48:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
if (data instanceof ctor) {
|
2014-06-13 06:35:20 +00:00
|
|
|
// Convert the data to be plain object to avoid pollutions
|
2014-05-09 22:27:45 +00:00
|
|
|
data = data.toObject(false);
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const properties = _extend({}, ctor.definition.properties);
|
2014-01-24 17:09:53 +00:00
|
|
|
data = data || {};
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-09-06 09:13:47 +00:00
|
|
|
if (typeof ctor.applyProperties === 'function') {
|
|
|
|
ctor.applyProperties(data);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-02-11 06:38:59 +00:00
|
|
|
options = options || {};
|
2018-12-07 14:54:29 +00:00
|
|
|
const applySetters = options.applySetters;
|
|
|
|
const applyDefaultValues = options.applyDefaultValues;
|
|
|
|
let strict = options.strict;
|
2014-02-11 06:38:59 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
if (strict === undefined) {
|
2014-02-11 06:38:59 +00:00
|
|
|
strict = ctor.definition.settings.strict;
|
2016-09-07 18:24:48 +00:00
|
|
|
} else if (strict === 'throw') {
|
|
|
|
g.warn('Warning: Model %s, {{strict mode: `throw`}} has been removed, ' +
|
|
|
|
'please use {{`strict: true`}} instead, which returns' +
|
|
|
|
'{{`Validation Error`}} for unknown properties,', ctor.modelName);
|
2014-02-11 06:38:59 +00:00
|
|
|
}
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const persistUndefinedAsNull = ctor.definition.settings.persistUndefinedAsNull;
|
2015-03-27 09:45:14 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
if (ctor.hideInternalProperties) {
|
|
|
|
// Object.defineProperty() is expensive. We only try to make the internal
|
|
|
|
// properties hidden (non-enumerable) if the model class has the
|
|
|
|
// `hideInternalProperties` set to true
|
|
|
|
Object.defineProperties(this, {
|
|
|
|
__cachedRelations: {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: {},
|
2014-06-13 06:35:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
__data: {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: {},
|
2014-06-13 06:35:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Instance level data source
|
|
|
|
__dataSource: {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: options.dataSource,
|
2014-06-13 06:35:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Instance level strict mode
|
|
|
|
__strict: {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: strict,
|
2014-10-19 16:47:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
__persisted: {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: false,
|
2015-04-09 14:57:38 +00:00
|
|
|
},
|
2014-06-13 06:35:20 +00:00
|
|
|
});
|
2015-04-09 14:57:38 +00:00
|
|
|
|
2016-09-07 18:24:48 +00:00
|
|
|
if (strict) {
|
2015-04-09 14:57:38 +00:00
|
|
|
Object.defineProperty(this, '__unknownProperties', {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configrable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: [],
|
2015-04-09 14:57:38 +00:00
|
|
|
});
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
} else {
|
|
|
|
this.__cachedRelations = {};
|
|
|
|
this.__data = {};
|
|
|
|
this.__dataSource = options.dataSource;
|
|
|
|
this.__strict = strict;
|
2014-10-19 16:52:18 +00:00
|
|
|
this.__persisted = false;
|
2016-09-07 18:24:48 +00:00
|
|
|
if (strict) {
|
2015-04-09 14:57:38 +00:00
|
|
|
this.__unknownProperties = [];
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
}
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-10-19 16:47:27 +00:00
|
|
|
if (options.persisted !== undefined) {
|
|
|
|
this.__persisted = options.persisted === true;
|
|
|
|
}
|
2014-02-11 06:38:59 +00:00
|
|
|
|
2014-01-28 17:57:23 +00:00
|
|
|
if (data.__cachedRelations) {
|
|
|
|
this.__cachedRelations = data.__cachedRelations;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let keys = Object.keys(data);
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-10-19 16:47:27 +00:00
|
|
|
if (Array.isArray(options.fields)) {
|
|
|
|
keys = keys.filter(function(k) {
|
|
|
|
return (options.fields.indexOf(k) != -1);
|
|
|
|
});
|
|
|
|
}
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let size = keys.length;
|
|
|
|
let p, propVal;
|
2018-12-07 15:22:36 +00:00
|
|
|
for (let k = 0; k < size; k++) {
|
2014-06-13 06:35:20 +00:00
|
|
|
p = keys[k];
|
|
|
|
propVal = data[p];
|
|
|
|
if (typeof propVal === 'function') {
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-27 09:45:14 +00:00
|
|
|
|
|
|
|
if (propVal === undefined && persistUndefinedAsNull) {
|
|
|
|
propVal = null;
|
|
|
|
}
|
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
if (properties[p]) {
|
|
|
|
// Managed property
|
2014-09-06 12:38:57 +00:00
|
|
|
if (applySetters || properties[p].id) {
|
2014-06-13 06:35:20 +00:00
|
|
|
self[p] = propVal;
|
|
|
|
} else {
|
|
|
|
self.__data[p] = propVal;
|
|
|
|
}
|
|
|
|
} else if (ctor.relations[p]) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const relationType = ctor.relations[p].type;
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2018-12-07 15:22:36 +00:00
|
|
|
let modelTo;
|
2014-08-20 12:03:38 +00:00
|
|
|
if (!properties[p]) {
|
2015-05-20 05:32:03 +00:00
|
|
|
modelTo = ctor.relations[p].modelTo || ModelBaseClass;
|
2018-12-07 14:54:29 +00:00
|
|
|
const multiple = ctor.relations[p].multiple;
|
|
|
|
const typeName = multiple ? 'Array' : modelTo.modelName;
|
|
|
|
const propType = multiple ? [modelTo] : modelTo;
|
2016-08-19 17:46:59 +00:00
|
|
|
properties[p] = {name: typeName, type: propType};
|
2017-03-20 23:48:57 +00:00
|
|
|
/* Issue #1252
|
2014-08-20 12:03:38 +00:00
|
|
|
this.setStrict(false);
|
2017-03-20 23:48:57 +00:00
|
|
|
*/
|
2014-08-20 12:03:38 +00:00
|
|
|
}
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
// Relation
|
2014-08-21 08:44:55 +00:00
|
|
|
if (relationType === 'belongsTo' && propVal != null) {
|
2014-03-13 23:43:38 +00:00
|
|
|
// If the related model is populated
|
2014-06-13 06:35:20 +00:00
|
|
|
self.__data[ctor.relations[p].keyFrom] = propVal[ctor.relations[p].keyTo];
|
2015-01-19 14:49:20 +00:00
|
|
|
|
|
|
|
if (ctor.relations[p].options.embedsProperties) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const fields = fieldsToArray(ctor.relations[p].properties,
|
2015-06-01 03:28:58 +00:00
|
|
|
modelTo.definition.properties, modelTo.settings.strict);
|
2015-01-19 14:49:20 +00:00
|
|
|
if (!~fields.indexOf(ctor.relations[p].keyTo)) {
|
|
|
|
fields.push(ctor.relations[p].keyTo);
|
|
|
|
}
|
2016-04-01 13:23:42 +00:00
|
|
|
self.__data[p] = new modelTo(propVal, {
|
|
|
|
fields: fields,
|
|
|
|
applySetters: false,
|
|
|
|
persisted: options.persisted,
|
|
|
|
});
|
2015-01-19 14:49:20 +00:00
|
|
|
}
|
2014-03-13 23:43:38 +00:00
|
|
|
}
|
2015-01-19 14:49:20 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
self.__cachedRelations[p] = propVal;
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
2014-06-13 06:35:20 +00:00
|
|
|
// Un-managed property
|
2014-08-20 12:03:38 +00:00
|
|
|
if (strict === false || self.__cachedRelations[p]) {
|
2014-08-21 20:47:09 +00:00
|
|
|
self[p] = self.__data[p] =
|
|
|
|
(propVal !== undefined) ? propVal : self.__cachedRelations[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(
|
2016-05-26 21:21:37 +00:00
|
|
|
'Property names containing dot(s) are not supported. ' +
|
2016-07-22 19:26:07 +00:00
|
|
|
'Model: %s, dynamic property: %s',
|
|
|
|
this.constructor.modelName, p
|
|
|
|
));
|
2015-04-02 07:49:04 +00:00
|
|
|
}
|
2016-09-07 18:24:48 +00:00
|
|
|
} else {
|
2017-03-20 22:10:01 +00:00
|
|
|
if (strict !== 'filter') {
|
|
|
|
this.__unknownProperties.push(p);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-04-18 09:05:11 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
keys = Object.keys(properties);
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-10-19 16:47:27 +00:00
|
|
|
if (Array.isArray(options.fields)) {
|
|
|
|
keys = keys.filter(function(k) {
|
|
|
|
return (options.fields.indexOf(k) != -1);
|
|
|
|
});
|
|
|
|
}
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
size = keys.length;
|
2013-08-30 23:51:17 +00:00
|
|
|
|
2018-12-07 15:22:36 +00:00
|
|
|
for (let k = 0; k < size; k++) {
|
2014-06-13 06:35:20 +00:00
|
|
|
p = keys[k];
|
|
|
|
propVal = self.__data[p];
|
2018-12-07 14:54:29 +00:00
|
|
|
const type = properties[p].type;
|
2012-10-13 13:59:25 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
// Set default values
|
2015-06-18 22:19:45 +00:00
|
|
|
if (applyDefaultValues && propVal === undefined) {
|
2018-12-07 14:54:29 +00:00
|
|
|
let def = properties[p]['default'];
|
2014-06-13 06:35:20 +00:00
|
|
|
if (def !== undefined) {
|
|
|
|
if (typeof def === 'function') {
|
2014-06-21 19:53:06 +00:00
|
|
|
if (def === Date) {
|
|
|
|
// FIXME: We should coerce the value in general
|
|
|
|
// This is a work around to {default: Date}
|
|
|
|
// Date() will return a string instead of Date
|
|
|
|
def = new Date();
|
|
|
|
} else {
|
|
|
|
def = def();
|
|
|
|
}
|
2015-02-13 13:00:36 +00:00
|
|
|
} else if (type.name === 'Date' && def === '$now') {
|
|
|
|
def = new Date();
|
2014-06-13 06:35:20 +00:00
|
|
|
}
|
2014-06-21 19:53:06 +00:00
|
|
|
// FIXME: We should coerce the value
|
|
|
|
// will implement it after we refactor the PropertyDefinition
|
2015-02-20 10:18:47 +00:00
|
|
|
self.__data[p] = propVal = def;
|
2014-06-13 06:35:20 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2015-02-20 10:18:47 +00:00
|
|
|
// Set default value using a named function
|
2015-06-18 22:19:45 +00:00
|
|
|
if (applyDefaultValues && propVal === undefined) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const defn = properties[p].defaultFn;
|
2015-02-20 10:18:47 +00:00
|
|
|
switch (defn) {
|
|
|
|
case undefined:
|
|
|
|
break;
|
|
|
|
case 'guid':
|
|
|
|
case 'uuid':
|
|
|
|
// Generate a v1 (time-based) id
|
|
|
|
propVal = uuid.v1();
|
|
|
|
break;
|
2015-09-02 14:39:00 +00:00
|
|
|
case 'uuidv4':
|
|
|
|
// Generate a RFC4122 v4 UUID
|
|
|
|
propVal = uuid.v4();
|
|
|
|
break;
|
2015-02-20 10:18:47 +00:00
|
|
|
case 'now':
|
|
|
|
propVal = new Date();
|
|
|
|
break;
|
2016-09-20 09:16:00 +00:00
|
|
|
case 'shortid':
|
|
|
|
propVal = shortid.generate();
|
|
|
|
break;
|
2015-02-20 10:18:47 +00:00
|
|
|
default:
|
|
|
|
// TODO Support user-provided functions via a registry of functions
|
2016-07-22 19:26:07 +00:00
|
|
|
g.warn('Unknown default value provider %s', defn);
|
2015-02-20 10:18:47 +00:00
|
|
|
}
|
|
|
|
// FIXME: We should coerce the value
|
|
|
|
// will implement it after we refactor the PropertyDefinition
|
|
|
|
if (propVal !== undefined)
|
|
|
|
self.__data[p] = propVal;
|
|
|
|
}
|
|
|
|
|
2015-03-27 09:45:14 +00:00
|
|
|
if (propVal === undefined && persistUndefinedAsNull) {
|
|
|
|
self.__data[p] = propVal = null;
|
|
|
|
}
|
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
// Handle complex types (JSON/Object)
|
2014-08-20 12:03:38 +00:00
|
|
|
if (!BASE_TYPES[type.name]) {
|
2014-06-13 06:35:20 +00:00
|
|
|
if (typeof self.__data[p] !== 'object' && self.__data[p]) {
|
2014-01-24 17:09:53 +00:00
|
|
|
try {
|
2014-06-13 06:35:20 +00:00
|
|
|
self.__data[p] = JSON.parse(self.__data[p] + '');
|
2014-01-24 17:09:53 +00:00
|
|
|
} catch (e) {
|
2014-06-13 06:35:20 +00:00
|
|
|
self.__data[p] = String(self.__data[p]);
|
2011-10-11 19:51:32 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-08-19 20:06:55 +00:00
|
|
|
if (type.prototype instanceof ModelBaseClass) {
|
2016-04-01 13:23:42 +00:00
|
|
|
if (!(self.__data[p] instanceof type) &&
|
|
|
|
typeof self.__data[p] === 'object' &&
|
|
|
|
self.__data[p] !== null) {
|
2014-08-19 20:06:55 +00:00
|
|
|
self.__data[p] = new type(self.__data[p]);
|
|
|
|
}
|
|
|
|
} else if (type.name === 'Array' || Array.isArray(type)) {
|
2016-04-01 13:23:42 +00:00
|
|
|
if (!(self.__data[p] instanceof List) &&
|
|
|
|
self.__data[p] !== undefined &&
|
|
|
|
self.__data[p] !== null) {
|
2014-06-13 06:35:20 +00:00
|
|
|
self.__data[p] = List(self.__data[p], type, self);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-11 19:51:32 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
this.trigger('initialize');
|
2014-02-11 06:38:59 +00:00
|
|
|
};
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Define a property on the model.
|
|
|
|
* @param {String} prop Property name
|
|
|
|
* @param {Object} params Various property configuration
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.defineProperty = function(prop, params) {
|
|
|
|
if (this.dataSource) {
|
2014-08-08 08:20:57 +00:00
|
|
|
this.dataSource.defineProperty(this.modelName, prop, params);
|
|
|
|
} else {
|
|
|
|
this.modelBuilder.defineProperty(this.modelName, prop, params);
|
|
|
|
}
|
2011-12-09 15:23:29 +00:00
|
|
|
};
|
|
|
|
|
2017-05-03 15:10:31 +00:00
|
|
|
/**
|
|
|
|
* Get model property type.
|
|
|
|
* @param {String} propName Property name
|
|
|
|
* @returns {String} Name of property type
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.getPropertyType = function(propName) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const prop = this.definition.properties[propName];
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!prop) {
|
|
|
|
// The property is not part of the definition
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!prop.type) {
|
2016-07-22 19:26:07 +00:00
|
|
|
throw new Error(g.f('Type not defined for property %s.%s', this.modelName, propName));
|
2014-01-24 17:09:53 +00:00
|
|
|
// return null;
|
|
|
|
}
|
|
|
|
return prop.type.name;
|
2012-05-29 11:16:24 +00:00
|
|
|
};
|
|
|
|
|
2017-05-03 15:10:31 +00:00
|
|
|
/**
|
|
|
|
* Get model property type.
|
|
|
|
* @param {String} propName Property name
|
|
|
|
* @returns {String} Name of property type
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.prototype.getPropertyType = function(propName) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return this.constructor.getPropertyType(propName);
|
2012-01-18 15:20:05 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Return string representation of class
|
2014-03-12 23:28:46 +00:00
|
|
|
* This overrides the default `toString()` method
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.toString = function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
return '[Model ' + this.modelName + ']';
|
2012-07-13 13:53:22 +00:00
|
|
|
};
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Convert model instance to a plain JSON object.
|
|
|
|
* Returns a canonical object representation (no getters and setters).
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {Boolean} onlySchema Restrict properties to dataSource only. Default is false. If true, the function returns only properties defined in the schema; Otherwise it returns all enumerable properties.
|
2017-05-03 15:10:31 +00:00
|
|
|
* @param {Boolean} removeHidden Boolean flag as part of the transformation. If true, then hidden properties should not be brought out.
|
|
|
|
* @param {Boolean} removeProtected Boolean flag as part of the transformation. If true, then protected properties should not be brought out.
|
|
|
|
* @returns {Object} returns Plain JSON object
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.prototype.toObject = function(onlySchema, removeHidden, removeProtected) {
|
2018-05-10 17:13:14 +00:00
|
|
|
if (typeof onlySchema === 'object' && onlySchema != null) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const options = onlySchema;
|
2018-05-10 17:13:14 +00:00
|
|
|
onlySchema = options.onlySchema;
|
|
|
|
removeHidden = options.removeHidden;
|
|
|
|
removeProtected = options.removeProtected;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
if (onlySchema === undefined) {
|
2014-01-30 19:51:34 +00:00
|
|
|
onlySchema = true;
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = {};
|
|
|
|
const self = this;
|
|
|
|
const Model = this.constructor;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-05-05 14:23:12 +00:00
|
|
|
// if it is already an Object
|
2014-06-13 06:35:20 +00:00
|
|
|
if (Model === Object) {
|
|
|
|
return self;
|
|
|
|
}
|
2014-05-05 14:23:12 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const strict = this.__strict;
|
|
|
|
const schemaLess = (strict === false) || !onlySchema;
|
|
|
|
const persistUndefinedAsNull = Model.definition.settings.persistUndefinedAsNull;
|
2014-02-11 06:38:59 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const props = Model.definition.properties;
|
|
|
|
let keys = Object.keys(props);
|
|
|
|
let propertyName, val;
|
2015-01-20 20:32:31 +00:00
|
|
|
|
2018-12-07 15:22:36 +00:00
|
|
|
for (let i = 0; i < keys.length; i++) {
|
2014-06-13 06:35:20 +00:00
|
|
|
propertyName = keys[i];
|
|
|
|
val = self[propertyName];
|
|
|
|
|
|
|
|
// Exclude functions
|
|
|
|
if (typeof val === 'function') {
|
|
|
|
continue;
|
2014-05-09 22:27:45 +00:00
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
// Exclude hidden properties
|
|
|
|
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
|
|
|
continue;
|
2014-05-09 22:27:45 +00:00
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
|
2015-01-20 19:58:52 +00:00
|
|
|
if (removeProtected && Model.isProtectedProperty(propertyName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
if (val instanceof List) {
|
2015-01-20 19:58:52 +00:00
|
|
|
data[propertyName] = val.toObject(!schemaLess, removeHidden, true);
|
2014-06-13 06:35:20 +00:00
|
|
|
} else {
|
|
|
|
if (val !== undefined && val !== null && val.toObject) {
|
2015-01-20 19:58:52 +00:00
|
|
|
data[propertyName] = val.toObject(!schemaLess, removeHidden, true);
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
2015-03-27 09:45:14 +00:00
|
|
|
if (val === undefined && persistUndefinedAsNull) {
|
|
|
|
val = null;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
data[propertyName] = val;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (schemaLess) {
|
2014-01-30 19:51:34 +00:00
|
|
|
// Find its own properties which can be set via myModel.myProperty = 'myValue'.
|
|
|
|
// If the property is not declared in the model definition, no setter will be
|
|
|
|
// triggered to add it to __data
|
2014-06-13 06:35:20 +00:00
|
|
|
keys = Object.keys(self);
|
2018-12-07 14:54:29 +00:00
|
|
|
let size = keys.length;
|
2018-12-07 15:22:36 +00:00
|
|
|
for (let i = 0; i < size; i++) {
|
2014-06-13 06:35:20 +00:00
|
|
|
propertyName = keys[i];
|
|
|
|
if (props[propertyName]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (propertyName.indexOf('__') === 0) {
|
2014-05-09 22:27:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-20 19:58:52 +00:00
|
|
|
if (removeProtected && Model.isProtectedProperty(propertyName)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-27 09:45:14 +00:00
|
|
|
if (data[propertyName] !== undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
val = self[propertyName];
|
2015-03-27 09:45:14 +00:00
|
|
|
if (val !== undefined) {
|
2014-05-09 22:27:45 +00:00
|
|
|
if (typeof val === 'function') {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
if (val !== null && val.toObject) {
|
2015-01-20 19:58:52 +00:00
|
|
|
data[propertyName] = val.toObject(!schemaLess, removeHidden, true);
|
2014-01-30 19:51:34 +00:00
|
|
|
} else {
|
|
|
|
data[propertyName] = val;
|
|
|
|
}
|
2015-03-27 09:45:14 +00:00
|
|
|
} else if (persistUndefinedAsNull) {
|
|
|
|
data[propertyName] = null;
|
2014-01-30 19:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now continue to check __data
|
2014-06-13 06:35:20 +00:00
|
|
|
keys = Object.keys(self.__data);
|
|
|
|
size = keys.length;
|
2018-12-07 15:22:36 +00:00
|
|
|
for (let i = 0; i < size; i++) {
|
2014-06-13 06:35:20 +00:00
|
|
|
propertyName = keys[i];
|
|
|
|
if (propertyName.indexOf('__') === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (data[propertyName] === undefined) {
|
|
|
|
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
2014-05-09 22:27:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-20 20:32:31 +00:00
|
|
|
if (removeProtected && Model.isProtectedProperty(propertyName)) {
|
2015-01-20 19:58:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const ownVal = self[propertyName];
|
2014-06-13 06:35:20 +00:00
|
|
|
// The ownVal can be a relation function
|
2015-01-20 19:58:52 +00:00
|
|
|
val = (ownVal !== undefined && (typeof ownVal !== 'function')) ? ownVal : self.__data[propertyName];
|
2014-05-09 22:27:45 +00:00
|
|
|
if (typeof val === 'function') {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (val !== undefined && val !== null && val.toObject) {
|
2015-01-20 19:58:52 +00:00
|
|
|
data[propertyName] = val.toObject(!schemaLess, removeHidden, true);
|
2015-03-27 09:45:14 +00:00
|
|
|
} else if (val === undefined && persistUndefinedAsNull) {
|
|
|
|
data[propertyName] = null;
|
2012-09-10 15:57:21 +00:00
|
|
|
} else {
|
2014-01-24 17:09:53 +00:00
|
|
|
data[propertyName] = val;
|
2013-10-07 04:27:02 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2012-10-13 13:59:25 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-04-11 18:39:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
return data;
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2018-11-12 21:54:22 +00:00
|
|
|
/**
|
|
|
|
* Convert an array of strings into an object as the map
|
|
|
|
* @param {string[]} arr An array of strings
|
|
|
|
*/
|
|
|
|
function asObjectMap(arr) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const obj = {};
|
2018-11-12 21:54:22 +00:00
|
|
|
if (Array.isArray(arr)) {
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0; i < arr.length; i++) {
|
2018-11-12 21:54:22 +00:00
|
|
|
obj[arr[i]] = true;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
return arr || obj;
|
|
|
|
}
|
2017-05-03 15:10:31 +00:00
|
|
|
/**
|
|
|
|
* Checks if property is protected.
|
|
|
|
* @param {String} propertyName Property name
|
2018-10-27 14:47:46 +00:00
|
|
|
* @returns {Boolean} true or false if protected or not.
|
2017-05-03 15:10:31 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.isProtectedProperty = function(propertyName) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const settings = (this.definition && this.definition.settings) || {};
|
|
|
|
const protectedProperties = settings.protectedProperties || settings.protected;
|
2018-11-12 21:54:22 +00:00
|
|
|
settings.protectedProperties = asObjectMap(protectedProperties);
|
|
|
|
return settings.protectedProperties[propertyName];
|
2015-01-20 19:58:52 +00:00
|
|
|
};
|
|
|
|
|
2017-05-03 15:10:31 +00:00
|
|
|
/**
|
|
|
|
* Checks if property is hidden.
|
|
|
|
* @param {String} propertyName Property name
|
|
|
|
* @returns {Boolean} true or false if hidden or not.
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.isHiddenProperty = function(propertyName) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const settings = (this.definition && this.definition.settings) || {};
|
|
|
|
const hiddenProperties = settings.hiddenProperties || settings.hidden;
|
2018-11-12 21:54:22 +00:00
|
|
|
settings.hiddenProperties = asObjectMap(hiddenProperties);
|
|
|
|
return settings.hiddenProperties[propertyName];
|
2015-01-20 19:58:52 +00:00
|
|
|
};
|
2014-04-11 18:39:57 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.prototype.toJSON = function() {
|
2015-01-20 19:58:52 +00:00
|
|
|
return this.toObject(false, true, false);
|
2012-09-10 15:57:21 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.prototype.fromObject = function(obj) {
|
2018-12-07 14:54:29 +00:00
|
|
|
for (const key in obj) {
|
2014-01-24 17:09:53 +00:00
|
|
|
this[key] = obj[key];
|
|
|
|
}
|
2012-04-09 16:24:35 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Reset dirty attributes.
|
|
|
|
* This method does not perform any database operations; it just resets the object to its
|
|
|
|
* initial state.
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.prototype.reset = function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const obj = this;
|
|
|
|
for (const k in obj) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (k !== 'id' && !obj.constructor.dataSource.definitions[obj.constructor.modelName].properties[k]) {
|
|
|
|
delete obj[k];
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 16:31:01 +00:00
|
|
|
};
|
|
|
|
|
2015-01-12 15:50:51 +00:00
|
|
|
// Node v0.11+ allows custom inspect functions to return an object
|
|
|
|
// instead of string. That way options like `showHidden` and `colors`
|
|
|
|
// can be preserved.
|
2018-12-07 14:54:29 +00:00
|
|
|
const versionParts = process.versions && process.versions.node ?
|
2015-01-15 07:45:52 +00:00
|
|
|
process.versions.node.split(/\./g).map(function(v) { return +v; }) :
|
|
|
|
[1, 0, 0]; // browserify ships 1.0-compatible version of util.inspect
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const INSPECT_SUPPORTS_OBJECT_RETVAL =
|
2015-01-12 15:50:51 +00:00
|
|
|
versionParts[0] > 0 ||
|
|
|
|
versionParts[1] > 11 ||
|
|
|
|
(versionParts[0] === 11 && versionParts[1] >= 14);
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.prototype.inspect = function(depth) {
|
2015-01-12 15:50:51 +00:00
|
|
|
if (INSPECT_SUPPORTS_OBJECT_RETVAL)
|
2016-04-01 11:48:17 +00:00
|
|
|
return this.__data;
|
2015-01-12 15:50:51 +00:00
|
|
|
|
|
|
|
// Workaround for older versions
|
|
|
|
// See also https://github.com/joyent/node/commit/66280de133
|
|
|
|
return util.inspect(this.__data, {
|
|
|
|
showHidden: false,
|
|
|
|
depth: depth,
|
2016-04-01 11:48:17 +00:00
|
|
|
colors: false,
|
2015-01-12 15:50:51 +00:00
|
|
|
});
|
2012-10-15 23:15:29 +00:00
|
|
|
};
|
|
|
|
|
2019-04-29 07:39:20 +00:00
|
|
|
if (util.inspect.custom) {
|
|
|
|
// Node.js 12+ no longer recognizes "inspect" method,
|
|
|
|
// it uses "inspect.custom" symbol as the key instead
|
|
|
|
// TODO(semver-major) always use the symbol key only (requires Node.js 8+).
|
|
|
|
ModelBaseClass.prototype[util.inspect.custom] = ModelBaseClass.prototype.inspect;
|
|
|
|
}
|
|
|
|
|
2017-05-03 15:10:31 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String} anotherClass could be string or class. Name of the class or the class itself
|
|
|
|
* @param {Object} options An object to control the instantiation
|
|
|
|
* @returns {ModelClass}
|
|
|
|
*/
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.mixin = function(anotherClass, options) {
|
2014-08-06 11:26:47 +00:00
|
|
|
if (typeof anotherClass === 'string') {
|
2014-08-08 08:20:57 +00:00
|
|
|
this.modelBuilder.mixins.applyMixin(this, anotherClass, options);
|
2014-08-06 11:26:47 +00:00
|
|
|
} else {
|
2014-08-08 08:20:57 +00:00
|
|
|
if (anotherClass.prototype instanceof ModelBaseClass) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const props = anotherClass.definition.properties;
|
|
|
|
for (const i in props) {
|
2014-08-08 08:20:57 +00:00
|
|
|
if (this.definition.properties[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this.defineProperty(i, props[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-08-06 11:26:47 +00:00
|
|
|
return jutil.mixin(this, anotherClass, options);
|
|
|
|
}
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
2013-05-28 20:50:59 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.prototype.getDataSource = function() {
|
2013-10-31 17:51:33 +00:00
|
|
|
return this.__dataSource || this.constructor.dataSource;
|
2014-02-11 06:38:59 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.getDataSource = function() {
|
2013-10-31 17:51:33 +00:00
|
|
|
return this.dataSource;
|
2014-02-11 06:38:59 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
ModelBaseClass.prototype.setStrict = function(strict) {
|
2014-02-11 06:38:59 +00:00
|
|
|
this.__strict = strict;
|
|
|
|
};
|
2013-10-31 17:51:33 +00:00
|
|
|
|
2017-03-27 22:30:29 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* `getMergePolicy()` provides model merge policies to apply when extending
|
|
|
|
* a child model from a base model. Such a policy drives the way parent/child model
|
|
|
|
* properties/settings are merged/mixed-in together.
|
|
|
|
*
|
|
|
|
* Below is presented the expected merge behaviour for each option.
|
|
|
|
* NOTE: This applies to top-level settings properties
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* - Any
|
|
|
|
* - `{replace: true}` (default): child replaces the value from parent
|
|
|
|
* - assignin `null` on child setting deletes the inherited setting
|
|
|
|
*
|
|
|
|
* - 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
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* The recommended built-in merge policy is as follows. It is returned by getMergePolicy()
|
|
|
|
* when calling the method with option `{configureModelMerge: true}`.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* {
|
|
|
|
* description: {replace: true}, // string or array
|
|
|
|
* options: {patch: true}, // object
|
|
|
|
* hidden: {replace: false}, // array
|
|
|
|
* protected: {replace: false}, // array
|
|
|
|
* indexes: {patch: true}, // object
|
|
|
|
* methods: {patch: true}, // object
|
|
|
|
* mixins: {patch: true}, // object
|
|
|
|
* relations: {patch: true}, // object
|
|
|
|
* scope: {replace: true}, // object
|
|
|
|
* scopes: {patch: true}, // object
|
|
|
|
* acls: {rank: true}, // array
|
|
|
|
* // this setting controls which child model property's value allows deleting
|
|
|
|
* // a base model's property
|
|
|
|
* __delete: null,
|
|
|
|
* // this setting controls the default merge behaviour for settings not defined
|
|
|
|
* // in the mergePolicy specification
|
|
|
|
* __default: {replace: true},
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The legacy built-in merge policy is as follows, it is retuned by `getMergePolicy()`
|
|
|
|
* when avoiding option `configureModelMerge`.
|
|
|
|
* NOTE: it also provides the ACLs ranking in addition to the legacy behaviour, as well
|
|
|
|
* as fixes for settings 'description' and 'relations': matching relations from child
|
|
|
|
* replace relations from parents.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* {
|
|
|
|
* 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
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* `getMergePolicy()` can be customized using model's setting `configureModelMerge` as follows:
|
|
|
|
*
|
|
|
|
* ``` json
|
|
|
|
* {
|
|
|
|
* // ..
|
|
|
|
* options: {
|
|
|
|
* configureModelMerge: {
|
|
|
|
* // merge options
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* // ..
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* NOTE: mergePolicy parameter can also defined at JSON model definition root
|
|
|
|
*
|
|
|
|
* `getMergePolicy()` method can also be extended programmatically as follows:
|
|
|
|
*
|
|
|
|
* ``` js
|
|
|
|
* myModel.getMergePolicy = function(options) {
|
|
|
|
* const origin = myModel.base.getMergePolicy(options);
|
|
|
|
* return Object.assign({}, origin, {
|
|
|
|
* // new/overriding options
|
|
|
|
* });
|
|
|
|
* };
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {Object} options option `configureModelMerge` can be used to alter the
|
|
|
|
* returned merge policy:
|
|
|
|
* - `configureModelMerge: true` will have the method return the recommended merge policy.
|
|
|
|
* - `configureModelMerge: {..}` will actually have the method return the provided object.
|
|
|
|
* - not providing this options will have the method return a merge policy emulating the
|
|
|
|
* the model merge behaviour up to datasource-juggler v3.6.1, as well as the ACLs ranking.
|
|
|
|
* @returns {Object} mergePolicy The model merge policy to apply when using the
|
|
|
|
* current model as base class for a child model
|
|
|
|
*/
|
|
|
|
ModelBaseClass.getMergePolicy = function(options) {
|
|
|
|
// NOTE: merge policy equivalent to datasource-juggler behaviour up to v3.6.1
|
|
|
|
// + fix for description arrays that should not be merged
|
|
|
|
// + fix for relations that should patch matching relations
|
|
|
|
// + ranking of ACLs
|
2018-12-07 14:54:29 +00:00
|
|
|
let mergePolicy = {
|
2017-03-27 22:30:29 +00:00
|
|
|
description: {replace: true}, // string or array
|
|
|
|
properties: {patch: true}, // object
|
|
|
|
hidden: {replace: false}, // array
|
|
|
|
protected: {replace: false}, // array
|
|
|
|
relations: {patch: true}, // object
|
|
|
|
acls: {rank: true}, // array
|
|
|
|
};
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const config = (options || {}).configureModelMerge;
|
2017-03-27 22:30:29 +00:00
|
|
|
|
|
|
|
if (config === true) {
|
|
|
|
// NOTE: recommended merge policy from datasource-juggler v3.6.2
|
|
|
|
mergePolicy = {
|
|
|
|
description: {replace: true}, // string or array
|
|
|
|
options: {patch: true}, // object
|
|
|
|
// properties: {patch: true}, // object // NOTE: not part of configurable merge
|
|
|
|
hidden: {replace: false}, // array
|
|
|
|
protected: {replace: false}, // array
|
|
|
|
indexes: {patch: true}, // object
|
|
|
|
methods: {patch: true}, // object
|
|
|
|
mixins: {patch: true}, // object
|
|
|
|
// validations: {patch: true}, // object // NOTE: not implemented
|
|
|
|
relations: {patch: true}, // object
|
|
|
|
scope: {replace: true}, // object
|
|
|
|
scopes: {patch: true}, // object
|
|
|
|
acls: {rank: true}, // array
|
|
|
|
// this option controls which value assigned on child model allows deleting
|
|
|
|
// a base model's setting
|
|
|
|
__delete: null,
|
|
|
|
// this option controls the default merge behaviour for settings not defined
|
|
|
|
// in the mergePolicy specification
|
|
|
|
__default: {replace: true},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// override mergePolicy with provided model setting if required
|
|
|
|
if (config && typeof config === 'object' && !Array.isArray(config)) {
|
|
|
|
// config is an object
|
|
|
|
mergePolicy = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mergePolicy;
|
|
|
|
};
|
|
|
|
|
2017-08-22 20:09:45 +00:00
|
|
|
/**
|
|
|
|
* Gets properties defined with 'updateOnly' flag set to true from the model. This flag is also set to true
|
|
|
|
* internally for the id property, if this property is generated and IdInjection is true.
|
|
|
|
* @returns {updateOnlyProps} List of properties with updateOnly set to true.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ModelBaseClass.getUpdateOnlyProperties = function() {
|
|
|
|
const props = this.definition.properties;
|
|
|
|
return Object.keys(props).filter(key => props[key].updateOnly);
|
|
|
|
};
|
|
|
|
|
2018-11-12 21:54:22 +00:00
|
|
|
// Mix in utils
|
|
|
|
jutil.mixin(ModelBaseClass, DataAccessUtils);
|
|
|
|
|
2015-05-14 18:51:52 +00:00
|
|
|
// Mixin observer
|
2018-11-12 21:54:22 +00:00
|
|
|
jutil.mixin(ModelBaseClass, Observer);
|
2015-01-19 12:39:31 +00:00
|
|
|
|
2013-05-28 05:20:30 +00:00
|
|
|
jutil.mixin(ModelBaseClass, Hookable);
|
2013-10-04 21:32:51 +00:00
|
|
|
jutil.mixin(ModelBaseClass, validations.Validatable);
|