UNPKG

1.79 kBJavaScriptView Raw
1/* eslint no-console:0 */
2
3const {
4 isFunction,
5 isUndefined,
6 isPlainObject,
7 isArray,
8 isTypedArray,
9} = require('lodash');
10const { CLIENT_ALIASES } = require('./constants');
11
12// Check if the first argument is an array, otherwise uses all arguments as an
13// array.
14
15function normalizeArr() {
16 const args = new Array(arguments.length);
17 for (let i = 0; i < args.length; i++) {
18 args[i] = arguments[i];
19 }
20 if (Array.isArray(args[0])) {
21 return args[0];
22 }
23 return args;
24}
25
26function containsUndefined(mixed) {
27 let argContainsUndefined = false;
28
29 if (isTypedArray(mixed)) return false;
30
31 if (mixed && isFunction(mixed.toSQL)) {
32 //Any QueryBuilder or Raw will automatically be validated during compile.
33 return argContainsUndefined;
34 }
35
36 if (isArray(mixed)) {
37 for (let i = 0; i < mixed.length; i++) {
38 if (argContainsUndefined) break;
39 argContainsUndefined = this.containsUndefined(mixed[i]);
40 }
41 } else if (isPlainObject(mixed)) {
42 Object.keys(mixed).forEach((key) => {
43 if (!argContainsUndefined) {
44 argContainsUndefined = this.containsUndefined(mixed[key]);
45 }
46 });
47 } else {
48 argContainsUndefined = isUndefined(mixed);
49 }
50
51 return argContainsUndefined;
52}
53
54function addQueryContext(Target) {
55 // Stores or returns (if called with no arguments) context passed to
56 // wrapIdentifier and postProcessResponse hooks
57 Target.prototype.queryContext = function(context) {
58 if (isUndefined(context)) {
59 return this._queryContext;
60 }
61 this._queryContext = context;
62 return this;
63 };
64}
65
66function resolveClientNameWithAliases(clientName) {
67 return CLIENT_ALIASES[clientName] || clientName;
68}
69
70module.exports = {
71 addQueryContext,
72 containsUndefined,
73 normalizeArr,
74 resolveClientNameWithAliases,
75};