2019-05-08 15:45:37 +00:00
|
|
|
// Copyright IBM Corp. 2012,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
|
2019-05-08 15:45:37 +00:00
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2016-04-01 22:25:16 +00:00
|
|
|
|
2012-01-30 19:37:14 +00:00
|
|
|
exports.safeRequire = safeRequire;
|
2013-07-17 00:53:52 +00:00
|
|
|
exports.fieldsToArray = fieldsToArray;
|
|
|
|
exports.selectFields = selectFields;
|
2018-10-19 18:56:51 +00:00
|
|
|
exports.sanitizeQuery = sanitizeQuery;
|
2013-10-25 23:18:02 +00:00
|
|
|
exports.parseSettings = parseSettings;
|
2017-03-27 22:30:29 +00:00
|
|
|
exports.mergeSettings = exports.deepMerge = deepMerge;
|
|
|
|
exports.deepMergeProperty = deepMergeProperty;
|
2014-01-28 17:57:23 +00:00
|
|
|
exports.isPlainObject = isPlainObject;
|
|
|
|
exports.defineCachedRelations = defineCachedRelations;
|
2014-08-15 17:39:18 +00:00
|
|
|
exports.sortObjectsByIds = sortObjectsByIds;
|
2014-09-06 09:13:47 +00:00
|
|
|
exports.setScopeValuesFromWhere = setScopeValuesFromWhere;
|
|
|
|
exports.mergeQuery = mergeQuery;
|
2015-04-24 20:23:13 +00:00
|
|
|
exports.mergeIncludes = mergeIncludes;
|
2015-05-29 17:50:37 +00:00
|
|
|
exports.createPromiseCallback = createPromiseCallback;
|
|
|
|
exports.uniq = uniq;
|
2015-07-24 19:56:31 +00:00
|
|
|
exports.toRegExp = toRegExp;
|
|
|
|
exports.hasRegExpFlags = hasRegExpFlags;
|
2015-08-17 19:49:38 +00:00
|
|
|
exports.idEquals = idEquals;
|
2015-08-18 14:22:36 +00:00
|
|
|
exports.findIndexOf = findIndexOf;
|
2016-10-27 14:18:54 +00:00
|
|
|
exports.collectTargetIds = collectTargetIds;
|
|
|
|
exports.idName = idName;
|
2017-03-27 22:30:29 +00:00
|
|
|
exports.rankArrayElements = rankArrayElements;
|
2018-10-03 22:17:08 +00:00
|
|
|
exports.idsHaveDuplicates = idsHaveDuplicates;
|
2019-01-25 08:46:31 +00:00
|
|
|
exports.isClass = isClass;
|
2019-10-21 16:55:14 +00:00
|
|
|
exports.escapeRegExp = escapeRegExp;
|
2019-10-23 16:31:39 +00:00
|
|
|
exports.applyParentProperty = applyParentProperty;
|
2013-10-11 18:50:00 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const g = require('strong-globalize')();
|
|
|
|
const traverse = require('traverse');
|
|
|
|
const assert = require('assert');
|
|
|
|
const debug = require('debug')('loopback:juggler:utils');
|
2012-01-30 19:37:14 +00:00
|
|
|
|
2019-10-23 16:31:39 +00:00
|
|
|
/**
|
|
|
|
* The name of the property in modelBuilder settings that will enable the child parent reference functionality
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const BUILDER_PARENT_SETTING = 'parentRef';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The property name that should be defined on each child instance if parent feature flag enabled
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const PARENT_PROPERTY_NAME = '__parent';
|
|
|
|
|
2012-01-30 19:37:14 +00:00
|
|
|
function safeRequire(module) {
|
2014-01-24 17:09:53 +00:00
|
|
|
try {
|
|
|
|
return require(module);
|
|
|
|
} catch (e) {
|
2016-07-22 19:26:07 +00:00
|
|
|
g.log('Run "{{npm install loopback-datasource-juggler}} %s" command ',
|
|
|
|
'to use {{loopback-datasource-juggler}} using %s database engine',
|
|
|
|
module, module);
|
2014-01-24 17:09:53 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2012-01-30 19:37:14 +00:00
|
|
|
}
|
|
|
|
|
2014-09-06 09:13:47 +00:00
|
|
|
/*
|
|
|
|
* Extracting fixed property values for the scope from the where clause into
|
|
|
|
* the data object
|
|
|
|
*
|
|
|
|
* @param {Object} The data object
|
|
|
|
* @param {Object} The where clause
|
|
|
|
*/
|
|
|
|
function setScopeValuesFromWhere(data, where, targetModel) {
|
2018-12-07 14:54:29 +00:00
|
|
|
for (const i in where) {
|
2014-09-06 09:13:47 +00:00
|
|
|
if (i === 'and') {
|
|
|
|
// Find fixed property values from each subclauses
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let w = 0, n = where[i].length; w < n; w++) {
|
2014-09-06 09:13:47 +00:00
|
|
|
setScopeValuesFromWhere(data, where[i][w], targetModel);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const prop = targetModel.definition.properties[i];
|
2014-09-06 09:13:47 +00:00
|
|
|
if (prop) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const val = where[i];
|
2016-04-01 13:23:42 +00:00
|
|
|
if (typeof val !== 'object' || val instanceof prop.type ||
|
2018-06-12 07:13:32 +00:00
|
|
|
prop.type.name === 'ObjectID' || // MongoDB key
|
2017-05-01 01:59:44 +00:00
|
|
|
prop.type.name === 'uuidFromString') { // C*
|
2014-09-06 09:13:47 +00:00
|
|
|
// Only pick the {propertyName: propertyValue}
|
|
|
|
data[i] = where[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 20:23:13 +00:00
|
|
|
/**
|
|
|
|
* Merge include options of default scope with runtime include option.
|
|
|
|
* exhibits the _.extend behaviour. Property value of source overrides
|
|
|
|
* property value of destination if property name collision occurs
|
|
|
|
* @param {String|Array|Object} destination The default value of `include` option
|
|
|
|
* @param {String|Array|Object} source The runtime value of `include` option
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
function mergeIncludes(destination, source) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const destArray = convertToArray(destination);
|
|
|
|
const sourceArray = convertToArray(source);
|
2015-04-24 20:23:13 +00:00
|
|
|
if (destArray.length === 0) {
|
|
|
|
return sourceArray;
|
|
|
|
}
|
|
|
|
if (sourceArray.length === 0) {
|
|
|
|
return destArray;
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const relationNames = [];
|
|
|
|
const resultArray = [];
|
|
|
|
for (const j in sourceArray) {
|
|
|
|
const sourceEntry = sourceArray[j];
|
|
|
|
const sourceEntryRelationName = (typeof (sourceEntry.rel || sourceEntry.relation) === 'string') ?
|
2015-04-24 20:23:13 +00:00
|
|
|
sourceEntry.relation : Object.keys(sourceEntry)[0];
|
|
|
|
relationNames.push(sourceEntryRelationName);
|
|
|
|
resultArray.push(sourceEntry);
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
for (const i in destArray) {
|
|
|
|
const destEntry = destArray[i];
|
|
|
|
const destEntryRelationName = (typeof (destEntry.rel || destEntry.relation) === 'string') ?
|
2015-04-24 20:23:13 +00:00
|
|
|
destEntry.relation : Object.keys(destEntry)[0];
|
|
|
|
if (relationNames.indexOf(destEntryRelationName) === -1) {
|
|
|
|
resultArray.push(destEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resultArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts input parameter into array of objects which wraps the value.
|
|
|
|
* "someValue" is converted to [{"someValue":true}]
|
|
|
|
* ["someValue"] is converted to [{"someValue":true}]
|
|
|
|
* {"someValue":true} is converted to [{"someValue":true}]
|
|
|
|
* @param {String|Array|Object} param - Input parameter to be converted
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
function convertToArray(include) {
|
|
|
|
if (typeof include === 'string') {
|
2017-01-31 15:14:53 +00:00
|
|
|
const obj = {};
|
2015-04-24 20:23:13 +00:00
|
|
|
obj[include] = true;
|
|
|
|
return [obj];
|
|
|
|
} else if (isPlainObject(include)) {
|
2017-01-06 12:33:54 +00:00
|
|
|
// if include is of the form - {relation:'',scope:''}
|
2015-04-24 20:23:13 +00:00
|
|
|
if (include.rel || include.relation) {
|
|
|
|
return [include];
|
|
|
|
}
|
|
|
|
// Build an array of key/value pairs
|
2018-12-07 14:54:29 +00:00
|
|
|
const newInclude = [];
|
|
|
|
for (const key in include) {
|
2017-01-31 15:14:53 +00:00
|
|
|
const obj = {};
|
2015-04-24 20:23:13 +00:00
|
|
|
obj[key] = include[key];
|
|
|
|
newInclude.push(obj);
|
|
|
|
}
|
|
|
|
return newInclude;
|
|
|
|
} else if (Array.isArray(include)) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const normalized = [];
|
|
|
|
for (const i in include) {
|
|
|
|
const includeEntry = include[i];
|
2015-04-24 20:23:13 +00:00
|
|
|
if (typeof includeEntry === 'string') {
|
2017-01-31 15:14:53 +00:00
|
|
|
const obj = {};
|
2015-04-24 20:23:13 +00:00
|
|
|
obj[includeEntry] = true;
|
2016-04-01 11:48:17 +00:00
|
|
|
normalized.push(obj);
|
2016-04-01 13:23:42 +00:00
|
|
|
} else {
|
2015-04-24 20:23:13 +00:00
|
|
|
normalized.push(includeEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return normalized;
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2014-09-06 09:13:47 +00:00
|
|
|
/*!
|
|
|
|
* Merge query parameters
|
|
|
|
* @param {Object} base The base object to contain the merged results
|
|
|
|
* @param {Object} update The object containing updates to be merged
|
2014-09-06 12:38:57 +00:00
|
|
|
* @param {Object} spec Optionally specifies parameters to exclude (set to false)
|
2014-09-06 09:13:47 +00:00
|
|
|
* @returns {*|Object} The base object
|
|
|
|
* @private
|
|
|
|
*/
|
2014-09-06 12:38:57 +00:00
|
|
|
function mergeQuery(base, update, spec) {
|
2014-09-06 09:13:47 +00:00
|
|
|
if (!update) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-06 12:38:57 +00:00
|
|
|
spec = spec || {};
|
2014-09-06 09:13:47 +00:00
|
|
|
base = base || {};
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-09-06 09:13:47 +00:00
|
|
|
if (update.where && Object.keys(update.where).length > 0) {
|
|
|
|
if (base.where && Object.keys(base.where).length > 0) {
|
2016-08-19 17:46:59 +00:00
|
|
|
base.where = {and: [base.where, update.where]};
|
2014-09-06 09:13:47 +00:00
|
|
|
} else {
|
|
|
|
base.where = update.where;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge inclusion
|
2014-09-06 12:38:57 +00:00
|
|
|
if (spec.include !== false && update.include) {
|
2014-09-06 09:13:47 +00:00
|
|
|
if (!base.include) {
|
|
|
|
base.include = update.include;
|
|
|
|
} else {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (spec.nestedInclude === true) {
|
2017-01-06 12:33:54 +00:00
|
|
|
// specify nestedInclude=true to force nesting of inclusions on scoped
|
2017-07-19 13:31:21 +00:00
|
|
|
// queries. e.g. In physician.patients.find({include: 'address'}),
|
2017-01-06 12:33:54 +00:00
|
|
|
// inclusion should be on patient model, not on physician model.
|
2018-12-07 14:54:29 +00:00
|
|
|
const saved = base.include;
|
2015-04-24 20:23:13 +00:00
|
|
|
base.include = {};
|
|
|
|
base.include[update.include] = saved;
|
2016-04-01 13:23:42 +00:00
|
|
|
} else {
|
2017-01-06 12:33:54 +00:00
|
|
|
// default behaviour of inclusion merge - merge inclusions at the same
|
|
|
|
// level. - https://github.com/strongloop/loopback-datasource-juggler/pull/569#issuecomment-95310874
|
2015-04-24 20:23:13 +00:00
|
|
|
base.include = mergeIncludes(base.include, update.include);
|
|
|
|
}
|
2014-09-06 09:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-09-06 12:38:57 +00:00
|
|
|
if (spec.collect !== false && update.collect) {
|
2014-09-06 09:13:47 +00:00
|
|
|
base.collect = update.collect;
|
|
|
|
}
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-09-06 12:38:57 +00:00
|
|
|
// Overwrite fields
|
|
|
|
if (spec.fields !== false && update.fields !== undefined) {
|
|
|
|
base.fields = update.fields;
|
2014-10-10 10:28:39 +00:00
|
|
|
} else if (update.fields !== undefined) {
|
|
|
|
base.fields = [].concat(base.fields).concat(update.fields);
|
2014-09-06 12:38:57 +00:00
|
|
|
}
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-09-06 09:13:47 +00:00
|
|
|
// set order
|
2014-09-06 12:38:57 +00:00
|
|
|
if ((!base.order || spec.order === false) && update.order) {
|
2014-09-06 09:13:47 +00:00
|
|
|
base.order = update.order;
|
|
|
|
}
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-09-06 09:13:47 +00:00
|
|
|
// overwrite pagination
|
2014-09-06 12:38:57 +00:00
|
|
|
if (spec.limit !== false && update.limit !== undefined) {
|
2014-09-06 09:13:47 +00:00
|
|
|
base.limit = update.limit;
|
|
|
|
}
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const skip = spec.skip !== false && spec.offset !== false;
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-09-06 12:38:57 +00:00
|
|
|
if (skip && update.skip !== undefined) {
|
2014-09-06 09:13:47 +00:00
|
|
|
base.skip = update.skip;
|
|
|
|
}
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-09-06 12:38:57 +00:00
|
|
|
if (skip && update.offset !== undefined) {
|
2014-09-06 09:13:47 +00:00
|
|
|
base.offset = update.offset;
|
|
|
|
}
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-09-06 09:13:47 +00:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2015-05-20 05:32:03 +00:00
|
|
|
/**
|
|
|
|
* Normalize fields to an array of included properties
|
|
|
|
* @param {String|String[]|Object} fields Fields filter
|
|
|
|
* @param {String[]} properties Property names
|
|
|
|
* @param {Boolean} excludeUnknown To exclude fields that are unknown properties
|
|
|
|
* @returns {String[]} An array of included property names
|
|
|
|
*/
|
|
|
|
function fieldsToArray(fields, properties, excludeUnknown) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!fields) return;
|
|
|
|
|
|
|
|
// include all properties by default
|
2018-12-07 14:54:29 +00:00
|
|
|
let result = properties;
|
|
|
|
let i, n;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (typeof fields === 'string') {
|
2015-05-20 05:32:03 +00:00
|
|
|
result = [fields];
|
|
|
|
} else if (Array.isArray(fields) && fields.length > 0) {
|
2014-01-24 17:09:53 +00:00
|
|
|
// No empty array, including all the fields
|
2015-05-20 05:32:03 +00:00
|
|
|
result = fields;
|
|
|
|
} else if ('object' === typeof fields) {
|
2014-01-24 17:09:53 +00:00
|
|
|
// { field1: boolean, field2: boolean ... }
|
2018-12-07 14:54:29 +00:00
|
|
|
const included = [];
|
|
|
|
const excluded = [];
|
|
|
|
const keys = Object.keys(fields);
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!keys.length) return;
|
|
|
|
|
2015-05-20 05:32:03 +00:00
|
|
|
for (i = 0, n = keys.length; i < n; i++) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const k = keys[i];
|
2014-01-24 17:09:53 +00:00
|
|
|
if (fields[k]) {
|
|
|
|
included.push(k);
|
|
|
|
} else if ((k in fields) && !fields[k]) {
|
|
|
|
excluded.push(k);
|
|
|
|
}
|
2015-05-20 05:32:03 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
if (included.length > 0) {
|
|
|
|
result = included;
|
|
|
|
} else if (excluded.length > 0) {
|
2015-05-20 05:32:03 +00:00
|
|
|
for (i = 0, n = excluded.length; i < n; i++) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const index = result.indexOf(excluded[i]);
|
2017-03-20 23:18:48 +00:00
|
|
|
if (index !== -1) result.splice(index, 1); // only when existing field excluded
|
2015-05-20 05:32:03 +00:00
|
|
|
}
|
2013-07-17 00:53:52 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let fieldArray = [];
|
2015-05-20 05:32:03 +00:00
|
|
|
if (excludeUnknown) {
|
|
|
|
for (i = 0, n = result.length; i < n; i++) {
|
|
|
|
if (properties.indexOf(result[i]) !== -1) {
|
|
|
|
fieldArray.push(result[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fieldArray = result;
|
|
|
|
}
|
|
|
|
return fieldArray;
|
2013-07-17 00:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function selectFields(fields) {
|
|
|
|
// map function
|
2016-04-01 11:48:17 +00:00
|
|
|
return function(obj) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const result = {};
|
|
|
|
let key;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0; i < fields.length; i++) {
|
2013-07-17 00:53:52 +00:00
|
|
|
key = fields[i];
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-07-17 00:53:52 +00:00
|
|
|
result[key] = obj[key];
|
|
|
|
}
|
|
|
|
return result;
|
2013-10-30 05:16:43 +00:00
|
|
|
};
|
2013-10-11 18:50:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:57:55 +00:00
|
|
|
function isProhibited(key, prohibitedKeys) {
|
|
|
|
if (!prohibitedKeys || !prohibitedKeys.length) return false;
|
|
|
|
if (typeof key !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
for (const k of prohibitedKeys) {
|
2018-10-29 14:57:55 +00:00
|
|
|
if (k === key) return true;
|
|
|
|
// x.secret, secret.y, or x.secret.y
|
|
|
|
if (key.split('.').indexOf(k) !== -1) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-21 16:55:14 +00:00
|
|
|
/**
|
|
|
|
* Accept an operator key and return whether it is used for a regular expression query or not
|
|
|
|
* @param {string} operator
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isRegExpOperator(operator) {
|
|
|
|
return ['like', 'nlike', 'ilike', 'nilike', 'regexp'].includes(operator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accept a RegExp string and make sure that any special characters for RegExp are escaped in case they
|
|
|
|
* create an invalid Regexp
|
|
|
|
* @param {string} str
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function escapeRegExp(str) {
|
|
|
|
assert.strictEqual(typeof str, 'string', 'String required for regexp escaping');
|
|
|
|
try {
|
|
|
|
new RegExp(str); // try to parse string as regexp
|
|
|
|
return str;
|
|
|
|
} catch (unused) {
|
|
|
|
console.warn(
|
|
|
|
'Auto-escaping invalid RegExp value %j supplied by the caller. ' +
|
|
|
|
'Please note this behavior may change in the future.',
|
2019-12-03 09:09:16 +00:00
|
|
|
str,
|
2019-10-21 16:55:14 +00:00
|
|
|
);
|
|
|
|
return str.replace(/[\-\[\]\/\{\}\(\)\+\?\.\\\^\$\|]/g, '\\$&');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 18:50:00 +00:00
|
|
|
/**
|
2018-10-19 18:56:51 +00:00
|
|
|
* Sanitize the query object
|
|
|
|
* @param query {object} The query object
|
|
|
|
* @param options
|
2018-10-26 16:05:47 +00:00
|
|
|
* @property normalizeUndefinedInQuery {String} either "nullify", "throw" or "ignore" (default: "ignore")
|
2018-10-19 18:56:51 +00:00
|
|
|
* @property prohibitedKeys {String[]} An array of prohibited keys to be removed
|
|
|
|
* @returns {*}
|
2013-10-11 18:50:00 +00:00
|
|
|
*/
|
2018-10-19 18:56:51 +00:00
|
|
|
function sanitizeQuery(query, options) {
|
|
|
|
debug('Sanitizing query object: %j', query);
|
2014-01-24 17:09:53 +00:00
|
|
|
if (typeof query !== 'object' || query === null) {
|
|
|
|
return query;
|
|
|
|
}
|
2018-10-19 18:56:51 +00:00
|
|
|
options = options || {};
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
// Keep it backward compatible
|
2018-10-26 16:05:47 +00:00
|
|
|
options = {normalizeUndefinedInQuery: options};
|
2018-10-19 18:56:51 +00:00
|
|
|
}
|
|
|
|
const prohibitedKeys = options.prohibitedKeys;
|
|
|
|
const offendingKeys = [];
|
2018-10-26 16:05:47 +00:00
|
|
|
const normalizeUndefinedInQuery = options.normalizeUndefinedInQuery;
|
2018-11-12 21:54:22 +00:00
|
|
|
const maxDepth = options.maxDepth || Number.MAX_SAFE_INTEGER;
|
2014-01-24 17:09:53 +00:00
|
|
|
// WARNING: [rfeng] Use map() will cause mongodb to produce invalid BSON
|
|
|
|
// as traverse doesn't transform the ObjectId correctly
|
2018-10-19 18:56:51 +00:00
|
|
|
const result = traverse(query).forEach(function(x) {
|
|
|
|
/**
|
|
|
|
* Security risk if the client passes in a very deep where object
|
|
|
|
*/
|
2018-10-30 19:14:52 +00:00
|
|
|
if (this.circular) {
|
|
|
|
const msg = g.f('The query object is circular');
|
2018-10-19 18:56:51 +00:00
|
|
|
const err = new Error(msg);
|
|
|
|
err.statusCode = 400;
|
2018-10-30 19:14:52 +00:00
|
|
|
err.code = 'QUERY_OBJECT_IS_CIRCULAR';
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if (this.level > maxDepth) {
|
|
|
|
const msg = g.f('The query object exceeds maximum depth %d', maxDepth);
|
|
|
|
const err = new Error(msg);
|
|
|
|
err.statusCode = 400;
|
|
|
|
err.code = 'QUERY_OBJECT_TOO_DEEP';
|
2018-10-19 18:56:51 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Make sure prohibited keys are removed from the query to prevent
|
|
|
|
* sensitive values from being guessed
|
|
|
|
*/
|
2018-10-29 14:57:55 +00:00
|
|
|
if (isProhibited(this.key, prohibitedKeys)) {
|
2018-10-19 18:56:51 +00:00
|
|
|
offendingKeys.push(this.key);
|
|
|
|
this.remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle undefined values
|
|
|
|
*/
|
2014-01-24 17:09:53 +00:00
|
|
|
if (x === undefined) {
|
2018-10-26 16:05:47 +00:00
|
|
|
switch (normalizeUndefinedInQuery) {
|
2015-09-29 21:40:47 +00:00
|
|
|
case 'nullify':
|
|
|
|
this.update(null);
|
|
|
|
break;
|
|
|
|
case 'throw':
|
2016-07-22 19:26:07 +00:00
|
|
|
throw new Error(g.f('Unexpected `undefined` in query'));
|
2015-09-29 21:40:47 +00:00
|
|
|
case 'ignore':
|
|
|
|
default:
|
|
|
|
this.remove();
|
|
|
|
}
|
2013-10-11 18:50:00 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
if (!Array.isArray(x) && (typeof x === 'object' && x !== null &&
|
|
|
|
x.constructor !== Object)) {
|
2014-01-24 17:09:53 +00:00
|
|
|
// This object is not a plain object
|
|
|
|
this.update(x, true); // Stop navigating into this object
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2019-10-21 16:55:14 +00:00
|
|
|
if (isRegExpOperator(this.key) && typeof x === 'string') { // we have regexp supporting operator and string to escape
|
|
|
|
return escapeRegExp(x);
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
return x;
|
|
|
|
});
|
2013-10-25 23:18:02 +00:00
|
|
|
|
2018-10-12 19:40:33 +00:00
|
|
|
if (offendingKeys.length) {
|
2018-10-19 18:56:51 +00:00
|
|
|
console.error(
|
|
|
|
g.f(
|
|
|
|
'Potential security alert: hidden/protected properties %j are used in query.',
|
2019-12-03 09:09:16 +00:00
|
|
|
offendingKeys,
|
|
|
|
),
|
2018-10-19 18:56:51 +00:00
|
|
|
);
|
2018-10-12 19:40:33 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:56:51 +00:00
|
|
|
const url = require('url');
|
|
|
|
const qs = require('qs');
|
2013-10-25 23:18:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a URL into a settings object
|
|
|
|
* @param {String} urlStr The URL for connector settings
|
|
|
|
* @returns {Object} The settings object
|
|
|
|
*/
|
|
|
|
function parseSettings(urlStr) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!urlStr) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const uri = url.parse(urlStr, false);
|
|
|
|
const settings = {};
|
2014-01-24 17:09:53 +00:00
|
|
|
settings.connector = uri.protocol && uri.protocol.split(':')[0]; // Remove the trailing :
|
|
|
|
settings.host = settings.hostname = uri.hostname;
|
|
|
|
settings.port = uri.port && Number(uri.port); // port is a string
|
|
|
|
settings.user = settings.username = uri.auth && uri.auth.split(':')[0]; // <username>:<password>
|
|
|
|
settings.password = uri.auth && uri.auth.split(':')[1];
|
2018-06-12 07:13:32 +00:00
|
|
|
settings.database = uri.pathname && uri.pathname.split('/')[1]; // remove the leading /
|
2014-01-24 17:09:53 +00:00
|
|
|
settings.url = urlStr;
|
|
|
|
if (uri.query) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const params = qs.parse(uri.query);
|
|
|
|
for (const p in params) {
|
2014-01-24 17:09:53 +00:00
|
|
|
settings[p] = params[p];
|
2013-10-25 23:18:02 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
return settings;
|
2013-10-25 23:18:02 +00:00
|
|
|
}
|
2013-12-06 23:52:39 +00:00
|
|
|
|
|
|
|
/**
|
2017-03-27 22:30:29 +00:00
|
|
|
* Objects deep merge
|
2013-12-06 23:52:39 +00:00
|
|
|
*
|
2017-03-27 22:30:29 +00:00
|
|
|
* Forked from https://github.com/nrf110/deepmerge/blob/master/index.js
|
2013-12-06 23:52:39 +00:00
|
|
|
*
|
2017-03-27 22:30:29 +00:00
|
|
|
* The original function tries to merge array items if they are objects, this
|
|
|
|
* was changed to always push new items in arrays, independently of their type.
|
2013-12-06 23:52:39 +00:00
|
|
|
*
|
2017-03-27 22:30:29 +00:00
|
|
|
* NOTE: The function operates as a deep clone when called with a single object
|
|
|
|
* argument.
|
|
|
|
*
|
|
|
|
* @param {Object} base The base object
|
|
|
|
* @param {Object} extras The object to merge with base
|
|
|
|
* @returns {Object} The merged object
|
2013-12-06 23:52:39 +00:00
|
|
|
*/
|
2017-03-27 22:30:29 +00:00
|
|
|
function deepMerge(base, extras) {
|
|
|
|
// deepMerge allows undefined extras to allow deep cloning of arrays
|
2018-12-07 14:54:29 +00:00
|
|
|
const array = Array.isArray(base) && (Array.isArray(extras) || !extras);
|
|
|
|
let dst = array && [] || {};
|
2013-12-06 23:52:39 +00:00
|
|
|
|
|
|
|
if (array) {
|
2017-03-27 22:30:29 +00:00
|
|
|
// extras or base is an array
|
|
|
|
extras = extras || [];
|
|
|
|
// Add items from base into dst
|
|
|
|
dst = dst.concat(base);
|
|
|
|
// Add non-existent items from extras into dst
|
|
|
|
extras.forEach(function(e) {
|
2013-12-17 01:14:56 +00:00
|
|
|
if (dst.indexOf(e) === -1) {
|
|
|
|
dst.push(e);
|
2013-12-06 23:52:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2017-03-27 22:30:29 +00:00
|
|
|
if (base != null && typeof base === 'object') {
|
|
|
|
// Add properties from base to dst
|
|
|
|
Object.keys(base).forEach(function(key) {
|
|
|
|
if (base[key] && typeof base[key] === 'object') {
|
|
|
|
// call deepMerge on nested object to operate a deep clone
|
|
|
|
dst[key] = deepMerge(base[key]);
|
|
|
|
} else {
|
|
|
|
dst[key] = base[key];
|
|
|
|
}
|
2013-12-06 23:52:39 +00:00
|
|
|
});
|
|
|
|
}
|
2017-03-27 22:30:29 +00:00
|
|
|
if (extras != null && typeof extras === 'object') {
|
|
|
|
// extras is an object {}
|
|
|
|
Object.keys(extras).forEach(function(key) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const extra = extras[key];
|
2017-03-27 22:30:29 +00:00
|
|
|
if (extra == null || typeof extra !== 'object') {
|
|
|
|
// extra item value is null, undefined or not an object
|
|
|
|
dst[key] = extra;
|
2013-12-06 23:52:39 +00:00
|
|
|
} else {
|
2017-03-27 22:30:29 +00:00
|
|
|
// The extra item value is an object
|
|
|
|
if (base == null || typeof base !== 'object' ||
|
|
|
|
base[key] == null) {
|
|
|
|
// base is not an object or base item value is undefined or null
|
|
|
|
dst[key] = extra;
|
2015-07-10 20:54:29 +00:00
|
|
|
} else {
|
2017-03-27 22:30:29 +00:00
|
|
|
// call deepMerge on nested object
|
|
|
|
dst[key] = deepMerge(base[key], extra);
|
2015-07-10 20:54:29 +00:00
|
|
|
}
|
2013-12-06 23:52:39 +00:00
|
|
|
}
|
2015-07-10 20:54:29 +00:00
|
|
|
});
|
|
|
|
}
|
2013-12-06 23:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
2014-01-28 17:57:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 22:30:29 +00:00
|
|
|
/**
|
|
|
|
* Properties deep merge
|
|
|
|
* Similar as deepMerge but also works on single properties of any type
|
|
|
|
*
|
|
|
|
* @param {Object} base The base property
|
|
|
|
* @param {Object} extras The property to merge with base
|
|
|
|
* @returns {Object} The merged property
|
|
|
|
*/
|
|
|
|
function deepMergeProperty(base, extras) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const mergedObject = deepMerge({key: base}, {key: extras});
|
|
|
|
const mergedProperty = mergedObject.key;
|
2017-03-27 22:30:29 +00:00
|
|
|
return mergedProperty;
|
|
|
|
}
|
|
|
|
|
2018-04-17 07:49:28 +00:00
|
|
|
const numberIsFinite = Number.isFinite || function(value) {
|
|
|
|
return typeof value === 'number' && isFinite(value);
|
|
|
|
};
|
|
|
|
|
2017-03-27 22:30:29 +00:00
|
|
|
/**
|
|
|
|
* Adds a property __rank to array elements of type object {}
|
|
|
|
* If an inner element already has the __rank property it is not altered
|
|
|
|
* NOTE: the function mutates the provided array
|
|
|
|
*
|
|
|
|
* @param array The original array
|
|
|
|
* @param rank The rank to apply to array elements
|
|
|
|
* @return rankedArray The original array with newly ranked elements
|
|
|
|
*/
|
|
|
|
function rankArrayElements(array, rank) {
|
2018-04-17 07:49:28 +00:00
|
|
|
if (!Array.isArray(array) || !numberIsFinite(rank))
|
2017-03-27 22:30:29 +00:00
|
|
|
return array;
|
|
|
|
|
|
|
|
array.forEach(function(el) {
|
|
|
|
// only apply ranking on objects {} in array
|
|
|
|
if (!el || typeof el != 'object' || Array.isArray(el))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// property rank is already defined for array element
|
|
|
|
if (el.__rank)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// define rank property as non-enumerable and read-only
|
|
|
|
Object.defineProperty(el, '__rank', {
|
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: false,
|
|
|
|
value: rank,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2014-01-28 17:57:23 +00:00
|
|
|
/**
|
|
|
|
* Define an non-enumerable __cachedRelations property
|
|
|
|
* @param {Object} obj The obj to receive the __cachedRelations
|
|
|
|
*/
|
|
|
|
function defineCachedRelations(obj) {
|
|
|
|
if (!obj.__cachedRelations) {
|
|
|
|
Object.defineProperty(obj, '__cachedRelations', {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: {},
|
2014-01-28 17:57:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the argument is plain object
|
2018-10-12 19:40:33 +00:00
|
|
|
* @param {*} obj The obj value
|
2014-01-28 17:57:23 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isPlainObject(obj) {
|
2016-04-01 13:23:42 +00:00
|
|
|
return (typeof obj === 'object') && (obj !== null) &&
|
|
|
|
(obj.constructor === Object);
|
2014-08-15 17:39:18 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 17:47:12 +00:00
|
|
|
function sortObjectsByIds(idName, ids, objects, strict) {
|
2014-08-15 17:39:18 +00:00
|
|
|
ids = ids.map(function(id) {
|
2016-04-01 11:48:17 +00:00
|
|
|
return (typeof id === 'object') ? String(id) : id;
|
2014-08-15 17:39:18 +00:00
|
|
|
});
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const indexOf = function(x) {
|
|
|
|
const isObj = (typeof x[idName] === 'object'); // ObjectID
|
|
|
|
const id = isObj ? String(x[idName]) : x[idName];
|
2014-08-15 17:39:18 +00:00
|
|
|
return ids.indexOf(id);
|
|
|
|
};
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const heading = [];
|
|
|
|
const tailing = [];
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-08-15 17:39:18 +00:00
|
|
|
objects.forEach(function(x) {
|
|
|
|
if (typeof x === 'object') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const idx = indexOf(x);
|
2014-08-15 17:47:12 +00:00
|
|
|
if (strict && idx === -1) return;
|
2014-08-15 17:39:18 +00:00
|
|
|
idx === -1 ? tailing.push(x) : heading.push(x);
|
|
|
|
}
|
|
|
|
});
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-08-15 17:39:18 +00:00
|
|
|
heading.sort(function(x, y) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const a = indexOf(x);
|
|
|
|
const b = indexOf(y);
|
2014-08-15 17:39:18 +00:00
|
|
|
if (a === -1 || b === -1) return 1; // last
|
|
|
|
if (a === b) return 0;
|
|
|
|
if (a > b) return 1;
|
|
|
|
if (a < b) return -1;
|
|
|
|
});
|
2015-05-20 05:32:03 +00:00
|
|
|
|
2014-08-15 17:39:18 +00:00
|
|
|
return heading.concat(tailing);
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2015-02-17 16:41:46 +00:00
|
|
|
|
|
|
|
function createPromiseCallback() {
|
2018-12-07 14:54:29 +00:00
|
|
|
let cb;
|
|
|
|
const promise = new Promise(function(resolve, reject) {
|
2016-04-01 11:48:17 +00:00
|
|
|
cb = function(err, data) {
|
2015-02-17 16:41:46 +00:00
|
|
|
if (err) return reject(err);
|
|
|
|
return resolve(data);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
cb.promise = promise;
|
|
|
|
return cb;
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:40:17 +00:00
|
|
|
function isBsonType(value) {
|
|
|
|
// bson@1.x stores _bsontype on ObjectID instance, bson@4.x on prototype
|
|
|
|
return value.hasOwnProperty('_bsontype') ||
|
|
|
|
value.constructor.prototype.hasOwnProperty('_bsontype');
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:50:37 +00:00
|
|
|
/**
|
|
|
|
* Dedupe an array
|
|
|
|
* @param {Array} an array
|
|
|
|
* @returns {Array} an array with unique items
|
|
|
|
*/
|
|
|
|
function uniq(a) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const uniqArray = [];
|
2015-05-29 17:50:37 +00:00
|
|
|
if (!a) {
|
|
|
|
return uniqArray;
|
|
|
|
}
|
|
|
|
assert(Array.isArray(a), 'array argument is required');
|
2018-12-07 14:54:29 +00:00
|
|
|
const comparableA = a.map(
|
2019-12-03 09:09:16 +00:00
|
|
|
item => isBsonType(item) ? item.toString() : item,
|
2018-07-16 06:46:25 +00:00
|
|
|
);
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0, n = comparableA.length; i < n; i++) {
|
2017-12-04 20:39:58 +00:00
|
|
|
if (comparableA.indexOf(comparableA[i]) === i) {
|
2015-05-29 17:50:37 +00:00
|
|
|
uniqArray.push(a[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uniqArray;
|
|
|
|
}
|
2015-07-24 19:56:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a string, regex literal, or a RegExp object to a RegExp object.
|
|
|
|
* @param {String|Object} The string, regex literal, or RegExp object to convert
|
|
|
|
* @returns {Object} A RegExp object
|
|
|
|
*/
|
|
|
|
function toRegExp(regex) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const isString = typeof regex === 'string';
|
|
|
|
const isRegExp = regex instanceof RegExp;
|
2015-07-24 19:56:31 +00:00
|
|
|
|
|
|
|
if (!(isString || isRegExp))
|
2016-07-22 19:26:07 +00:00
|
|
|
return new Error(g.f('Invalid argument, must be a string, {{regex}} literal, or ' +
|
|
|
|
'{{RegExp}} object'));
|
2015-07-24 19:56:31 +00:00
|
|
|
|
|
|
|
if (isRegExp)
|
|
|
|
return regex;
|
|
|
|
|
|
|
|
if (!hasRegExpFlags(regex))
|
|
|
|
return new RegExp(regex);
|
|
|
|
|
|
|
|
// only accept i, g, or m as valid regex flags
|
2018-12-07 14:54:29 +00:00
|
|
|
const flags = regex.split('/').pop().split('');
|
|
|
|
const validFlags = ['i', 'g', 'm'];
|
|
|
|
const invalidFlags = [];
|
2015-07-24 19:56:31 +00:00
|
|
|
flags.forEach(function(flag) {
|
|
|
|
if (validFlags.indexOf(flag) === -1)
|
|
|
|
invalidFlags.push(flag);
|
|
|
|
});
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const hasInvalidFlags = invalidFlags.length > 0;
|
2015-07-24 19:56:31 +00:00
|
|
|
if (hasInvalidFlags)
|
2016-07-22 19:26:07 +00:00
|
|
|
return new Error(g.f('Invalid {{regex}} flags: %s', invalidFlags));
|
2015-07-24 19:56:31 +00:00
|
|
|
|
|
|
|
// strip regex delimiter forward slashes
|
2018-12-07 14:54:29 +00:00
|
|
|
const expression = regex.substr(1, regex.lastIndexOf('/') - 1);
|
2015-07-24 19:56:31 +00:00
|
|
|
return new RegExp(expression, flags.join(''));
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasRegExpFlags(regex) {
|
|
|
|
return regex instanceof RegExp ?
|
|
|
|
regex.toString().split('/').pop() :
|
|
|
|
!!regex.match(/.*\/.+$/);
|
|
|
|
}
|
2015-08-17 19:49:38 +00:00
|
|
|
|
|
|
|
// Compare two id values to decide if updateAttributes is trying to change
|
|
|
|
// the id value for a given instance
|
|
|
|
function idEquals(id1, id2) {
|
|
|
|
if (id1 === id2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Allows number/string conversions
|
|
|
|
if ((typeof id1 === 'number' && typeof id2 === 'string') ||
|
|
|
|
(typeof id1 === 'string' && typeof id2 === 'number')) {
|
|
|
|
return id1 == id2;
|
|
|
|
}
|
|
|
|
// For complex id types such as MongoDB ObjectID
|
|
|
|
id1 = JSON.stringify(id1);
|
|
|
|
id2 = JSON.stringify(id2);
|
|
|
|
if (id1 === id2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-18 14:22:36 +00:00
|
|
|
|
|
|
|
// Defaults to native Array.prototype.indexOf when no idEqual is present
|
|
|
|
// Otherwise, returns the lowest index for which isEqual(arr[]index, target) is true
|
|
|
|
function findIndexOf(arr, target, isEqual) {
|
|
|
|
if (!isEqual) {
|
|
|
|
return arr.indexOf(target);
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
for (let i = 0; i < arr.length; i++) {
|
2015-08-18 14:22:36 +00:00
|
|
|
if (isEqual(arr[i], target)) { return i; }
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2015-08-18 14:22:36 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2016-10-27 14:18:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object that queries targetIds.
|
|
|
|
* @param {Array} The array of targetData
|
|
|
|
* @param {String} The Id property name of target model
|
|
|
|
* @returns {Object} The object that queries targetIds
|
|
|
|
*/
|
|
|
|
function collectTargetIds(targetData, idPropertyName) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const targetIds = [];
|
|
|
|
for (let i = 0; i < targetData.length; i++) {
|
|
|
|
const targetId = targetData[i][idPropertyName];
|
2016-10-27 14:18:54 +00:00
|
|
|
targetIds.push(targetId);
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2018-12-07 14:54:29 +00:00
|
|
|
const IdQuery = {
|
2016-10-27 14:18:54 +00:00
|
|
|
inq: uniq(targetIds),
|
|
|
|
};
|
|
|
|
return IdQuery;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the idKey of a Model.
|
|
|
|
* @param {ModelConstructor} m - Model Constructor
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
function idName(m) {
|
|
|
|
return m.definition.idName() || 'id';
|
|
|
|
}
|
2018-10-03 22:17:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check a list of IDs to see if there are any duplicates.
|
|
|
|
*
|
|
|
|
* @param {Array} The array of IDs to check
|
|
|
|
* @returns {boolean} If any duplicates were found
|
|
|
|
*/
|
|
|
|
function idsHaveDuplicates(ids) {
|
|
|
|
// use Set if available and all ids are of string or number type
|
2018-12-07 14:54:29 +00:00
|
|
|
let hasDuplicates = undefined;
|
|
|
|
let i, j;
|
2018-10-03 22:17:08 +00:00
|
|
|
if (typeof Set === 'function') {
|
2018-12-07 14:54:29 +00:00
|
|
|
const uniqueIds = new Set();
|
2018-10-03 22:17:08 +00:00
|
|
|
for (i = 0; i < ids.length; ++i) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const idType = typeof ids[i];
|
2018-10-03 22:17:08 +00:00
|
|
|
if (idType === 'string' || idType === 'number') {
|
|
|
|
if (uniqueIds.has(ids[i])) {
|
|
|
|
hasDuplicates = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
uniqueIds.add(ids[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ids are not all string/number that can be checked via Set, stop and do the slow test
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 22:56:57 +00:00
|
|
|
if (hasDuplicates === undefined && uniqueIds.size === ids.length) {
|
2018-10-03 22:17:08 +00:00
|
|
|
hasDuplicates = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasDuplicates === undefined) {
|
|
|
|
// fast check was inconclusive or unavailable, do the slow check
|
|
|
|
// can still optimize this by doing 1/2 N^2 instead of the full N^2
|
|
|
|
for (i = 0; i < ids.length && hasDuplicates === undefined; ++i) {
|
|
|
|
for (j = 0; j < i; ++j) {
|
|
|
|
if (idEquals(ids[i], ids[j])) {
|
|
|
|
hasDuplicates = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasDuplicates === true;
|
|
|
|
}
|
2019-01-25 08:46:31 +00:00
|
|
|
|
|
|
|
function isClass(fn) {
|
|
|
|
return fn && fn.toString().startsWith('class ');
|
|
|
|
}
|
2019-10-23 16:31:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Accept an element, and attach the __parent property to it, unless no object given, while also
|
|
|
|
* making sure to check for already created properties
|
|
|
|
*
|
|
|
|
* @param {object} element
|
|
|
|
* @param {Model} parent
|
|
|
|
*/
|
|
|
|
function applyParentProperty(element, parent) {
|
|
|
|
assert.strictEqual(typeof element, 'object', 'Non object element given to assign parent');
|
|
|
|
const {constructor: {modelBuilder: {settings: builderSettings} = {}} = {}} = element;
|
|
|
|
if (!builderSettings || !builderSettings[BUILDER_PARENT_SETTING]) {
|
|
|
|
// parentRef flag not enabled on ModelBuilder settings
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element.hasOwnProperty(PARENT_PROPERTY_NAME)) {
|
|
|
|
// property already created on model, just assign
|
|
|
|
const existingParent = element[PARENT_PROPERTY_NAME];
|
|
|
|
if (existingParent && existingParent !== parent) {
|
|
|
|
// parent re-assigned (child model assigned to other model instance)
|
|
|
|
g.warn('Re-assigning child model instance to another parent than the original!\n' +
|
|
|
|
'Although supported, this is not a recommended practice: ' +
|
|
|
|
`${element.constructor.name} -> ${parent.constructor.name}\n` +
|
|
|
|
'You should create an independent copy of the child model using `new Model(CHILD)` OR ' +
|
|
|
|
'`new Model(CHILD.toJSON())` and assign to new parent');
|
|
|
|
}
|
|
|
|
element[PARENT_PROPERTY_NAME] = parent;
|
|
|
|
} else {
|
|
|
|
// first time defining the property on the element
|
|
|
|
Object.defineProperty(element, PARENT_PROPERTY_NAME, {
|
|
|
|
value: parent,
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|