data-types.js
34.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
'use strict';
const util = require('util');
const inherits = require('./utils/inherits');
const _ = require('lodash');
const wkx = require('wkx');
const sequelizeErrors = require('./errors');
const warnings = {};
const Validator = require('./utils/validator-extras').validator;
const momentTz = require('moment-timezone');
const moment = require('moment');
const logger = require('./utils/logger');
function ABSTRACT() {}
ABSTRACT.prototype.dialectTypes = '';
ABSTRACT.prototype.toString = function toString(options) {
return this.toSql(options);
};
ABSTRACT.prototype.toSql = function toSql() {
return this.key;
};
ABSTRACT.warn = function warn(link, text) {
if (!warnings[text]) {
warnings[text] = true;
logger.warn(`${text}, '\n>> Check:', ${link}`);
}
};
ABSTRACT.prototype.stringify = function stringify(value, options) {
if (this._stringify) {
return this._stringify(value, options);
}
return value;
};
ABSTRACT.prototype.bindParam = function bindParam(value, options) {
if (this._bindParam) {
return this._bindParam(value, options);
}
return options.bindParam(this.stringify(value, options));
};
/**
* STRING A variable length string
*
* @param {number} [length=255] length of string
* @param {boolean} [binary=false] Is this binary?
*
* @namespace DataTypes.STRING
*
*/
function STRING(length, binary) {
const options = typeof length === 'object' && length || {length, binary};
if (!(this instanceof STRING)) return new STRING(options);
this.options = options;
this._binary = options.binary;
this._length = options.length || 255;
}
inherits(STRING, ABSTRACT);
STRING.prototype.key = STRING.key = 'STRING';
STRING.prototype.toSql = function toSql() {
return 'VARCHAR(' + this._length + ')' + (this._binary ? ' BINARY' : '');
};
STRING.prototype.validate = function validate(value) {
if (Object.prototype.toString.call(value) !== '[object String]') {
if (this.options.binary && Buffer.isBuffer(value) || _.isNumber(value)) {
return true;
}
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid string', value));
}
return true;
};
Object.defineProperty(STRING.prototype, 'BINARY', {
get() {
this._binary = true;
this.options.binary = true;
return this;
}
});
/**
* CHAR A fixed length string
*
* @param {number} [length=255] length of string
* @param {boolean} [binary=false] Is this binary?
*
* @namespace DataTypes.CHAR
*/
function CHAR(length, binary) {
const options = typeof length === 'object' && length || {length, binary};
if (!(this instanceof CHAR)) return new CHAR(options);
STRING.apply(this, arguments);
}
inherits(CHAR, STRING);
CHAR.prototype.key = CHAR.key = 'CHAR';
CHAR.prototype.toSql = function toSql() {
return 'CHAR(' + this._length + ')' + (this._binary ? ' BINARY' : '');
};
/**
* Unlimited length TEXT column
*
* @param {string} [length=''] could be tiny, medium, long.
*
* @namespace DataTypes.TEXT
*/
function TEXT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof TEXT)) return new TEXT(options);
this.options = options;
this._length = options.length || '';
}
inherits(TEXT, ABSTRACT);
TEXT.prototype.key = TEXT.key = 'TEXT';
TEXT.prototype.toSql = function toSql() {
switch (this._length.toLowerCase()) {
case 'tiny':
return 'TINYTEXT';
case 'medium':
return 'MEDIUMTEXT';
case 'long':
return 'LONGTEXT';
default:
return this.key;
}
};
TEXT.prototype.validate = function validate(value) {
if (!_.isString(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid string', value));
}
return true;
};
/**
* Base number type which is used to build other types
*
* @param {Object} options type options
* @param {string|number} [options.length] length of type, like `INT(4)`
* @param {boolean} [options.zerofill] Is zero filled?
* @param {boolean} [options.unsigned] Is unsigned?
* @param {string|number} [options.decimals] number of decimal points, used with length `FLOAT(5, 4)`
* @param {string|number} [options.precision] defines precision for decimal type
* @param {string|number} [options.scale] defines scale for decimal type
*
* @namespace DataTypes.NUMBER
*/
function NUMBER(options) {
this.options = options;
this._length = options.length;
this._zerofill = options.zerofill;
this._decimals = options.decimals;
this._precision = options.precision;
this._scale = options.scale;
this._unsigned = options.unsigned;
}
inherits(NUMBER, ABSTRACT);
NUMBER.prototype.key = NUMBER.key = 'NUMBER';
NUMBER.prototype.toSql = function toSql() {
let result = this.key;
if (this._length) {
result += '(' + this._length;
if (typeof this._decimals === 'number') {
result += ',' + this._decimals;
}
result += ')';
}
if (this._unsigned) {
result += ' UNSIGNED';
}
if (this._zerofill) {
result += ' ZEROFILL';
}
return result;
};
NUMBER.prototype.validate = function(value) {
if (!Validator.isFloat(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid ' + _.toLower(this.key), value));
}
return true;
};
Object.defineProperty(NUMBER.prototype, 'UNSIGNED', {
get() {
this._unsigned = true;
this.options.unsigned = true;
return this;
}
});
Object.defineProperty(NUMBER.prototype, 'ZEROFILL', {
get() {
this._zerofill = true;
this.options.zerofill = true;
return this;
}
});
/**
* A 32 bit integer
*
* @param {string|number} [length] Integer length, INT(12)
*
* @namespace DataTypes.INTEGER
*/
function INTEGER(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof INTEGER)) return new INTEGER(options);
NUMBER.call(this, options);
}
inherits(INTEGER, NUMBER);
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.validate = function validate(value) {
if (!Validator.isInt(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid ' + _.toLower(this.key), value));
}
return true;
};
/**
* A 8 bit integer
*
* @param {string|number} [length] Integer length
*
* @namespace DataTypes.TINYINT
*/
function TINYINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof TINYINT)) return new TINYINT(options);
NUMBER.call(this, options);
}
inherits(TINYINT, INTEGER);
TINYINT.prototype.key = TINYINT.key = 'TINYINT';
/**
* A 16 bit integer
*
* @param {string|number} [length] Integer length
*
* @namespace DataTypes.SMALLINT
*/
function SMALLINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof SMALLINT)) return new SMALLINT(options);
NUMBER.call(this, options);
}
inherits(SMALLINT, INTEGER);
SMALLINT.prototype.key = SMALLINT.key = 'SMALLINT';
/**
* A 24 bit integer
*
* @param {string|number} [length] Integer length
*
* @namespace DataTypes.MEDIUMINT
*/
function MEDIUMINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof MEDIUMINT)) return new MEDIUMINT(options);
NUMBER.call(this, options);
}
inherits(MEDIUMINT, INTEGER);
MEDIUMINT.prototype.key = MEDIUMINT.key = 'MEDIUMINT';
/**
* A 64 bit integer
*
* @param {string|number} [length] Integer length
*
* @namespace DataTypes.BIGINT
*/
function BIGINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof BIGINT)) return new BIGINT(options);
NUMBER.call(this, options);
}
inherits(BIGINT, INTEGER);
BIGINT.prototype.key = BIGINT.key = 'BIGINT';
/**
* Floating point number (4-byte precision).
*
* @param {string|number} [length] length of type, like `FLOAT(4)`
* @param {string|number} [decimals] number of decimal points, used with length `FLOAT(5, 4)`
*
* @namespace DataTypes.FLOAT
*/
function FLOAT(length, decimals) {
const options = typeof length === 'object' && length || {length, decimals};
if (!(this instanceof FLOAT)) return new FLOAT(options);
NUMBER.call(this, options);
}
inherits(FLOAT, NUMBER);
FLOAT.prototype.key = FLOAT.key = 'FLOAT';
FLOAT.prototype.validate = function validate(value) {
if (!Validator.isFloat(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid float', value));
}
return true;
};
/**
* Floating point number (4-byte precision).
*
* @param {string|number} [length] length of type, like `REAL(4)`
* @param {string|number} [decimals] number of decimal points, used with length `REAL(5, 4)`
*
* @namespace DataTypes.REAL
*/
function REAL(length, decimals) {
const options = typeof length === 'object' && length || {length, decimals};
if (!(this instanceof REAL)) return new REAL(options);
NUMBER.call(this, options);
}
inherits(REAL, NUMBER);
REAL.prototype.key = REAL.key = 'REAL';
/**
* Floating point number (8-byte precision).
*
* @param {string|number} [length] length of type, like `DOUBLE PRECISION(25)`
* @param {string|number} [decimals] number of decimal points, used with length `DOUBLE PRECISION(25, 10)`
*
* @namespace DataTypes.DOUBLE
*/
function DOUBLE(length, decimals) {
const options = typeof length === 'object' && length || {length, decimals};
if (!(this instanceof DOUBLE)) return new DOUBLE(options);
NUMBER.call(this, options);
}
inherits(DOUBLE, NUMBER);
DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
/**
* Decimal type, variable precision, take length as specified by user
*
* @param {string|number} [precision] defines precision
* @param {string|number} [scale] defines scale
*
* @namespace DataTypes.DECIMAL
*/
function DECIMAL(precision, scale) {
const options = typeof precision === 'object' && precision || {precision, scale};
if (!(this instanceof DECIMAL)) return new DECIMAL(options);
NUMBER.call(this, options);
}
inherits(DECIMAL, NUMBER);
DECIMAL.prototype.key = DECIMAL.key = 'DECIMAL';
DECIMAL.prototype.toSql = function toSql() {
if (this._precision || this._scale) {
return 'DECIMAL(' + [this._precision, this._scale].filter(_.identity).join(',') + ')';
}
return 'DECIMAL';
};
DECIMAL.prototype.validate = function validate(value) {
if (!Validator.isDecimal(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid decimal', value));
}
return true;
};
for (const floating of [FLOAT, DOUBLE, REAL]) {
floating.prototype.escape = false;
floating.prototype._value = function _value(value) {
if (isNaN(value)) {
return 'NaN';
} else if (!isFinite(value)) {
const sign = value < 0 ? '-' : '';
return sign + 'Infinity';
}
return value;
};
floating.prototype._stringify = function _stringify(value) {
return "'" + this._value(value) + "'";
};
floating.prototype._bindParam = function _bindParam(value, options) {
return options.bindParam(this._value(value));
};
}
/**
* A boolean / tinyint column, depending on dialect
*
* @namespace DataTypes.BOOLEAN
*/
function BOOLEAN() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
}
inherits(BOOLEAN, ABSTRACT);
BOOLEAN.prototype.key = BOOLEAN.key = 'BOOLEAN';
BOOLEAN.prototype.toSql = function toSql() {
return 'TINYINT(1)';
};
BOOLEAN.prototype.validate = function validate(value) {
if (!Validator.isBoolean(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid boolean', value));
}
return true;
};
BOOLEAN.prototype._sanitize = function _sanitize(value) {
if (value !== null && value !== undefined) {
if (Buffer.isBuffer(value) && value.length === 1) {
// Bit fields are returned as buffers
value = value[0];
}
if (_.isString(value)) {
// Only take action on valid boolean strings.
value = value === 'true' ? true : value === 'false' ? false : value;
} else if (_.isNumber(value)) {
// Only take action on valid boolean integers.
value = value === 1 ? true : value === 0 ? false : value;
}
}
return value;
};
BOOLEAN.parse = BOOLEAN.prototype._sanitize;
/**
* A time column
*
* @namespace DataTypes.TIME
*/
function TIME() {
if (!(this instanceof TIME)) return new TIME();
}
inherits(TIME, ABSTRACT);
TIME.prototype.key = TIME.key = 'TIME';
TIME.prototype.toSql = function toSql() {
return 'TIME';
};
/**
* Date column with timezone, default is UTC
*
* @param {string|number} [length] precision to allow storing milliseconds
*
* @namespace DataTypes.DATE
*/
function DATE(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof DATE)) return new DATE(options);
this.options = options;
this._length = options.length || '';
}
inherits(DATE, ABSTRACT);
DATE.prototype.key = DATE.key = 'DATE';
DATE.prototype.toSql = function toSql() {
return 'DATETIME';
};
DATE.prototype.validate = function validate(value) {
if (!Validator.isDate(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid date', value));
}
return true;
};
DATE.prototype._sanitize = function _sanitize(value, options) {
if ((!options || options && !options.raw) && !(value instanceof Date) && !!value) {
return new Date(value);
}
return value;
};
DATE.prototype._isChanged = function _isChanged(value, originalValue) {
if (
originalValue && !!value &&
(
value === originalValue ||
value instanceof Date && originalValue instanceof Date && value.getTime() === originalValue.getTime()
)
) {
return false;
}
// not changed when set to same empty value
if (!originalValue && !value && originalValue === value) {
return false;
}
return true;
};
DATE.prototype._applyTimezone = function _applyTimezone(date, options) {
if (options.timezone) {
if (momentTz.tz.zone(options.timezone)) {
date = momentTz(date).tz(options.timezone);
} else {
date = moment(date).utcOffset(options.timezone);
}
} else {
date = momentTz(date);
}
return date;
};
DATE.prototype._stringify = function _stringify(date, options) {
date = this._applyTimezone(date, options);
// Z here means current timezone, _not_ UTC
return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
};
/**
* A date only column (no timestamp)
*
* @namespace DataTypes.DATEONLY
*/
function DATEONLY() {
if (!(this instanceof DATEONLY)) return new DATEONLY();
}
util.inherits(DATEONLY, ABSTRACT);
DATEONLY.prototype.key = DATEONLY.key = 'DATEONLY';
DATEONLY.prototype.toSql = function() {
return 'DATE';
};
DATEONLY.prototype._stringify = function _stringify(date) {
return moment(date).format('YYYY-MM-DD');
};
DATEONLY.prototype._sanitize = function _sanitize(value, options) {
if ((!options || options && !options.raw) && !!value) {
return moment(value).format('YYYY-MM-DD');
}
return value;
};
DATEONLY.prototype._isChanged = function _isChanged(value, originalValue) {
if (originalValue && !!value && originalValue === value) {
return false;
}
// not changed when set to same empty value
if (!originalValue && !value && originalValue === value) {
return false;
}
return true;
};
/**
* A key / value store column. Only available in Postgres.
*
* @namespace DataTypes.HSTORE
*/
function HSTORE() {
if (!(this instanceof HSTORE)) return new HSTORE();
}
inherits(HSTORE, ABSTRACT);
HSTORE.prototype.key = HSTORE.key = 'HSTORE';
HSTORE.prototype.validate = function validate(value) {
if (!_.isPlainObject(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid hstore', value));
}
return true;
};
/**
* A JSON string column. Available in MySQL, Postgres and SQLite
*
* @namespace DataTypes.JSON
*/
function JSONTYPE() {
if (!(this instanceof JSONTYPE)) return new JSONTYPE();
}
inherits(JSONTYPE, ABSTRACT);
JSONTYPE.prototype.key = JSONTYPE.key = 'JSON';
JSONTYPE.prototype.validate = function validate() {
return true;
};
JSONTYPE.prototype._stringify = function _stringify(value) {
return JSON.stringify(value);
};
/**
* A binary storage JSON column. Only available in Postgres.
*
* @namespace DataTypes.JSONB
*/
function JSONB() {
if (!(this instanceof JSONB)) return new JSONB();
JSONTYPE.call(this);
}
inherits(JSONB, JSONTYPE);
JSONB.prototype.key = JSONB.key = 'JSONB';
/**
* A default value of the current timestamp
*
* @namespace DataTypes.NOW
*/
function NOW() {
if (!(this instanceof NOW)) return new NOW();
}
inherits(NOW, ABSTRACT);
NOW.prototype.key = NOW.key = 'NOW';
/**
* Binary storage
*
* @param {string} [length=''] could be tiny, medium, long.
*
* @namespace DataTypes.BLOB
*
*/
function BLOB(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof BLOB)) return new BLOB(options);
this.options = options;
this._length = options.length || '';
}
inherits(BLOB, ABSTRACT);
BLOB.prototype.key = BLOB.key = 'BLOB';
BLOB.prototype.toSql = function toSql() {
switch (this._length.toLowerCase()) {
case 'tiny':
return 'TINYBLOB';
case 'medium':
return 'MEDIUMBLOB';
case 'long':
return 'LONGBLOB';
default:
return this.key;
}
};
BLOB.prototype.validate = function validate(value) {
if (!_.isString(value) && !Buffer.isBuffer(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid blob', value));
}
return true;
};
BLOB.prototype.escape = false;
BLOB.prototype._stringify = function _stringify(value) {
if (!Buffer.isBuffer(value)) {
if (Array.isArray(value)) {
value = Buffer.from(value);
} else {
value = Buffer.from(value.toString());
}
}
const hex = value.toString('hex');
return this._hexify(hex);
};
BLOB.prototype._hexify = function _hexify(hex) {
return "X'" + hex + "'";
};
BLOB.prototype._bindParam = function _bindParam(value, options) {
if (!Buffer.isBuffer(value)) {
if (Array.isArray(value)) {
value = Buffer.from(value);
} else {
value = Buffer.from(value.toString());
}
}
return options.bindParam(value);
};
/**
* Range types are data types representing a range of values of some element type (called the range's subtype).
* Only available in Postgres. See [the Postgres documentation](http://www.postgresql.org/docs/9.4/static/rangetypes.html) for more details
*
* @param {<DataTypes>} subtype A subtype for range, like RANGE(DATE)
*
* @namespace DataTypes.RANGE
*/
function RANGE(subtype) {
const options = _.isPlainObject(subtype) ? subtype : {subtype};
if (!options.subtype) options.subtype = new INTEGER();
if (_.isFunction(options.subtype)) {
options.subtype = new options.subtype();
}
if (!(this instanceof RANGE)) return new RANGE(options);
this._subtype = options.subtype.key;
this.options = options;
}
inherits(RANGE, ABSTRACT);
const pgRangeSubtypes = {
integer: 'int4range',
bigint: 'int8range',
decimal: 'numrange',
dateonly: 'daterange',
date: 'tstzrange',
datenotz: 'tsrange'
};
const pgRangeCastTypes = {
integer: 'integer',
bigint: 'bigint',
decimal: 'numeric',
dateonly: 'date',
date: 'timestamptz',
datenotz: 'timestamp'
};
RANGE.prototype.key = RANGE.key = 'RANGE';
RANGE.prototype.toSql = function toSql() {
return pgRangeSubtypes[this._subtype.toLowerCase()];
};
RANGE.prototype.toCastType = function toCastType() {
return pgRangeCastTypes[this._subtype.toLowerCase()];
};
RANGE.prototype.validate = function validate(value) {
if (!_.isArray(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid range', value));
}
if (value.length !== 2) {
throw new sequelizeErrors.ValidationError('A range must be an array with two elements');
}
return true;
};
/**
* A column storing a unique universal identifier.
* Use with `UUIDV1` or `UUIDV4` for default values.
*
* @namespace DataTypes.UUID
*/
function UUID() {
if (!(this instanceof UUID)) return new UUID();
}
inherits(UUID, ABSTRACT);
UUID.prototype.key = UUID.key = 'UUID';
UUID.prototype.validate = function validate(value, options) {
if (!_.isString(value) || !Validator.isUUID(value) && (!options || !options.acceptStrings)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid uuid', value));
}
return true;
};
/**
* A default unique universal identifier generated following the UUID v1 standard
*
* @namespace DataTypes.UUIDV1
*/
function UUIDV1() {
if (!(this instanceof UUIDV1)) return new UUIDV1();
}
inherits(UUIDV1, ABSTRACT);
UUIDV1.prototype.key = UUIDV1.key = 'UUIDV1';
UUIDV1.prototype.validate = function validate(value, options) {
if (!_.isString(value) || !Validator.isUUID(value) && (!options || !options.acceptStrings)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid uuid', value));
}
return true;
};
/**
* A default unique universal identifier generated following the UUID v4 standard
*
* @namespace DataTypes.UUIDV4
*/
function UUIDV4() {
if (!(this instanceof UUIDV4)) return new UUIDV4();
}
inherits(UUIDV4, ABSTRACT);
UUIDV4.prototype.key = UUIDV4.key = 'UUIDV4';
UUIDV4.prototype.validate = function validate(value, options) {
if (!_.isString(value) || !Validator.isUUID(value, 4) && (!options || !options.acceptStrings)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid uuidv4', value));
}
return true;
};
/**
* A virtual value that is not stored in the DB. This could for example be useful if you want to provide a default value in your model that is returned to the user but not stored in the DB.
*
* You could also use it to validate a value before permuting and storing it. VIRTUAL also takes a return type and dependency fields as arguments
* If a virtual attribute is present in `attributes` it will automatically pull in the extra fields as well.
* Return type is mostly useful for setups that rely on types like GraphQL.
*
* @example <caption>Checking password length before hashing it</caption>
* sequelize.define('user', {
* password_hash: DataTypes.STRING,
* password: {
* type: DataTypes.VIRTUAL,
* set: function (val) {
* // Remember to set the data value, otherwise it won't be validated
* this.setDataValue('password', val);
* this.setDataValue('password_hash', this.salt + val);
* },
* validate: {
* isLongEnough: function (val) {
* if (val.length < 7) {
* throw new Error("Please choose a longer password")
* }
* }
* }
* }
* })
*
* # In the above code the password is stored plainly in the password field so it can be validated, but is never stored in the DB.
*
* @example <caption>Virtual with dependency fields</caption>
* {
* active: {
* type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['createdAt']),
* get: function() {
* return this.get('createdAt') > Date.now() - (7 * 24 * 60 * 60 * 1000)
* }
* }
* }
*
* @param {<DataTypes>} [ReturnType] return type for virtual type
* @param {Array} [fields] array of fields this virtual type is dependent on
*
* @namespace DataTypes.VIRTUAL
*
*/
function VIRTUAL(ReturnType, fields) {
if (!(this instanceof VIRTUAL)) return new VIRTUAL(ReturnType, fields);
if (typeof ReturnType === 'function') ReturnType = new ReturnType();
this.returnType = ReturnType;
this.fields = fields;
}
inherits(VIRTUAL, ABSTRACT);
VIRTUAL.prototype.key = VIRTUAL.key = 'VIRTUAL';
/**
* An enumeration, Postgres Only
*
* @example
* DataTypes.ENUM('value', 'another value')
* DataTypes.ENUM(['value', 'another value'])
* DataTypes.ENUM({
* values: ['value', 'another value']
* })
*
* @param {Array|Object} value either array of values or options object with values array. It also supports variadic values
*
* @namespace DataTypes.ENUM
*
*/
function ENUM(value) {
const options = typeof value === 'object' && !Array.isArray(value) && value || {
values: Array.prototype.slice.call(arguments).reduce((result, element) => {
return result.concat(Array.isArray(element) ? element : [element]);
}, [])
};
if (!(this instanceof ENUM)) return new ENUM(options);
this.values = options.values;
this.options = options;
}
inherits(ENUM, ABSTRACT);
ENUM.prototype.key = ENUM.key = 'ENUM';
ENUM.prototype.validate = function validate(value) {
if (!_.includes(this.values, value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid choice in %j', value, this.values));
}
return true;
};
/**
* An array of `type`. Only available in Postgres.
*
* @example
* DataTypes.ARRAY(DataTypes.DECIMAL)`.
*
* @param {<DataTypes>} type type of array values
*
* @namespace DataTypes.ARRAY
*/
function ARRAY(type) {
const options = _.isPlainObject(type) ? type : {type};
if (!(this instanceof ARRAY)) return new ARRAY(options);
this.type = typeof options.type === 'function' ? new options.type() : options.type;
}
inherits(ARRAY, ABSTRACT);
ARRAY.prototype.key = ARRAY.key = 'ARRAY';
ARRAY.prototype.toSql = function toSql() {
return this.type.toSql() + '[]';
};
ARRAY.prototype.validate = function validate(value) {
if (!_.isArray(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid array', value));
}
return true;
};
ARRAY.is = function is(obj, type) {
return obj instanceof ARRAY && obj.type instanceof type;
};
/**
* A column storing Geometry information.
* It is only available in PostgreSQL (with PostGIS) or MySQL.
* In MySQL, allowable Geometry types are `POINT`, `LINESTRING`, `POLYGON`.
*
* GeoJSON is accepted as input and returned as output.
*
* In PostGIS, the GeoJSON is parsed using the PostGIS function `ST_GeomFromGeoJSON`.
* In MySQL it is parsed using the function `GeomFromText`.
*
* Therefore, one can just follow the [GeoJSON spec](http://geojson.org/geojson-spec.html) for handling geometry objects. See the following examples:
*
* @example <caption>Defining a Geometry type attribute</caption>
* DataTypes.GEOMETRY
* DataTypes.GEOMETRY('POINT')
* DataTypes.GEOMETRY('POINT', 4326)
*
* @example <caption>Create a new point</caption>
* const point = { type: 'Point', coordinates: [39.807222,-76.984722]};
*
* User.create({username: 'username', geometry: point });
*
* @example <caption>Create a new linestring</caption>
* const line = { type: 'LineString', 'coordinates': [ [100.0, 0.0], [101.0, 1.0] ] };
*
* User.create({username: 'username', geometry: line });
*
* @example <caption>Create a new polygon</caption>
* const polygon = { type: 'Polygon', coordinates: [
* [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
* [100.0, 1.0], [100.0, 0.0] ]
* ]};
*
* User.create({username: 'username', geometry: polygon });
* @example <caption>Create a new point with a custom SRID</caption>
* const point = {
* type: 'Point',
* coordinates: [39.807222,-76.984722],
* crs: { type: 'name', properties: { name: 'EPSG:4326'} }
* };
*
* User.create({username: 'username', geometry: point })
*
* @param {string} [type] Type of geometry data
* @param {string} [srid] SRID of type
*
* @see {@link DataTypes.GEOGRAPHY}
* @namespace DataTypes.GEOMETRY
*/
function GEOMETRY(type, srid) {
const options = _.isPlainObject(type) ? type : {type, srid};
if (!(this instanceof GEOMETRY)) return new GEOMETRY(options);
this.options = options;
this.type = options.type;
this.srid = options.srid;
}
inherits(GEOMETRY, ABSTRACT);
GEOMETRY.prototype.key = GEOMETRY.key = 'GEOMETRY';
GEOMETRY.prototype.escape = false;
GEOMETRY.prototype._stringify = function _stringify(value, options) {
return 'GeomFromText(' + options.escape(wkx.Geometry.parseGeoJSON(value).toWkt()) + ')';
};
GEOMETRY.prototype._bindParam = function _bindParam(value, options) {
return 'GeomFromText(' + options.bindParam(wkx.Geometry.parseGeoJSON(value).toWkt()) + ')';
};
/**
* A geography datatype represents two dimensional spacial objects in an elliptic coord system.
*
* __The difference from geometry and geography type:__
*
* PostGIS 1.5 introduced a new spatial type called geography, which uses geodetic measurement instead of Cartesian measurement.
* Coordinate points in the geography type are always represented in WGS 84 lon lat degrees (SRID 4326),
* but measurement functions and relationships ST_Distance, ST_DWithin, ST_Length, and ST_Area always return answers in meters or assume inputs in meters.
*
* __What is best to use? It depends:__
*
* When choosing between the geometry and geography type for data storage, you should consider what you’ll be using it for.
* If all you do are simple measurements and relationship checks on your data, and your data covers a fairly large area, then most likely you’ll be better off storing your data using the new geography type.
* Although the new geography data type can cover the globe, the geometry type is far from obsolete.
* The geometry type has a much richer set of functions than geography, relationship checks are generally faster, and it has wider support currently across desktop and web-mapping tools
*
* @example <caption>Defining a Geography type attribute</caption>
* DataTypes.GEOGRAPHY
* DataTypes.GEOGRAPHY('POINT')
* DataTypes.GEOGRAPHY('POINT', 4326)
*
* @param {string} [type] Type of geography data
* @param {string} [srid] SRID of type
*
* @namespace DataTypes.GEOGRAPHY
*/
function GEOGRAPHY(type, srid) {
const options = _.isPlainObject(type) ? type : {type, srid};
if (!(this instanceof GEOGRAPHY)) return new GEOGRAPHY(options);
this.options = options;
this.type = options.type;
this.srid = options.srid;
}
inherits(GEOGRAPHY, ABSTRACT);
GEOGRAPHY.prototype.key = GEOGRAPHY.key = 'GEOGRAPHY';
GEOGRAPHY.prototype.escape = false;
GEOGRAPHY.prototype._stringify = function _stringify(value, options) {
return 'GeomFromText(' + options.escape(wkx.Geometry.parseGeoJSON(value).toWkt()) + ')';
};
GEOGRAPHY.prototype._bindParam = function _bindParam(value, options) {
return 'GeomFromText(' + options.bindParam(wkx.Geometry.parseGeoJSON(value).toWkt()) + ')';
};
/**
* The cidr type holds an IPv4 or IPv6 network specification. Takes 7 or 19 bytes.
*
* Only available for Postgres
*
* @namespace DataTypes.CIDR
*/
function CIDR() {
if (!(this instanceof CIDR)) return new CIDR();
}
inherits(CIDR, ABSTRACT);
CIDR.prototype.key = CIDR.key = 'CIDR';
CIDR.prototype.validate = function validate(value) {
if (!_.isString(value) || !Validator.isIPRange(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid CIDR', value));
}
return true;
};
/**
* The INET type holds an IPv4 or IPv6 host address, and optionally its subnet. Takes 7 or 19 bytes
*
* Only available for Postgres
*
* @namespace DataTypes.INET
*/
function INET() {
if (!(this instanceof INET)) return new INET();
}
inherits(INET, ABSTRACT);
INET.prototype.key = INET.key = 'INET';
INET.prototype.validate = function validate(value) {
if (!_.isString(value) || !Validator.isIP(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid INET', value));
}
return true;
};
/**
* The MACADDR type stores MAC addresses. Takes 6 bytes
*
* Only available for Postgres
*
* @namespace DataTypes.MACADDR
*/
function MACADDR() {
if (!(this instanceof MACADDR)) return new MACADDR();
}
inherits(MACADDR, ABSTRACT);
MACADDR.prototype.key = MACADDR.key = 'MACADDR';
MACADDR.prototype.validate = function validate(value) {
if (!_.isString(value) || !Validator.isMACAddress(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid MACADDR', value));
}
return true;
};
const helpers = {
BINARY: [STRING, CHAR],
UNSIGNED: [NUMBER, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
ZEROFILL: [NUMBER, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
PRECISION: [DECIMAL],
SCALE: [DECIMAL]
};
for (const helper of Object.keys(helpers)) {
for (const DataType of helpers[helper]) {
if (!DataType[helper]) {
Object.defineProperty(DataType, helper, {
get() {
const dataType = new DataType();
if (typeof dataType[helper] === 'object') {
return dataType;
}
return dataType[helper].apply(dataType, arguments);
}
});
}
}
}
/**
* A convenience class holding commonly used data types. The data types are used when defining a new model using `Sequelize.define`, like this:
* ```js
* sequelize.define('model', {
* column: DataTypes.INTEGER
* })
* ```
* When defining a model you can just as easily pass a string as type, but often using the types defined here is beneficial. For example, using `DataTypes.BLOB`, mean
* that that column will be returned as an instance of `Buffer` when being fetched by sequelize.
*
* To provide a length for the data type, you can invoke it like a function: `INTEGER(2)`
*
* Some data types have special properties that can be accessed in order to change the data type.
* For example, to get an unsigned integer with zerofill you can do `DataTypes.INTEGER.UNSIGNED.ZEROFILL`.
* The order you access the properties in do not matter, so `DataTypes.INTEGER.ZEROFILL.UNSIGNED` is fine as well.
*
* * All number types (`INTEGER`, `BIGINT`, `FLOAT`, `DOUBLE`, `REAL`, `DECIMAL`) expose the properties `UNSIGNED` and `ZEROFILL`
* * The `CHAR` and `STRING` types expose the `BINARY` property
*
* Three of the values provided here (`NOW`, `UUIDV1` and `UUIDV4`) are special default values, that should not be used to define types. Instead they are used as shorthands for
* defining default values. For example, to get a uuid field with a default value generated following v1 of the UUID standard:
* ```js`
* sequelize.define('model',` {
* uuid: {
* type: DataTypes.UUID,
* defaultValue: DataTypes.UUIDV1,
* primaryKey: true
* }
* })
* ```
* There may be times when you want to generate your own UUID conforming to some other algorithm. This is accomplished
* using the defaultValue property as well, but instead of specifying one of the supplied UUID types, you return a value
* from a function.
* ```js
* sequelize.define('model', {
* uuid: {
* type: DataTypes.UUID,
* defaultValue: function() {
* return generateMyId()
* },
* primaryKey: true
* }
* })
* ```
*
* @namespace DataTypes
*/
const DataTypes = module.exports = {
ABSTRACT,
STRING,
CHAR,
TEXT,
NUMBER,
TINYINT,
SMALLINT,
MEDIUMINT,
INTEGER,
BIGINT,
FLOAT,
TIME,
DATE,
DATEONLY,
BOOLEAN,
NOW,
BLOB,
DECIMAL,
NUMERIC: DECIMAL,
UUID,
UUIDV1,
UUIDV4,
HSTORE,
JSON: JSONTYPE,
JSONB,
VIRTUAL,
ARRAY,
ENUM,
RANGE,
REAL,
DOUBLE,
'DOUBLE PRECISION': DOUBLE,
GEOMETRY,
GEOGRAPHY,
CIDR,
INET,
MACADDR
};
_.each(DataTypes, dataType => {
dataType.types = {};
});
DataTypes.postgres = require('./dialects/postgres/data-types')(DataTypes);
DataTypes.mysql = require('./dialects/mysql/data-types')(DataTypes);
DataTypes.sqlite = require('./dialects/sqlite/data-types')(DataTypes);
DataTypes.mssql = require('./dialects/mssql/data-types')(DataTypes);
module.exports = DataTypes;