1 |
|
2 |
|
3 | const {
|
4 | isFunction,
|
5 | isUndefined,
|
6 | isPlainObject,
|
7 | isArray,
|
8 | isTypedArray,
|
9 | } = require('lodash');
|
10 | const { CLIENT_ALIASES } = require('./constants');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function 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 |
|
26 | function containsUndefined(mixed) {
|
27 | let argContainsUndefined = false;
|
28 |
|
29 | if (isTypedArray(mixed)) return false;
|
30 |
|
31 | if (mixed && isFunction(mixed.toSQL)) {
|
32 |
|
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 |
|
54 | function addQueryContext(Target) {
|
55 |
|
56 |
|
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 |
|
66 | function resolveClientNameWithAliases(clientName) {
|
67 | return CLIENT_ALIASES[clientName] || clientName;
|
68 | }
|
69 |
|
70 | module.exports = {
|
71 | addQueryContext,
|
72 | containsUndefined,
|
73 | normalizeArr,
|
74 | resolveClientNameWithAliases,
|
75 | };
|