2020-04-18 20:41:23 +03:00
|
|
|
const isPlainObject = require('lodash/isPlainObject');
|
|
|
|
const isTypedArray = require('lodash/isTypedArray');
|
2019-06-04 00:37:17 +02:00
|
|
|
const { CLIENT_ALIASES } = require('./constants');
|
2020-10-05 21:29:39 +03:00
|
|
|
const { isFunction } = require('./util/is');
|
2016-05-17 01:01:34 +10:00
|
|
|
|
|
|
|
// Check if the first argument is an array, otherwise uses all arguments as an
|
|
|
|
// array.
|
2019-06-04 00:37:17 +02:00
|
|
|
|
|
|
|
function normalizeArr() {
|
2016-05-17 01:01:34 +10:00
|
|
|
const args = new Array(arguments.length);
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
|
|
args[i] = arguments[i];
|
|
|
|
}
|
|
|
|
if (Array.isArray(args[0])) {
|
|
|
|
return args[0];
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
function containsUndefined(mixed) {
|
2016-05-28 19:45:00 +02:00
|
|
|
let argContainsUndefined = false;
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
if (isTypedArray(mixed)) return false;
|
2016-07-31 08:22:18 -04:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
if (mixed && isFunction(mixed.toSQL)) {
|
2016-05-28 19:45:00 +02:00
|
|
|
//Any QueryBuilder or Raw will automatically be validated during compile.
|
|
|
|
return argContainsUndefined;
|
|
|
|
}
|
|
|
|
|
2020-04-18 20:41:23 +03:00
|
|
|
if (Array.isArray(mixed)) {
|
2018-07-09 08:10:34 -04:00
|
|
|
for (let i = 0; i < mixed.length; i++) {
|
|
|
|
if (argContainsUndefined) break;
|
2019-09-04 23:59:04 +03:00
|
|
|
argContainsUndefined = containsUndefined(mixed[i]);
|
2016-05-28 19:45:00 +02:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
} else if (isPlainObject(mixed)) {
|
2019-07-10 20:47:56 +02:00
|
|
|
Object.keys(mixed).forEach((key) => {
|
|
|
|
if (!argContainsUndefined) {
|
2019-09-04 23:59:04 +03:00
|
|
|
argContainsUndefined = containsUndefined(mixed[key]);
|
2017-10-31 15:15:53 -07:00
|
|
|
}
|
2019-07-10 20:47:56 +02:00
|
|
|
});
|
2016-05-28 19:45:00 +02:00
|
|
|
} else {
|
2020-04-18 20:41:23 +03:00
|
|
|
argContainsUndefined = mixed === undefined;
|
2016-05-28 19:45:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return argContainsUndefined;
|
2018-02-01 23:41:01 +01:00
|
|
|
}
|
|
|
|
|
2019-09-04 23:59:04 +03:00
|
|
|
function getUndefinedIndices(mixed) {
|
|
|
|
const indices = [];
|
|
|
|
|
|
|
|
if (Array.isArray(mixed)) {
|
|
|
|
mixed.forEach((item, index) => {
|
|
|
|
if (containsUndefined(item)) {
|
|
|
|
indices.push(index);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (isPlainObject(mixed)) {
|
|
|
|
Object.keys(mixed).forEach((key) => {
|
|
|
|
if (containsUndefined(mixed[key])) {
|
|
|
|
indices.push(key);
|
|
|
|
}
|
2019-10-06 20:21:32 +02:00
|
|
|
});
|
2019-09-04 23:59:04 +03:00
|
|
|
} else {
|
|
|
|
indices.push(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return indices;
|
|
|
|
}
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
function addQueryContext(Target) {
|
2018-02-01 23:41:01 +01:00
|
|
|
// Stores or returns (if called with no arguments) context passed to
|
|
|
|
// wrapIdentifier and postProcessResponse hooks
|
2020-04-19 00:40:23 +02:00
|
|
|
Target.prototype.queryContext = function (context) {
|
2020-04-18 20:41:23 +03:00
|
|
|
if (context === undefined) {
|
2018-02-01 23:41:01 +01:00
|
|
|
return this._queryContext;
|
|
|
|
}
|
|
|
|
this._queryContext = context;
|
|
|
|
return this;
|
2018-07-09 08:10:34 -04:00
|
|
|
};
|
2018-02-01 23:41:01 +01:00
|
|
|
}
|
2018-11-16 13:23:22 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
function resolveClientNameWithAliases(clientName) {
|
2018-11-16 13:23:22 +01:00
|
|
|
return CLIENT_ALIASES[clientName] || clientName;
|
|
|
|
}
|
2019-06-04 00:37:17 +02:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
addQueryContext,
|
|
|
|
containsUndefined,
|
|
|
|
normalizeArr,
|
|
|
|
resolveClientNameWithAliases,
|
2019-09-04 23:59:04 +03:00
|
|
|
getUndefinedIndices,
|
2019-06-04 00:37:17 +02:00
|
|
|
};
|