loopback-datasource-juggler/lib/model-utils.js

587 lines
16 KiB
JavaScript
Raw Normal View History

2018-11-12 21:54:22 +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
// Turning on strict for this file breaks lots of test cases;
// disabling strict for this file
/* eslint-disable strict */
module.exports = ModelUtils;
/*!
* Module dependencies
*/
2018-12-07 14:54:29 +00:00
const g = require('strong-globalize')();
const geo = require('./geo');
const {
fieldsToArray,
sanitizeQuery: sanitizeQueryOrData,
isPlainObject,
isClass,
toRegExp,
} = require('./utils');
2018-12-07 14:54:29 +00:00
const BaseModel = require('./model');
2018-11-12 21:54:22 +00:00
/**
* A mixin to contain utility methods for DataAccessObject
*/
function ModelUtils() {
}
/**
* Verify if allowExtendedOperators is enabled
* @options {Object} [options] Optional options to use.
* @property {Boolean} allowExtendedOperators.
* @returns {Boolean} Returns `true` if allowExtendedOperators is enabled, else `false`.
*/
ModelUtils._allowExtendedOperators = function(options) {
const flag = this._getSetting('allowExtendedOperators', options);
if (flag != null) return !!flag;
// Default to `false`
return false;
};
/**
* Get settings via hierarchical determination
* - method level options
* - model level settings
* - data source level settings
*
* @param {String} key The setting key
*/
ModelUtils._getSetting = function(key, options) {
// Check method level options
2018-12-07 14:54:29 +00:00
let val = options && options[key];
2018-11-12 21:54:22 +00:00
if (val !== undefined) return val;
// Check for settings in model
2018-12-07 14:54:29 +00:00
const m = this.definition;
2018-11-12 21:54:22 +00:00
if (m && m.settings) {
val = m.settings[key];
if (val !== undefined) {
return m.settings[key];
}
// Fall back to data source level
}
// Check for settings in connector
2018-12-07 14:54:29 +00:00
const ds = this.getDataSource();
2018-11-12 21:54:22 +00:00
if (ds && ds.settings) {
return ds.settings[key];
}
return undefined;
};
2018-12-07 14:54:29 +00:00
const operators = {
2018-11-12 21:54:22 +00:00
eq: '=',
gt: '>',
gte: '>=',
lt: '<',
lte: '<=',
between: 'BETWEEN',
inq: 'IN',
nin: 'NOT IN',
neq: '!=',
like: 'LIKE',
nlike: 'NOT LIKE',
ilike: 'ILIKE',
nilike: 'NOT ILIKE',
regexp: 'REGEXP',
};
/*
* Normalize the filter object and throw errors if invalid values are detected
* @param {Object} filter The query filter object
* @options {Object} [options] Optional options to use.
* @property {Boolean} allowExtendedOperators.
* @returns {Object} The normalized filter object
* @private
*/
ModelUtils._normalize = function(filter, options) {
if (!filter) {
return undefined;
}
2018-12-07 14:54:29 +00:00
let err = null;
2018-11-12 21:54:22 +00:00
if ((typeof filter !== 'object') || Array.isArray(filter)) {
err = new Error(g.f('The query filter %j is not an {{object}}', filter));
err.statusCode = 400;
throw err;
}
if (filter.limit || filter.skip || filter.offset) {
2018-12-07 14:54:29 +00:00
const limit = Number(filter.limit || 100);
const offset = Number(filter.skip || filter.offset || 0);
2018-11-12 21:54:22 +00:00
if (isNaN(limit) || limit <= 0 || Math.ceil(limit) !== limit) {
err = new Error(g.f('The {{limit}} parameter %j is not valid',
filter.limit));
err.statusCode = 400;
throw err;
}
if (isNaN(offset) || offset < 0 || Math.ceil(offset) !== offset) {
err = new Error(g.f('The {{offset/skip}} parameter %j is not valid',
filter.skip || filter.offset));
err.statusCode = 400;
throw err;
}
filter.limit = limit;
filter.offset = offset;
filter.skip = offset;
}
if (filter.order) {
2018-12-07 14:54:29 +00:00
let order = filter.order;
2018-11-12 21:54:22 +00:00
if (!Array.isArray(order)) {
order = [order];
}
2018-12-07 14:54:29 +00:00
const fields = [];
for (let i = 0, m = order.length; i < m; i++) {
2018-11-12 21:54:22 +00:00
if (typeof order[i] === 'string') {
// Normalize 'f1 ASC, f2 DESC, f3' to ['f1 ASC', 'f2 DESC', 'f3']
2018-12-07 14:54:29 +00:00
const tokens = order[i].split(/(?:\s*,\s*)+/);
for (let t = 0, n = tokens.length; t < n; t++) {
let token = tokens[t];
2018-11-12 21:54:22 +00:00
if (token.length === 0) {
// Skip empty token
continue;
}
2018-12-07 14:54:29 +00:00
const parts = token.split(/\s+/);
2018-11-12 21:54:22 +00:00
if (parts.length >= 2) {
2018-12-07 14:54:29 +00:00
const dir = parts[1].toUpperCase();
2018-11-12 21:54:22 +00:00
if (dir === 'ASC' || dir === 'DESC') {
token = parts[0] + ' ' + dir;
} else {
err = new Error(g.f('The {{order}} %j has invalid direction', token));
err.statusCode = 400;
throw err;
}
}
fields.push(token);
}
} else {
err = new Error(g.f('The order %j is not valid', order[i]));
err.statusCode = 400;
throw err;
}
}
if (fields.length === 1 && typeof filter.order === 'string') {
filter.order = fields[0];
} else {
filter.order = fields;
}
}
// normalize fields as array of included property names
if (filter.fields) {
filter.fields = fieldsToArray(filter.fields,
Object.keys(this.definition.properties), this.settings.strict);
}
filter = this._sanitizeQuery(filter, options);
this._coerce(filter.where, options);
return filter;
};
function DateType(arg) {
2018-12-07 14:54:29 +00:00
const d = new Date(arg);
2018-11-12 21:54:22 +00:00
if (isNaN(d.getTime())) {
throw new Error(g.f('Invalid date: %s', arg));
}
return d;
}
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);
}
function NumberType(val) {
2018-12-07 14:54:29 +00:00
const num = Number(val);
2018-11-12 21:54:22 +00:00
return !isNaN(num) ? num : val;
}
function coerceArray(val) {
if (Array.isArray(val)) {
return val;
}
if (!isPlainObject(val)) {
2018-11-12 21:54:22 +00:00
throw new Error(g.f('Value is not an {{array}} or {{object}} with sequential numeric indices'));
}
// It is an object, check if empty
2018-12-07 14:54:29 +00:00
const props = Object.keys(val);
2018-11-12 21:54:22 +00:00
if (props.length === 0) {
throw new Error(g.f('Value is an empty {{object}}'));
}
2018-12-07 14:54:29 +00:00
const arrayVal = new Array(props.length);
for (let i = 0; i < arrayVal.length; ++i) {
2018-11-12 21:54:22 +00:00
if (!val.hasOwnProperty(i)) {
throw new Error(g.f('Value is not an {{array}} or {{object}} with sequential numeric indices'));
}
arrayVal[i] = val[i];
}
return arrayVal;
}
function _normalizeAsArray(result) {
if (typeof result === 'string') {
result = [result];
}
if (Array.isArray(result)) {
return result;
} else {
// See https://github.com/strongloop/loopback-datasource-juggler/issues/1646
// `ModelBaseClass` normalize the properties to an object such as `{secret: true}`
2018-12-07 14:54:29 +00:00
const keys = [];
for (const k in result) {
2018-11-12 21:54:22 +00:00
if (result[k]) keys.push(k);
}
return keys;
}
}
/**
* Get an array of hidden property names
*/
ModelUtils._getHiddenProperties = function() {
2018-12-07 14:54:29 +00:00
const settings = this.definition.settings || {};
const result = settings.hiddenProperties || settings.hidden || [];
2018-11-12 21:54:22 +00:00
return _normalizeAsArray(result);
};
/**
* Get an array of protected property names
*/
ModelUtils._getProtectedProperties = function() {
2018-12-07 14:54:29 +00:00
const settings = this.definition.settings || {};
const result = settings.protectedProperties || settings.protected || [];
2018-11-12 21:54:22 +00:00
return _normalizeAsArray(result);
};
/**
* Get the maximum depth of a query object
*/
ModelUtils._getMaxDepthOfQuery = function(options, defaultValue) {
options = options || {};
// See https://github.com/strongloop/loopback-datasource-juggler/issues/1651
2018-12-07 14:54:29 +00:00
let maxDepth = this._getSetting('maxDepthOfQuery', options);
2018-11-12 21:54:22 +00:00
if (maxDepth == null) {
maxDepth = defaultValue || 32;
}
return +maxDepth;
};
/**
* Get the maximum depth of a data object
*/
ModelUtils._getMaxDepthOfData = function(options, defaultValue) {
options = options || {};
// See https://github.com/strongloop/loopback-datasource-juggler/issues/1651
2018-12-07 14:54:29 +00:00
let maxDepth = this._getSetting('maxDepthOfData', options);
2018-11-12 21:54:22 +00:00
if (maxDepth == null) {
maxDepth = defaultValue || 64;
}
return +maxDepth;
};
/**
* Get the prohibitHiddenPropertiesInQuery flag
*/
ModelUtils._getProhibitHiddenPropertiesInQuery = function(options, defaultValue) {
2018-12-07 14:54:29 +00:00
const flag = this._getSetting('prohibitHiddenPropertiesInQuery', options);
2018-11-12 21:54:22 +00:00
if (flag == null) return !!defaultValue;
return !!flag;
};
/**
* Sanitize the query object
*/
ModelUtils._sanitizeQuery = function(query, options) {
options = options || {};
// Get settings to normalize `undefined` values
2018-12-07 14:54:29 +00:00
const normalizeUndefinedInQuery = this._getSetting('normalizeUndefinedInQuery', options);
2018-11-12 21:54:22 +00:00
// Get setting to prohibit hidden/protected properties in query
2018-12-07 14:54:29 +00:00
const prohibitHiddenPropertiesInQuery = this._getProhibitHiddenPropertiesInQuery(options);
2018-11-12 21:54:22 +00:00
// See https://github.com/strongloop/loopback-datasource-juggler/issues/1651
2018-12-07 14:54:29 +00:00
const maxDepthOfQuery = this._getMaxDepthOfQuery(options);
2018-11-12 21:54:22 +00:00
2018-12-07 14:54:29 +00:00
let prohibitedKeys = [];
2018-11-12 21:54:22 +00:00
// Check violation of keys
if (prohibitHiddenPropertiesInQuery) {
prohibitedKeys = this._getHiddenProperties();
if (options.prohibitProtectedPropertiesInQuery) {
prohibitedKeys = prohibitedKeys.concat(this._getProtectedProperties());
}
}
return sanitizeQueryOrData(query,
Object.assign({
maxDepth: maxDepthOfQuery,
prohibitedKeys: prohibitedKeys,
normalizeUndefinedInQuery: normalizeUndefinedInQuery,
}, options));
};
/**
* Sanitize the data object
*/
ModelUtils._sanitizeData = function(data, options) {
options = options || {};
return sanitizeQueryOrData(data,
Object.assign({
maxDepth: this._getMaxDepthOfData(options),
}, options));
};
/*
* Coerce values based the property types
* @param {Object} where The where clause
* @options {Object} [options] Optional options to use.
* @param {Object} Optional model definition to use.
2018-11-12 21:54:22 +00:00
* @property {Boolean} allowExtendedOperators.
* @returns {Object} The coerced where clause
* @private
*/
ModelUtils._coerce = function(where, options, modelDef) {
2018-12-07 14:54:29 +00:00
const self = this;
2018-11-12 21:54:22 +00:00
if (where == null) {
return where;
}
options = options || {};
2018-12-07 14:54:29 +00:00
let err;
2018-11-12 21:54:22 +00:00
if (typeof where !== 'object' || Array.isArray(where)) {
err = new Error(g.f('The where clause %j is not an {{object}}', where));
err.statusCode = 400;
throw err;
}
let props;
if (modelDef && modelDef.properties) {
props = modelDef.properties;
} else {
props = self.definition.properties;
}
2018-11-12 21:54:22 +00:00
2018-12-07 14:54:29 +00:00
for (const p in where) {
2018-11-12 21:54:22 +00:00
// Handle logical operators
if (p === 'and' || p === 'or' || p === 'nor') {
2018-12-07 14:54:29 +00:00
let clauses = where[p];
2018-11-12 21:54:22 +00:00
try {
clauses = coerceArray(clauses);
} catch (e) {
err = new Error(g.f('The %s operator has invalid clauses %j: %s', p, clauses, e.message));
err.statusCode = 400;
throw err;
}
2018-12-07 14:54:29 +00:00
for (let k = 0; k < clauses.length; k++) {
2018-11-12 21:54:22 +00:00
self._coerce(clauses[k], options);
}
where[p] = clauses;
2018-11-12 21:54:22 +00:00
continue;
}
2018-12-07 14:54:29 +00:00
let DataType = props[p] && props[p].type;
2018-11-12 21:54:22 +00:00
if (!DataType) {
continue;
}
if ((Array.isArray(DataType) || DataType === Array) && !isNestedModel(DataType)) {
2018-11-12 21:54:22 +00:00
DataType = DataType[0];
}
if (DataType === Date) {
DataType = DateType;
} else if (DataType === Boolean) {
DataType = BooleanType;
} else if (DataType === Number) {
// This fixes a regression in mongodb connector
// For numbers, only convert it produces a valid number
// LoopBack by default injects a number id. We should fix it based
// on the connector's input, for example, MongoDB should use string
// while RDBs typically use number
DataType = NumberType;
}
if (!DataType) {
continue;
}
if (DataType === geo.GeoPoint) {
// Skip the GeoPoint as the near operator breaks the assumption that
// an operation has only one property
// We should probably fix it based on
// http://docs.mongodb.org/manual/reference/operator/query/near/
// The other option is to make operators start with $
continue;
}
2018-12-07 14:54:29 +00:00
let val = where[p];
2018-11-12 21:54:22 +00:00
if (val === null || val === undefined) {
continue;
}
// Check there is an operator
2018-12-07 14:54:29 +00:00
let operator = null;
const exp = val;
2018-11-12 21:54:22 +00:00
if (val.constructor === Object) {
2018-12-07 14:54:29 +00:00
for (const op in operators) {
2018-11-12 21:54:22 +00:00
if (op in val) {
val = val[op];
operator = op;
switch (operator) {
case 'inq':
case 'nin':
case 'between':
try {
val = coerceArray(val);
} catch (e) {
err = new Error(g.f('The %s property has invalid clause %j: %s', p, where[p], e));
err.statusCode = 400;
throw err;
}
if (operator === 'between' && val.length !== 2) {
err = new Error(g.f(
'The %s property has invalid clause %j: Expected precisely 2 values, received %d',
p,
where[p],
val.length
));
err.statusCode = 400;
throw err;
}
break;
case 'like':
case 'nlike':
case 'ilike':
case 'nilike':
if (!(typeof val === 'string' || val instanceof RegExp)) {
err = new Error(g.f(
'The %s property has invalid clause %j: Expected a string or RegExp',
p,
where[p]
));
err.statusCode = 400;
throw err;
}
break;
case 'regexp':
val = toRegExp(val);
2018-11-12 21:54:22 +00:00
if (val instanceof Error) {
val.statusCode = 400;
throw val;
}
break;
}
break;
}
}
}
try {
// Coerce val into an array if it resembles an array-like object
val = coerceArray(val);
} catch (e) {
// NOOP when not coercable into an array.
}
2018-12-07 14:54:29 +00:00
const allowExtendedOperators = self._allowExtendedOperators(options);
2018-11-12 21:54:22 +00:00
// Coerce the array items
if (Array.isArray(val) && !isNestedModel(DataType)) {
2018-12-07 14:54:29 +00:00
for (let i = 0; i < val.length; i++) {
2018-11-12 21:54:22 +00:00
if (val[i] !== null && val[i] !== undefined) {
if (!(val[i] instanceof RegExp)) {
val[i] = isClass(DataType) ? new DataType(val[i]) : DataType(val[i]);
2018-11-12 21:54:22 +00:00
}
}
}
} else {
if (val != null) {
if (operator === null && val instanceof RegExp) {
// Normalize {name: /A/} to {name: {regexp: /A/}}
operator = 'regexp';
} else if (operator === 'regexp' && val instanceof RegExp) {
// Do not coerce regex literals/objects
} else if ((operator === 'like' || operator === 'nlike' ||
operator === 'ilike' || operator === 'nilike') && val instanceof RegExp) {
// Do not coerce RegExp operator value
} else if (allowExtendedOperators && typeof val === 'object') {
// Do not coerce object values when extended operators are allowed
} else {
if (!allowExtendedOperators) {
2018-12-07 14:54:29 +00:00
const extendedOperators = Object.keys(val).filter(function(k) {
2018-11-12 21:54:22 +00:00
return k[0] === '$';
});
if (extendedOperators.length) {
const msg = g.f('Operators "' + extendedOperators.join(', ') + '" are not allowed in query');
const err = new Error(msg);
err.code = 'OPERATOR_NOT_ALLOWED_IN_QUERY';
err.statusCode = 400;
err.details = {
operators: extendedOperators,
where: where,
};
throw err;
}
}
if (isNestedModel(DataType)) {
if (Array.isArray(DataType) && Array.isArray(val)) {
if (val === null || val === undefined) continue;
for (const it of val) {
self._coerce(it, options, DataType[0].definition);
}
} else {
self._coerce(val, options, DataType.definition);
}
continue;
} else {
val = isClass(DataType) ? new DataType(val) : DataType(val);
}
2018-11-12 21:54:22 +00:00
}
}
}
// Rebuild {property: {operator: value}}
if (operator && operator !== 'eq') {
2018-12-07 14:54:29 +00:00
const value = {};
2018-11-12 21:54:22 +00:00
value[operator] = val;
if (exp.options) {
// Keep options for operators
value.options = exp.options;
}
val = value;
}
where[p] = val;
}
return where;
};
/**
* A utility function which checks for nested property definitions
*
* @param {*} propType Property type metadata
*
*/
function isNestedModel(propType) {
if (!propType) return false;
if (Array.isArray(propType)) return isNestedModel(propType[0]);
return propType.hasOwnProperty('definition') && propType.definition.hasOwnProperty('properties');
}