2016-05-17 01:01:34 +10:00
|
|
|
/* eslint no-console:0 */
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
const {
|
2018-02-14 17:00:50 +01:00
|
|
|
isFunction,
|
|
|
|
isUndefined,
|
|
|
|
isPlainObject,
|
|
|
|
isArray,
|
2018-07-09 08:10:34 -04:00
|
|
|
isTypedArray,
|
2019-06-04 00:37:17 +02:00
|
|
|
} = require('lodash');
|
|
|
|
const { CLIENT_ALIASES } = require('./constants');
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
if (isArray(mixed)) {
|
|
|
|
for (let i = 0; i < mixed.length; i++) {
|
|
|
|
if (argContainsUndefined) break;
|
2016-05-28 19:45:00 +02:00
|
|
|
argContainsUndefined = this.containsUndefined(mixed[i]);
|
|
|
|
}
|
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) {
|
2017-10-31 15:15:53 -07:00
|
|
|
argContainsUndefined = this.containsUndefined(mixed[key]);
|
|
|
|
}
|
2019-07-10 20:47:56 +02:00
|
|
|
});
|
2016-05-28 19:45:00 +02:00
|
|
|
} else {
|
|
|
|
argContainsUndefined = isUndefined(mixed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return argContainsUndefined;
|
2018-02-01 23:41:01 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
Target.prototype.queryContext = function(context) {
|
|
|
|
if (isUndefined(context)) {
|
|
|
|
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,
|
|
|
|
};
|