1 | 'use strict';
|
2 |
|
3 | const DataTypes = require('./data-types');
|
4 | const SqlString = require('./sql-string');
|
5 | const _ = require('lodash');
|
6 | const uuidv1 = require('uuid').v1;
|
7 | const uuidv4 = require('uuid').v4;
|
8 | const operators = require('./operators');
|
9 | const operatorsSet = new Set(Object.values(operators));
|
10 |
|
11 | let inflection = require('inflection');
|
12 |
|
13 | exports.classToInvokable = require('./utils/class-to-invokable').classToInvokable;
|
14 | exports.joinSQLFragments = require('./utils/join-sql-fragments').joinSQLFragments;
|
15 |
|
16 | function useInflection(_inflection) {
|
17 | inflection = _inflection;
|
18 | }
|
19 | exports.useInflection = useInflection;
|
20 |
|
21 | function camelizeIf(str, condition) {
|
22 | let result = str;
|
23 |
|
24 | if (condition) {
|
25 | result = camelize(str);
|
26 | }
|
27 |
|
28 | return result;
|
29 | }
|
30 | exports.camelizeIf = camelizeIf;
|
31 |
|
32 | function underscoredIf(str, condition) {
|
33 | let result = str;
|
34 |
|
35 | if (condition) {
|
36 | result = underscore(str);
|
37 | }
|
38 |
|
39 | return result;
|
40 | }
|
41 | exports.underscoredIf = underscoredIf;
|
42 |
|
43 | function isPrimitive(val) {
|
44 | const type = typeof val;
|
45 | return type === 'string' || type === 'number' || type === 'boolean';
|
46 | }
|
47 | exports.isPrimitive = isPrimitive;
|
48 |
|
49 |
|
50 | function mergeDefaults(a, b) {
|
51 | return _.mergeWith(a, b, (objectValue, sourceValue) => {
|
52 |
|
53 | if (!_.isPlainObject(objectValue) && objectValue !== undefined) {
|
54 | if (_.isFunction(objectValue) && _.isNative(objectValue)) {
|
55 | return sourceValue || objectValue;
|
56 | }
|
57 | return objectValue;
|
58 | }
|
59 | });
|
60 | }
|
61 | exports.mergeDefaults = mergeDefaults;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function merge() {
|
67 | const result = {};
|
68 |
|
69 | for (const obj of arguments) {
|
70 | _.forOwn(obj, (value, key) => {
|
71 | if (value !== undefined) {
|
72 | if (!result[key]) {
|
73 | result[key] = value;
|
74 | } else if (_.isPlainObject(value) && _.isPlainObject(result[key])) {
|
75 | result[key] = merge(result[key], value);
|
76 | } else if (Array.isArray(value) && Array.isArray(result[key])) {
|
77 | result[key] = value.concat(result[key]);
|
78 | } else {
|
79 | result[key] = value;
|
80 | }
|
81 | }
|
82 | });
|
83 | }
|
84 |
|
85 | return result;
|
86 | }
|
87 | exports.merge = merge;
|
88 |
|
89 | function spliceStr(str, index, count, add) {
|
90 | return str.slice(0, index) + add + str.slice(index + count);
|
91 | }
|
92 | exports.spliceStr = spliceStr;
|
93 |
|
94 | function camelize(str) {
|
95 | return str.trim().replace(/[-_\s]+(.)?/g, (match, c) => c.toUpperCase());
|
96 | }
|
97 | exports.camelize = camelize;
|
98 |
|
99 | function underscore(str) {
|
100 | return inflection.underscore(str);
|
101 | }
|
102 | exports.underscore = underscore;
|
103 |
|
104 | function singularize(str) {
|
105 | return inflection.singularize(str);
|
106 | }
|
107 | exports.singularize = singularize;
|
108 |
|
109 | function pluralize(str) {
|
110 | return inflection.pluralize(str);
|
111 | }
|
112 | exports.pluralize = pluralize;
|
113 |
|
114 | function format(arr, dialect) {
|
115 | const timeZone = null;
|
116 |
|
117 | return SqlString.format(arr[0], arr.slice(1), timeZone, dialect);
|
118 | }
|
119 | exports.format = format;
|
120 |
|
121 | function formatNamedParameters(sql, parameters, dialect) {
|
122 | const timeZone = null;
|
123 | return SqlString.formatNamedParameters(sql, parameters, timeZone, dialect);
|
124 | }
|
125 | exports.formatNamedParameters = formatNamedParameters;
|
126 |
|
127 | function cloneDeep(obj, onlyPlain) {
|
128 | obj = obj || {};
|
129 | return _.cloneDeepWith(obj, elem => {
|
130 |
|
131 | if (Array.isArray(elem) || _.isPlainObject(elem)) {
|
132 | return undefined;
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 | if (onlyPlain || typeof elem === 'object') {
|
138 | return elem;
|
139 | }
|
140 |
|
141 |
|
142 | if (elem && typeof elem.clone === 'function') {
|
143 | return elem.clone();
|
144 | }
|
145 | });
|
146 | }
|
147 | exports.cloneDeep = cloneDeep;
|
148 |
|
149 |
|
150 | function mapFinderOptions(options, Model) {
|
151 | if (options.attributes && Array.isArray(options.attributes)) {
|
152 | options.attributes = Model._injectDependentVirtualAttributes(options.attributes);
|
153 | options.attributes = options.attributes.filter(v => !Model._virtualAttributes.has(v));
|
154 | }
|
155 |
|
156 | mapOptionFieldNames(options, Model);
|
157 |
|
158 | return options;
|
159 | }
|
160 | exports.mapFinderOptions = mapFinderOptions;
|
161 |
|
162 |
|
163 | function mapOptionFieldNames(options, Model) {
|
164 | if (Array.isArray(options.attributes)) {
|
165 | options.attributes = options.attributes.map(attr => {
|
166 |
|
167 | if (typeof attr !== 'string') return attr;
|
168 |
|
169 | if (Model.rawAttributes[attr] && attr !== Model.rawAttributes[attr].field) {
|
170 | return [Model.rawAttributes[attr].field, attr];
|
171 | }
|
172 | return attr;
|
173 | });
|
174 | }
|
175 |
|
176 | if (options.where && _.isPlainObject(options.where)) {
|
177 | options.where = mapWhereFieldNames(options.where, Model);
|
178 | }
|
179 |
|
180 | return options;
|
181 | }
|
182 | exports.mapOptionFieldNames = mapOptionFieldNames;
|
183 |
|
184 | function mapWhereFieldNames(attributes, Model) {
|
185 | if (attributes) {
|
186 | getComplexKeys(attributes).forEach(attribute => {
|
187 | const rawAttribute = Model.rawAttributes[attribute];
|
188 |
|
189 | if (rawAttribute && rawAttribute.field !== rawAttribute.fieldName) {
|
190 | attributes[rawAttribute.field] = attributes[attribute];
|
191 | delete attributes[attribute];
|
192 | }
|
193 |
|
194 | if (_.isPlainObject(attributes[attribute])
|
195 | && !(rawAttribute && (
|
196 | rawAttribute.type instanceof DataTypes.HSTORE
|
197 | || rawAttribute.type instanceof DataTypes.JSON))) {
|
198 | attributes[attribute] = mapOptionFieldNames({
|
199 | where: attributes[attribute]
|
200 | }, Model).where;
|
201 | }
|
202 |
|
203 | if (Array.isArray(attributes[attribute])) {
|
204 | attributes[attribute].forEach((where, index) => {
|
205 | if (_.isPlainObject(where)) {
|
206 | attributes[attribute][index] = mapWhereFieldNames(where, Model);
|
207 | }
|
208 | });
|
209 | }
|
210 |
|
211 | });
|
212 | }
|
213 |
|
214 | return attributes;
|
215 | }
|
216 | exports.mapWhereFieldNames = mapWhereFieldNames;
|
217 |
|
218 |
|
219 | function mapValueFieldNames(dataValues, fields, Model) {
|
220 | const values = {};
|
221 |
|
222 | for (const attr of fields) {
|
223 | if (dataValues[attr] !== undefined && !Model._virtualAttributes.has(attr)) {
|
224 |
|
225 | if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field && Model.rawAttributes[attr].field !== attr) {
|
226 | values[Model.rawAttributes[attr].field] = dataValues[attr];
|
227 | } else {
|
228 | values[attr] = dataValues[attr];
|
229 | }
|
230 | }
|
231 | }
|
232 |
|
233 | return values;
|
234 | }
|
235 | exports.mapValueFieldNames = mapValueFieldNames;
|
236 |
|
237 | function isColString(value) {
|
238 | return typeof value === 'string' && value[0] === '$' && value[value.length - 1] === '$';
|
239 | }
|
240 | exports.isColString = isColString;
|
241 |
|
242 | function canTreatArrayAsAnd(arr) {
|
243 | return arr.some(arg => _.isPlainObject(arg) || arg instanceof Where);
|
244 | }
|
245 | exports.canTreatArrayAsAnd = canTreatArrayAsAnd;
|
246 |
|
247 | function combineTableNames(tableName1, tableName2) {
|
248 | return tableName1.toLowerCase() < tableName2.toLowerCase() ? tableName1 + tableName2 : tableName2 + tableName1;
|
249 | }
|
250 | exports.combineTableNames = combineTableNames;
|
251 |
|
252 | function toDefaultValue(value, dialect) {
|
253 | if (typeof value === 'function') {
|
254 | const tmp = value();
|
255 | if (tmp instanceof DataTypes.ABSTRACT) {
|
256 | return tmp.toSql();
|
257 | }
|
258 | return tmp;
|
259 | }
|
260 | if (value instanceof DataTypes.UUIDV1) {
|
261 | return uuidv1();
|
262 | }
|
263 | if (value instanceof DataTypes.UUIDV4) {
|
264 | return uuidv4();
|
265 | }
|
266 | if (value instanceof DataTypes.NOW) {
|
267 | return now(dialect);
|
268 | }
|
269 | if (Array.isArray(value)) {
|
270 | return value.slice();
|
271 | }
|
272 | if (_.isPlainObject(value)) {
|
273 | return { ...value };
|
274 | }
|
275 | return value;
|
276 | }
|
277 | exports.toDefaultValue = toDefaultValue;
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | function defaultValueSchemable(value) {
|
288 | if (value === undefined) { return false; }
|
289 |
|
290 |
|
291 |
|
292 | if (value instanceof DataTypes.NOW) { return false; }
|
293 |
|
294 | if (value instanceof DataTypes.UUIDV1 || value instanceof DataTypes.UUIDV4) { return false; }
|
295 |
|
296 | return typeof value !== 'function';
|
297 | }
|
298 | exports.defaultValueSchemable = defaultValueSchemable;
|
299 |
|
300 | function removeNullValuesFromHash(hash, omitNull, options) {
|
301 | let result = hash;
|
302 |
|
303 | options = options || {};
|
304 | options.allowNull = options.allowNull || [];
|
305 |
|
306 | if (omitNull) {
|
307 | const _hash = {};
|
308 |
|
309 | _.forIn(hash, (val, key) => {
|
310 | if (options.allowNull.includes(key) || key.endsWith('Id') || val !== null && val !== undefined) {
|
311 | _hash[key] = val;
|
312 | }
|
313 | });
|
314 |
|
315 | result = _hash;
|
316 | }
|
317 |
|
318 | return result;
|
319 | }
|
320 | exports.removeNullValuesFromHash = removeNullValuesFromHash;
|
321 |
|
322 | const dialects = new Set(['mariadb', 'mysql', 'postgres', 'sqlite', 'mssql']);
|
323 |
|
324 | function now(dialect) {
|
325 | const d = new Date();
|
326 | if (!dialects.has(dialect)) {
|
327 | d.setMilliseconds(0);
|
328 | }
|
329 | return d;
|
330 | }
|
331 | exports.now = now;
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | const TICK_CHAR = '`';
|
337 | exports.TICK_CHAR = TICK_CHAR;
|
338 |
|
339 | function addTicks(s, tickChar) {
|
340 | tickChar = tickChar || TICK_CHAR;
|
341 | return tickChar + removeTicks(s, tickChar) + tickChar;
|
342 | }
|
343 | exports.addTicks = addTicks;
|
344 |
|
345 | function removeTicks(s, tickChar) {
|
346 | tickChar = tickChar || TICK_CHAR;
|
347 | return s.replace(new RegExp(tickChar, 'g'), '');
|
348 | }
|
349 | exports.removeTicks = removeTicks;
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 | function flattenObjectDeep(value) {
|
381 | if (!_.isPlainObject(value)) return value;
|
382 | const flattenedObj = {};
|
383 |
|
384 | function flattenObject(obj, subPath) {
|
385 | Object.keys(obj).forEach(key => {
|
386 | const pathToProperty = subPath ? `${subPath}.${key}` : key;
|
387 | if (typeof obj[key] === 'object' && obj[key] !== null) {
|
388 | flattenObject(obj[key], pathToProperty);
|
389 | } else {
|
390 | flattenedObj[pathToProperty] = _.get(obj, key);
|
391 | }
|
392 | });
|
393 | return flattenedObj;
|
394 | }
|
395 |
|
396 | return flattenObject(value, undefined);
|
397 | }
|
398 | exports.flattenObjectDeep = flattenObjectDeep;
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 | class SequelizeMethod {}
|
407 | exports.SequelizeMethod = SequelizeMethod;
|
408 |
|
409 | class Fn extends SequelizeMethod {
|
410 | constructor(fn, args) {
|
411 | super();
|
412 | this.fn = fn;
|
413 | this.args = args;
|
414 | }
|
415 | clone() {
|
416 | return new Fn(this.fn, this.args);
|
417 | }
|
418 | }
|
419 | exports.Fn = Fn;
|
420 |
|
421 | class Col extends SequelizeMethod {
|
422 | constructor(col, ...args) {
|
423 | super();
|
424 | if (args.length > 0) {
|
425 | col = args;
|
426 | }
|
427 | this.col = col;
|
428 | }
|
429 | }
|
430 | exports.Col = Col;
|
431 |
|
432 | class Cast extends SequelizeMethod {
|
433 | constructor(val, type, json) {
|
434 | super();
|
435 | this.val = val;
|
436 | this.type = (type || '').trim();
|
437 | this.json = json || false;
|
438 | }
|
439 | }
|
440 | exports.Cast = Cast;
|
441 |
|
442 | class Literal extends SequelizeMethod {
|
443 | constructor(val) {
|
444 | super();
|
445 | this.val = val;
|
446 | }
|
447 | }
|
448 | exports.Literal = Literal;
|
449 |
|
450 | class Json extends SequelizeMethod {
|
451 | constructor(conditionsOrPath, value) {
|
452 | super();
|
453 | if (_.isObject(conditionsOrPath)) {
|
454 | this.conditions = conditionsOrPath;
|
455 | } else {
|
456 | this.path = conditionsOrPath;
|
457 | if (value) {
|
458 | this.value = value;
|
459 | }
|
460 | }
|
461 | }
|
462 | }
|
463 | exports.Json = Json;
|
464 |
|
465 | class Where extends SequelizeMethod {
|
466 | constructor(attribute, comparator, logic) {
|
467 | super();
|
468 | if (logic === undefined) {
|
469 | logic = comparator;
|
470 | comparator = '=';
|
471 | }
|
472 |
|
473 | this.attribute = attribute;
|
474 | this.comparator = comparator;
|
475 | this.logic = logic;
|
476 | }
|
477 | }
|
478 | exports.Where = Where;
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 | function getOperators(obj) {
|
490 | return Object.getOwnPropertySymbols(obj).filter(s => operatorsSet.has(s));
|
491 | }
|
492 | exports.getOperators = getOperators;
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 | function getComplexKeys(obj) {
|
502 | return getOperators(obj).concat(Object.keys(obj));
|
503 | }
|
504 | exports.getComplexKeys = getComplexKeys;
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 | function getComplexSize(obj) {
|
514 | return Array.isArray(obj) ? obj.length : getComplexKeys(obj).length;
|
515 | }
|
516 | exports.getComplexSize = getComplexSize;
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 | function isWhereEmpty(obj) {
|
526 | return !!obj && _.isEmpty(obj) && getOperators(obj).length === 0;
|
527 | }
|
528 | exports.isWhereEmpty = isWhereEmpty;
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 | function generateEnumName(tableName, columnName) {
|
539 | return `enum_${tableName}_${columnName}`;
|
540 | }
|
541 | exports.generateEnumName = generateEnumName;
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 | function camelizeObjectKeys(obj) {
|
551 | const newObj = new Object();
|
552 | Object.keys(obj).forEach(key => {
|
553 | newObj[camelize(key)] = obj[key];
|
554 | });
|
555 | return newObj;
|
556 | }
|
557 | exports.camelizeObjectKeys = camelizeObjectKeys;
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 | function defaults(object, ...sources) {
|
573 | object = Object(object);
|
574 |
|
575 | sources.forEach(source => {
|
576 | if (source) {
|
577 | source = Object(source);
|
578 |
|
579 | getComplexKeys(source).forEach(key => {
|
580 | const value = object[key];
|
581 | if (
|
582 | value === undefined ||
|
583 | _.eq(value, Object.prototype[key]) &&
|
584 | !Object.prototype.hasOwnProperty.call(object, key)
|
585 |
|
586 | ) {
|
587 | object[key] = source[key];
|
588 | }
|
589 | });
|
590 | }
|
591 | });
|
592 |
|
593 | return object;
|
594 | }
|
595 | exports.defaults = defaults;
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 |
|
604 |
|
605 |
|
606 |
|
607 | function nameIndex(index, tableName) {
|
608 | if (tableName.tableName) tableName = tableName.tableName;
|
609 |
|
610 | if (!Object.prototype.hasOwnProperty.call(index, 'name')) {
|
611 | const fields = index.fields.map(
|
612 | field => typeof field === 'string' ? field : field.name || field.attribute
|
613 | );
|
614 | index.name = underscore(`${tableName}_${fields.join('_')}`);
|
615 | }
|
616 |
|
617 | return index;
|
618 | }
|
619 | exports.nameIndex = nameIndex;
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 | function intersects(arr1, arr2) {
|
629 | return arr1.some(v => arr2.includes(v));
|
630 | }
|
631 | exports.intersects = intersects;
|