1 | 'use strict';
|
2 |
|
3 | const util = require('util');
|
4 | const _ = require('lodash');
|
5 | const wkx = require('wkx');
|
6 | const sequelizeErrors = require('./errors');
|
7 | const Validator = require('./utils/validator-extras').validator;
|
8 | const momentTz = require('moment-timezone');
|
9 | const moment = require('moment');
|
10 | const { logger } = require('./utils/logger');
|
11 | const warnings = {};
|
12 | const { classToInvokable } = require('./utils/class-to-invokable');
|
13 | const { joinSQLFragments } = require('./utils/join-sql-fragments');
|
14 |
|
15 | class ABSTRACT {
|
16 | toString(options) {
|
17 | return this.toSql(options);
|
18 | }
|
19 | toSql() {
|
20 | return this.key;
|
21 | }
|
22 | stringify(value, options) {
|
23 | if (this._stringify) {
|
24 | return this._stringify(value, options);
|
25 | }
|
26 | return value;
|
27 | }
|
28 | bindParam(value, options) {
|
29 | if (this._bindParam) {
|
30 | return this._bindParam(value, options);
|
31 | }
|
32 | return options.bindParam(this.stringify(value, options));
|
33 | }
|
34 | static toString() {
|
35 | return this.name;
|
36 | }
|
37 | static warn(link, text) {
|
38 | if (!warnings[text]) {
|
39 | warnings[text] = true;
|
40 | logger.warn(`${text} \n>> Check: ${link}`);
|
41 | }
|
42 | }
|
43 | static extend(oldType) {
|
44 | return new this(oldType.options);
|
45 | }
|
46 | }
|
47 |
|
48 | ABSTRACT.prototype.dialectTypes = '';
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | class STRING extends ABSTRACT {
|
54 | |
55 |
|
56 |
|
57 |
|
58 | constructor(length, binary) {
|
59 | super();
|
60 | const options = typeof length === 'object' && length || { length, binary };
|
61 | this.options = options;
|
62 | this._binary = options.binary;
|
63 | this._length = options.length || 255;
|
64 | }
|
65 | toSql() {
|
66 | return joinSQLFragments([
|
67 | `VARCHAR(${this._length})`,
|
68 | this._binary && 'BINARY'
|
69 | ]);
|
70 | }
|
71 | validate(value) {
|
72 | if (Object.prototype.toString.call(value) !== '[object String]') {
|
73 | if (this.options.binary && Buffer.isBuffer(value) || typeof value === 'number') {
|
74 | return true;
|
75 | }
|
76 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid string', value));
|
77 | }
|
78 | return true;
|
79 | }
|
80 |
|
81 | get BINARY() {
|
82 | this._binary = true;
|
83 | this.options.binary = true;
|
84 | return this;
|
85 | }
|
86 |
|
87 | static get BINARY() {
|
88 | return new this().BINARY;
|
89 | }
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | class CHAR extends STRING {
|
96 | |
97 |
|
98 |
|
99 |
|
100 | constructor(length, binary) {
|
101 | super(typeof length === 'object' && length || { length, binary });
|
102 | }
|
103 | toSql() {
|
104 | return joinSQLFragments([
|
105 | `CHAR(${this._length})`,
|
106 | this._binary && 'BINARY'
|
107 | ]);
|
108 | }
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | class TEXT extends ABSTRACT {
|
115 | |
116 |
|
117 |
|
118 | constructor(length) {
|
119 | super();
|
120 | const options = typeof length === 'object' && length || { length };
|
121 | this.options = options;
|
122 | this._length = options.length || '';
|
123 | }
|
124 | toSql() {
|
125 | switch (this._length.toLowerCase()) {
|
126 | case 'tiny':
|
127 | return 'TINYTEXT';
|
128 | case 'medium':
|
129 | return 'MEDIUMTEXT';
|
130 | case 'long':
|
131 | return 'LONGTEXT';
|
132 | default:
|
133 | return this.key;
|
134 | }
|
135 | }
|
136 | validate(value) {
|
137 | if (typeof value !== 'string') {
|
138 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid string', value));
|
139 | }
|
140 | return true;
|
141 | }
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | class CITEXT extends ABSTRACT {
|
151 | toSql() {
|
152 | return 'CITEXT';
|
153 | }
|
154 | validate(value) {
|
155 | if (typeof value !== 'string') {
|
156 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid string', value));
|
157 | }
|
158 | return true;
|
159 | }
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | class NUMBER extends ABSTRACT {
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | constructor(options = {}) {
|
176 | super();
|
177 | if (typeof options === 'number') {
|
178 | options = {
|
179 | length: options
|
180 | };
|
181 | }
|
182 | this.options = options;
|
183 | this._length = options.length;
|
184 | this._zerofill = options.zerofill;
|
185 | this._decimals = options.decimals;
|
186 | this._precision = options.precision;
|
187 | this._scale = options.scale;
|
188 | this._unsigned = options.unsigned;
|
189 | }
|
190 | toSql() {
|
191 | let result = this.key;
|
192 | if (this._length) {
|
193 | result += `(${this._length}`;
|
194 | if (typeof this._decimals === 'number') {
|
195 | result += `,${this._decimals}`;
|
196 | }
|
197 | result += ')';
|
198 | }
|
199 | if (this._unsigned) {
|
200 | result += ' UNSIGNED';
|
201 | }
|
202 | if (this._zerofill) {
|
203 | result += ' ZEROFILL';
|
204 | }
|
205 | return result;
|
206 | }
|
207 | validate(value) {
|
208 | if (!Validator.isFloat(String(value))) {
|
209 | throw new sequelizeErrors.ValidationError(util.format(`%j is not a valid ${this.key.toLowerCase()}`, value));
|
210 | }
|
211 | return true;
|
212 | }
|
213 | _stringify(number) {
|
214 | if (typeof number === 'number' || typeof number === 'boolean' || number === null || number === undefined) {
|
215 | return number;
|
216 | }
|
217 | if (typeof number.toString === 'function') {
|
218 | return number.toString();
|
219 | }
|
220 | return number;
|
221 | }
|
222 |
|
223 | get UNSIGNED() {
|
224 | this._unsigned = true;
|
225 | this.options.unsigned = true;
|
226 | return this;
|
227 | }
|
228 |
|
229 | get ZEROFILL() {
|
230 | this._zerofill = true;
|
231 | this.options.zerofill = true;
|
232 | return this;
|
233 | }
|
234 |
|
235 | static get UNSIGNED() {
|
236 | return new this().UNSIGNED;
|
237 | }
|
238 |
|
239 | static get ZEROFILL() {
|
240 | return new this().ZEROFILL;
|
241 | }
|
242 | }
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | class INTEGER extends NUMBER {
|
248 | validate(value) {
|
249 | if (!Validator.isInt(String(value))) {
|
250 | throw new sequelizeErrors.ValidationError(util.format(`%j is not a valid ${this.key.toLowerCase()}`, value));
|
251 | }
|
252 | return true;
|
253 | }
|
254 | }
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | class TINYINT extends INTEGER {
|
260 | }
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | class SMALLINT extends INTEGER {
|
266 | }
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | class MEDIUMINT extends INTEGER {
|
272 | }
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | class BIGINT extends INTEGER {
|
278 | }
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | class FLOAT extends NUMBER {
|
284 | |
285 |
|
286 |
|
287 |
|
288 | constructor(length, decimals) {
|
289 | super(typeof length === 'object' && length || { length, decimals });
|
290 | }
|
291 | validate(value) {
|
292 | if (!Validator.isFloat(String(value))) {
|
293 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid float', value));
|
294 | }
|
295 | return true;
|
296 | }
|
297 | }
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | class REAL extends NUMBER {
|
303 | |
304 |
|
305 |
|
306 |
|
307 | constructor(length, decimals) {
|
308 | super(typeof length === 'object' && length || { length, decimals });
|
309 | }
|
310 | }
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | class DOUBLE extends NUMBER {
|
316 | |
317 |
|
318 |
|
319 |
|
320 | constructor(length, decimals) {
|
321 | super(typeof length === 'object' && length || { length, decimals });
|
322 | }
|
323 | }
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | class DECIMAL extends NUMBER {
|
329 | |
330 |
|
331 |
|
332 |
|
333 | constructor(precision, scale) {
|
334 | super(typeof precision === 'object' && precision || { precision, scale });
|
335 | }
|
336 | toSql() {
|
337 | if (this._precision || this._scale) {
|
338 | return `DECIMAL(${[this._precision, this._scale].filter(_.identity).join(',')})`;
|
339 | }
|
340 | return 'DECIMAL';
|
341 | }
|
342 | validate(value) {
|
343 | if (!Validator.isDecimal(String(value))) {
|
344 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid decimal', value));
|
345 | }
|
346 | return true;
|
347 | }
|
348 | }
|
349 |
|
350 |
|
351 | const protoExtensions = {
|
352 | escape: false,
|
353 | _value(value) {
|
354 | if (isNaN(value)) {
|
355 | return 'NaN';
|
356 | }
|
357 | if (!isFinite(value)) {
|
358 | const sign = value < 0 ? '-' : '';
|
359 | return `${sign}Infinity`;
|
360 | }
|
361 |
|
362 | return value;
|
363 | },
|
364 | _stringify(value) {
|
365 | return `'${this._value(value)}'`;
|
366 | },
|
367 | _bindParam(value, options) {
|
368 | return options.bindParam(this._value(value));
|
369 | }
|
370 | };
|
371 |
|
372 | for (const floating of [FLOAT, DOUBLE, REAL]) {
|
373 | Object.assign(floating.prototype, protoExtensions);
|
374 | }
|
375 |
|
376 |
|
377 |
|
378 |
|
379 | class BOOLEAN extends ABSTRACT {
|
380 | toSql() {
|
381 | return 'TINYINT(1)';
|
382 | }
|
383 | validate(value) {
|
384 | if (!Validator.isBoolean(String(value))) {
|
385 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid boolean', value));
|
386 | }
|
387 | return true;
|
388 | }
|
389 | _sanitize(value) {
|
390 | if (value !== null && value !== undefined) {
|
391 | if (Buffer.isBuffer(value) && value.length === 1) {
|
392 |
|
393 | value = value[0];
|
394 | }
|
395 | const type = typeof value;
|
396 | if (type === 'string') {
|
397 |
|
398 | return value === 'true' ? true : value === 'false' ? false : value;
|
399 | }
|
400 | if (type === 'number') {
|
401 |
|
402 | return value === 1 ? true : value === 0 ? false : value;
|
403 | }
|
404 | }
|
405 | return value;
|
406 | }
|
407 | }
|
408 |
|
409 |
|
410 | BOOLEAN.parse = BOOLEAN.prototype._sanitize;
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 | class TIME extends ABSTRACT {
|
417 | toSql() {
|
418 | return 'TIME';
|
419 | }
|
420 | }
|
421 |
|
422 |
|
423 |
|
424 |
|
425 | class DATE extends ABSTRACT {
|
426 | |
427 |
|
428 |
|
429 | constructor(length) {
|
430 | super();
|
431 | const options = typeof length === 'object' && length || { length };
|
432 | this.options = options;
|
433 | this._length = options.length || '';
|
434 | }
|
435 | toSql() {
|
436 | return 'DATETIME';
|
437 | }
|
438 | validate(value) {
|
439 | if (!Validator.isDate(String(value))) {
|
440 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid date', value));
|
441 | }
|
442 | return true;
|
443 | }
|
444 | _sanitize(value, options) {
|
445 | if ((!options || options && !options.raw) && !(value instanceof Date) && !!value) {
|
446 | return new Date(value);
|
447 | }
|
448 | return value;
|
449 | }
|
450 | _isChanged(value, originalValue) {
|
451 | if (originalValue && !!value &&
|
452 | (value === originalValue ||
|
453 | value instanceof Date && originalValue instanceof Date && value.getTime() === originalValue.getTime())) {
|
454 | return false;
|
455 | }
|
456 |
|
457 | if (!originalValue && !value && originalValue === value) {
|
458 | return false;
|
459 | }
|
460 | return true;
|
461 | }
|
462 | _applyTimezone(date, options) {
|
463 | if (options.timezone) {
|
464 | if (momentTz.tz.zone(options.timezone)) {
|
465 | return momentTz(date).tz(options.timezone);
|
466 | }
|
467 | return date = moment(date).utcOffset(options.timezone);
|
468 | }
|
469 | return momentTz(date);
|
470 | }
|
471 | _stringify(date, options) {
|
472 | date = this._applyTimezone(date, options);
|
473 |
|
474 | return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
|
475 | }
|
476 | }
|
477 |
|
478 |
|
479 |
|
480 |
|
481 | class DATEONLY extends ABSTRACT {
|
482 | toSql() {
|
483 | return 'DATE';
|
484 | }
|
485 | _stringify(date) {
|
486 | return moment(date).format('YYYY-MM-DD');
|
487 | }
|
488 | _sanitize(value, options) {
|
489 | if ((!options || options && !options.raw) && !!value) {
|
490 | return moment(value).format('YYYY-MM-DD');
|
491 | }
|
492 | return value;
|
493 | }
|
494 | _isChanged(value, originalValue) {
|
495 | if (originalValue && !!value && originalValue === value) {
|
496 | return false;
|
497 | }
|
498 |
|
499 | if (!originalValue && !value && originalValue === value) {
|
500 | return false;
|
501 | }
|
502 | return true;
|
503 | }
|
504 | }
|
505 |
|
506 |
|
507 |
|
508 |
|
509 | class HSTORE extends ABSTRACT {
|
510 | validate(value) {
|
511 | if (!_.isPlainObject(value)) {
|
512 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid hstore', value));
|
513 | }
|
514 | return true;
|
515 | }
|
516 | }
|
517 |
|
518 |
|
519 |
|
520 |
|
521 | class JSONTYPE extends ABSTRACT {
|
522 | validate() {
|
523 | return true;
|
524 | }
|
525 | _stringify(value) {
|
526 | return JSON.stringify(value);
|
527 | }
|
528 | }
|
529 |
|
530 |
|
531 |
|
532 |
|
533 | class JSONB extends JSONTYPE {
|
534 | }
|
535 |
|
536 |
|
537 |
|
538 |
|
539 | class NOW extends ABSTRACT {
|
540 | }
|
541 |
|
542 |
|
543 |
|
544 |
|
545 | class BLOB extends ABSTRACT {
|
546 | |
547 |
|
548 |
|
549 | constructor(length) {
|
550 | super();
|
551 | const options = typeof length === 'object' && length || { length };
|
552 | this.options = options;
|
553 | this._length = options.length || '';
|
554 | }
|
555 | toSql() {
|
556 | switch (this._length.toLowerCase()) {
|
557 | case 'tiny':
|
558 | return 'TINYBLOB';
|
559 | case 'medium':
|
560 | return 'MEDIUMBLOB';
|
561 | case 'long':
|
562 | return 'LONGBLOB';
|
563 | default:
|
564 | return this.key;
|
565 | }
|
566 | }
|
567 | validate(value) {
|
568 | if (typeof value !== 'string' && !Buffer.isBuffer(value)) {
|
569 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid blob', value));
|
570 | }
|
571 | return true;
|
572 | }
|
573 | _stringify(value) {
|
574 | if (!Buffer.isBuffer(value)) {
|
575 | if (Array.isArray(value)) {
|
576 | value = Buffer.from(value);
|
577 | }
|
578 | else {
|
579 | value = Buffer.from(value.toString());
|
580 | }
|
581 | }
|
582 | const hex = value.toString('hex');
|
583 | return this._hexify(hex);
|
584 | }
|
585 | _hexify(hex) {
|
586 | return `X'${hex}'`;
|
587 | }
|
588 | _bindParam(value, options) {
|
589 | if (!Buffer.isBuffer(value)) {
|
590 | if (Array.isArray(value)) {
|
591 | value = Buffer.from(value);
|
592 | }
|
593 | else {
|
594 | value = Buffer.from(value.toString());
|
595 | }
|
596 | }
|
597 | return options.bindParam(value);
|
598 | }
|
599 | }
|
600 |
|
601 |
|
602 | BLOB.prototype.escape = false;
|
603 |
|
604 |
|
605 |
|
606 |
|
607 |
|
608 | class RANGE extends ABSTRACT {
|
609 | |
610 |
|
611 |
|
612 | constructor(subtype) {
|
613 | super();
|
614 | const options = _.isPlainObject(subtype) ? subtype : { subtype };
|
615 | if (!options.subtype)
|
616 | options.subtype = new INTEGER();
|
617 | if (typeof options.subtype === 'function') {
|
618 | options.subtype = new options.subtype();
|
619 | }
|
620 | this._subtype = options.subtype.key;
|
621 | this.options = options;
|
622 | }
|
623 | validate(value) {
|
624 | if (!Array.isArray(value)) {
|
625 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid range', value));
|
626 | }
|
627 | if (value.length !== 2) {
|
628 | throw new sequelizeErrors.ValidationError('A range must be an array with two elements');
|
629 | }
|
630 | return true;
|
631 | }
|
632 | }
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 | class UUID extends ABSTRACT {
|
639 | validate(value, options) {
|
640 | if (typeof value !== 'string' || !Validator.isUUID(value) && (!options || !options.acceptStrings)) {
|
641 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid uuid', value));
|
642 | }
|
643 | return true;
|
644 | }
|
645 | }
|
646 |
|
647 |
|
648 |
|
649 |
|
650 | class UUIDV1 extends ABSTRACT {
|
651 | validate(value, options) {
|
652 | if (typeof value !== 'string' || !Validator.isUUID(value) && (!options || !options.acceptStrings)) {
|
653 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid uuid', value));
|
654 | }
|
655 | return true;
|
656 | }
|
657 | }
|
658 |
|
659 |
|
660 |
|
661 |
|
662 | class UUIDV4 extends ABSTRACT {
|
663 | validate(value, options) {
|
664 | if (typeof value !== 'string' || !Validator.isUUID(value, 4) && (!options || !options.acceptStrings)) {
|
665 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid uuidv4', value));
|
666 | }
|
667 | return true;
|
668 | }
|
669 | }
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 | class VIRTUAL extends ABSTRACT {
|
712 | |
713 |
|
714 |
|
715 |
|
716 | constructor(ReturnType, fields) {
|
717 | super();
|
718 | if (typeof ReturnType === 'function')
|
719 | ReturnType = new ReturnType();
|
720 | this.returnType = ReturnType;
|
721 | this.fields = fields;
|
722 | }
|
723 | }
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 | class ENUM extends ABSTRACT {
|
736 | |
737 |
|
738 |
|
739 | constructor(...args) {
|
740 | super();
|
741 | const value = args[0];
|
742 | const options = typeof value === 'object' && !Array.isArray(value) && value || {
|
743 | values: args.reduce((result, element) => {
|
744 | return result.concat(Array.isArray(element) ? element : [element]);
|
745 | }, [])
|
746 | };
|
747 | this.values = options.values;
|
748 | this.options = options;
|
749 | }
|
750 | validate(value) {
|
751 | if (!this.values.includes(value)) {
|
752 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid choice in %j', value, this.values));
|
753 | }
|
754 | return true;
|
755 | }
|
756 | }
|
757 |
|
758 |
|
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 | class ARRAY extends ABSTRACT {
|
765 | |
766 |
|
767 |
|
768 | constructor(type) {
|
769 | super();
|
770 | const options = _.isPlainObject(type) ? type : { type };
|
771 | this.options = options;
|
772 | this.type = typeof options.type === 'function' ? new options.type() : options.type;
|
773 | }
|
774 | toSql() {
|
775 | return `${this.type.toSql()}[]`;
|
776 | }
|
777 | validate(value) {
|
778 | if (!Array.isArray(value)) {
|
779 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid array', value));
|
780 | }
|
781 | return true;
|
782 | }
|
783 | static is(obj, type) {
|
784 | return obj instanceof ARRAY && obj.type instanceof type;
|
785 | }
|
786 | }
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 |
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 | class GEOMETRY extends ABSTRACT {
|
835 | |
836 |
|
837 |
|
838 |
|
839 | constructor(type, srid) {
|
840 | super();
|
841 | const options = _.isPlainObject(type) ? type : { type, srid };
|
842 | this.options = options;
|
843 | this.type = options.type;
|
844 | this.srid = options.srid;
|
845 | }
|
846 | _stringify(value, options) {
|
847 | return `GeomFromText(${options.escape(wkx.Geometry.parseGeoJSON(value).toWkt())})`;
|
848 | }
|
849 | _bindParam(value, options) {
|
850 | return `GeomFromText(${options.bindParam(wkx.Geometry.parseGeoJSON(value).toWkt())})`;
|
851 | }
|
852 | }
|
853 |
|
854 | GEOMETRY.prototype.escape = false;
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 | class GEOGRAPHY extends ABSTRACT {
|
878 | |
879 |
|
880 |
|
881 |
|
882 | constructor(type, srid) {
|
883 | super();
|
884 | const options = _.isPlainObject(type) ? type : { type, srid };
|
885 | this.options = options;
|
886 | this.type = options.type;
|
887 | this.srid = options.srid;
|
888 | }
|
889 | _stringify(value, options) {
|
890 | return `GeomFromText(${options.escape(wkx.Geometry.parseGeoJSON(value).toWkt())})`;
|
891 | }
|
892 | _bindParam(value, options) {
|
893 | return `GeomFromText(${options.bindParam(wkx.Geometry.parseGeoJSON(value).toWkt())})`;
|
894 | }
|
895 | }
|
896 |
|
897 |
|
898 | GEOGRAPHY.prototype.escape = false;
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 | class CIDR extends ABSTRACT {
|
906 | validate(value) {
|
907 | if (typeof value !== 'string' || !Validator.isIPRange(value)) {
|
908 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid CIDR', value));
|
909 | }
|
910 | return true;
|
911 | }
|
912 | }
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 | class INET extends ABSTRACT {
|
920 | validate(value) {
|
921 | if (typeof value !== 'string' || !Validator.isIP(value)) {
|
922 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid INET', value));
|
923 | }
|
924 | return true;
|
925 | }
|
926 | }
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 | class MACADDR extends ABSTRACT {
|
935 | validate(value) {
|
936 | if (typeof value !== 'string' || !Validator.isMACAddress(value)) {
|
937 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid MACADDR', value));
|
938 | }
|
939 | return true;
|
940 | }
|
941 | }
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 | class TSVECTOR extends ABSTRACT {
|
950 | validate(value) {
|
951 | if (typeof value !== 'string') {
|
952 | throw new sequelizeErrors.ValidationError(util.format('%j is not a valid string', value));
|
953 | }
|
954 | return true;
|
955 | }
|
956 | }
|
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 | const DataTypes = module.exports = {
|
1004 | ABSTRACT,
|
1005 | STRING,
|
1006 | CHAR,
|
1007 | TEXT,
|
1008 | NUMBER,
|
1009 | TINYINT,
|
1010 | SMALLINT,
|
1011 | MEDIUMINT,
|
1012 | INTEGER,
|
1013 | BIGINT,
|
1014 | FLOAT,
|
1015 | TIME,
|
1016 | DATE,
|
1017 | DATEONLY,
|
1018 | BOOLEAN,
|
1019 | NOW,
|
1020 | BLOB,
|
1021 | DECIMAL,
|
1022 | NUMERIC: DECIMAL,
|
1023 | UUID,
|
1024 | UUIDV1,
|
1025 | UUIDV4,
|
1026 | HSTORE,
|
1027 | JSON: JSONTYPE,
|
1028 | JSONB,
|
1029 | VIRTUAL,
|
1030 | ARRAY,
|
1031 | ENUM,
|
1032 | RANGE,
|
1033 | REAL,
|
1034 | 'DOUBLE PRECISION': DOUBLE,
|
1035 | DOUBLE,
|
1036 | GEOMETRY,
|
1037 | GEOGRAPHY,
|
1038 | CIDR,
|
1039 | INET,
|
1040 | MACADDR,
|
1041 | CITEXT,
|
1042 | TSVECTOR
|
1043 | };
|
1044 |
|
1045 | _.each(DataTypes, (dataType, name) => {
|
1046 |
|
1047 | if (!Object.prototype.hasOwnProperty.call(dataType, 'key')) {
|
1048 | dataType.types = {};
|
1049 | dataType.key = dataType.prototype.key = name;
|
1050 | }
|
1051 | });
|
1052 |
|
1053 | const dialectMap = {};
|
1054 | dialectMap.postgres = require('./dialects/postgres/data-types')(DataTypes);
|
1055 | dialectMap.mysql = require('./dialects/mysql/data-types')(DataTypes);
|
1056 | dialectMap.mariadb = require('./dialects/mariadb/data-types')(DataTypes);
|
1057 | dialectMap.sqlite = require('./dialects/sqlite/data-types')(DataTypes);
|
1058 | dialectMap.mssql = require('./dialects/mssql/data-types')(DataTypes);
|
1059 |
|
1060 | const dialectList = Object.values(dialectMap);
|
1061 |
|
1062 | for (const dataTypes of dialectList) {
|
1063 | _.each(dataTypes, (DataType, key) => {
|
1064 | if (!DataType.key) {
|
1065 | DataType.key = DataType.prototype.key = key;
|
1066 | }
|
1067 | });
|
1068 | }
|
1069 |
|
1070 |
|
1071 | for (const dataTypes of [DataTypes, ...dialectList]) {
|
1072 | _.each(dataTypes, (DataType, key) => {
|
1073 | dataTypes[key] = classToInvokable(DataType);
|
1074 | });
|
1075 | }
|
1076 |
|
1077 | Object.assign(DataTypes, dialectMap);
|