UNPKG

18.6 kBTypeScriptView Raw
1/**
2 * The datatypes are used when defining a new model using `Model.init`, like this:
3 * ```js
4 * class MyModel extends MyModel {}
5 * MyModel.init({ column: DataTypes.INTEGER }, { sequelize });
6 * ```
7 * 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
8 * that that column will be returned as an instance of `Buffer` when being fetched by sequelize.
9 *
10 * Some data types have special properties that can be accessed in order to change the data type.
11 * For example, to get an unsigned integer with zerofill you can do `DataTypes.INTEGER.UNSIGNED.ZEROFILL`.
12 * The order you access the properties in do not matter, so `DataTypes.INTEGER.ZEROFILL.UNSIGNED` is fine as well. The available properties are listed under each data type.
13 *
14 * To provide a length for the data type, you can invoke it like a function: `INTEGER(2)`
15 *
16 * 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
17 * defining default values. For example, to get a uuid field with a default value generated following v1 of the UUID standard:
18 * ```js
19 * class MyModel extends Model {}
20 * MyModel.init({
21 * uuid: {
22 * type: DataTypes.UUID,
23 * defaultValue: DataTypes.UUIDV1,
24 * primaryKey: true
25 * }
26 * }, { sequelize })
27 * ```
28 * There may be times when you want to generate your own UUID conforming to some other algorithm. This is accomplised
29 * using the defaultValue property as well, but instead of specifying one of the supplied UUID types, you return a value
30 * from a function.
31 * ```js
32 * class MyModel extends Model {}
33 * MyModel.init({
34 * uuid: {
35 * type: DataTypes.UUID,
36 * defaultValue() {
37 * return generateMyId()
38 * },
39 * primaryKey: true
40 * }
41 * }, { sequelize })
42 * ```
43 */
44
45/**
46 *
47 */
48export type DataType = string | AbstractDataTypeConstructor | AbstractDataType;
49
50export const ABSTRACT: AbstractDataTypeConstructor;
51
52interface AbstractDataTypeConstructor {
53 key: string;
54 warn(link: string, text: string): void;
55 new (): AbstractDataType;
56 (): AbstractDataType;
57}
58
59export interface AbstractDataType {
60 key: string;
61 dialectTypes: string;
62 toSql(): string;
63 stringify(value: unknown, options?: object): string;
64 toString(options: object): string;
65}
66
67/**
68 * A variable length string. Default length 255
69 */
70export const STRING: StringDataTypeConstructor;
71
72interface StringDataTypeConstructor extends AbstractDataTypeConstructor {
73 new (length?: number, binary?: boolean): StringDataType;
74 new (options?: StringDataTypeOptions): StringDataType;
75 (length?: number, binary?: boolean): StringDataType;
76 (options?: StringDataTypeOptions): StringDataType;
77}
78
79export interface StringDataType extends AbstractDataType {
80 options?: StringDataTypeOptions;
81 BINARY: this;
82 validate(value: unknown): boolean;
83}
84
85export interface StringDataTypeOptions {
86 length?: number;
87 binary?: boolean;
88}
89
90/**
91 * A fixed length string. Default length 255
92 */
93export const CHAR: CharDataTypeConstructor;
94
95interface CharDataTypeConstructor extends StringDataTypeConstructor {
96 new (length?: number, binary?: boolean): CharDataType;
97 new (options?: CharDataTypeOptions): CharDataType;
98 (length?: number, binary?: boolean): CharDataType;
99 (options?: CharDataTypeOptions): CharDataType;
100}
101
102export interface CharDataType extends StringDataType {
103 options: CharDataTypeOptions;
104}
105
106export interface CharDataTypeOptions extends StringDataTypeOptions {}
107
108export type TextLength = 'tiny' | 'medium' | 'long';
109
110/**
111 * An (un)limited length text column. Available lengths: `tiny`, `medium`, `long`
112 */
113export const TEXT: TextDataTypeConstructor;
114
115interface TextDataTypeConstructor extends AbstractDataTypeConstructor {
116 new (length?: TextLength): TextDataType;
117 new (options?: TextDataTypeOptions): TextDataType;
118 (length?: TextLength): TextDataType;
119 (options?: TextDataTypeOptions): TextDataType;
120}
121
122export interface TextDataType extends AbstractDataType {
123 options: TextDataTypeOptions;
124 validate(value: unknown): boolean;
125}
126
127export interface TextDataTypeOptions {
128 length?: TextLength;
129}
130
131export const NUMBER: NumberDataTypeConstructor;
132
133interface NumberDataTypeConstructor extends AbstractDataTypeConstructor {
134 options: NumberDataTypeOptions;
135 UNSIGNED: this;
136 ZEROFILL: this;
137 new (options?: NumberDataTypeOptions): NumberDataType;
138 (options?: NumberDataTypeOptions): NumberDataType;
139 validate(value: unknown): boolean;
140}
141
142export interface NumberDataType extends AbstractDataType {
143 options: NumberDataTypeOptions;
144 UNSIGNED: this;
145 ZEROFILL: this;
146 validate(value: unknown): boolean;
147}
148
149export interface IntegerDataTypeOptions {
150 length?: number;
151 zerofill?: boolean;
152 unsigned?: boolean;
153}
154export interface NumberDataTypeOptions extends IntegerDataTypeOptions {
155 decimals?: number;
156 precision?: number;
157 scale?: number;
158}
159
160/**
161 * A 8 bit integer.
162 */
163export const TINYINT: TinyIntegerDataTypeConstructor;
164
165interface TinyIntegerDataTypeConstructor extends NumberDataTypeConstructor {
166 new (options?: IntegerDataTypeOptions): TinyIntegerDataType;
167 (options?: IntegerDataTypeOptions): TinyIntegerDataType;
168}
169
170export interface TinyIntegerDataType extends NumberDataType {
171 options: IntegerDataTypeOptions;
172}
173
174/**
175 * A 16 bit integer.
176 */
177export const SMALLINT: SmallIntegerDataTypeConstructor;
178
179interface SmallIntegerDataTypeConstructor extends NumberDataTypeConstructor {
180 new (options?: IntegerDataTypeOptions): SmallIntegerDataType;
181 (options?: IntegerDataTypeOptions): SmallIntegerDataType;
182}
183
184export interface SmallIntegerDataType extends NumberDataType {
185 options: IntegerDataTypeOptions;
186}
187
188/**
189 * A 24 bit integer.
190 */
191export const MEDIUMINT: MediumIntegerDataTypeConstructor;
192
193interface MediumIntegerDataTypeConstructor extends NumberDataTypeConstructor {
194 new (options?: IntegerDataTypeOptions): MediumIntegerDataType;
195 (options?: IntegerDataTypeOptions): MediumIntegerDataType;
196}
197
198export interface MediumIntegerDataType extends NumberDataType {
199 options: IntegerDataTypeOptions;
200}
201
202/**
203 * A 32 bit integer.
204 */
205export const INTEGER: IntegerDataTypeConstructor;
206
207interface IntegerDataTypeConstructor extends NumberDataTypeConstructor {
208 new (options?: NumberDataTypeOptions): IntegerDataType;
209 (options?: NumberDataTypeOptions): IntegerDataType;
210}
211
212export interface IntegerDataType extends NumberDataType {
213 options: NumberDataTypeOptions;
214}
215
216/**
217 * A 64 bit integer.
218 *
219 * Available properties: `UNSIGNED`, `ZEROFILL`
220 *
221 */
222export const BIGINT: BigIntDataTypeConstructor;
223
224interface BigIntDataTypeConstructor extends NumberDataTypeConstructor {
225 new (options?: IntegerDataTypeOptions): BigIntDataType;
226 (options?: IntegerDataTypeOptions): BigIntDataType;
227}
228
229export interface BigIntDataType extends NumberDataType {
230 options: IntegerDataTypeOptions;
231}
232
233/**
234 * Floating point number (4-byte precision). Accepts one or two arguments for precision
235 */
236export const FLOAT: FloatDataTypeConstructor;
237
238interface FloatDataTypeConstructor extends NumberDataTypeConstructor {
239 new (length?: number, decimals?: number): FloatDataType;
240 new (options?: FloatDataTypeOptions): FloatDataType;
241 (length?: number, decimals?: number): FloatDataType;
242 (options?: FloatDataTypeOptions): FloatDataType;
243}
244
245export interface FloatDataType extends NumberDataType {
246 options: FloatDataTypeOptions;
247}
248
249export interface FloatDataTypeOptions {
250 length?: number;
251 decimals?: number;
252}
253
254/**
255 * Floating point number (4-byte precision). Accepts one or two arguments for precision
256 */
257export const REAL: RealDataTypeConstructor;
258
259interface RealDataTypeConstructor extends NumberDataTypeConstructor {
260 new (length?: number, decimals?: number): RealDataType;
261 new (options?: RealDataTypeOptions): RealDataType;
262 (length?: number, decimals?: number): RealDataType;
263 (options?: RealDataTypeOptions): RealDataType;
264}
265
266export interface RealDataType extends NumberDataType {
267 options: RealDataTypeOptions;
268}
269
270export interface RealDataTypeOptions {
271 length?: number;
272 decimals?: number;
273}
274
275/**
276 * Floating point number (8-byte precision). Accepts one or two arguments for precision
277 */
278export const DOUBLE: DoubleDataTypeConstructor;
279
280interface DoubleDataTypeConstructor extends NumberDataTypeConstructor {
281 new (length?: number, decimals?: number): DoubleDataType;
282 new (options?: DoubleDataTypeOptions): DoubleDataType;
283 (length?: number, decimals?: number): DoubleDataType;
284 (options?: DoubleDataTypeOptions): DoubleDataType;
285}
286
287export interface DoubleDataType extends NumberDataType {
288 options: DoubleDataTypeOptions;
289}
290
291export interface DoubleDataTypeOptions {
292 length?: number;
293 decimals?: number;
294}
295
296/**
297 * Decimal number. Accepts one or two arguments for precision
298 */
299export const DECIMAL: DecimalDataTypeConstructor;
300
301interface DecimalDataTypeConstructor extends NumberDataTypeConstructor {
302 PRECISION: this;
303 SCALE: this;
304 new (precision?: number, scale?: number): DecimalDataType;
305 new (options?: DecimalDataTypeOptions): DecimalDataType;
306 (precision?: number, scale?: number): DecimalDataType;
307 (options?: DecimalDataTypeOptions): DecimalDataType;
308}
309
310export interface DecimalDataType extends NumberDataType {
311 options: DecimalDataTypeOptions;
312}
313
314export interface DecimalDataTypeOptions {
315 precision?: number;
316 scale?: number;
317}
318
319/**
320 * A boolean / tinyint column, depending on dialect
321 */
322export const BOOLEAN: AbstractDataTypeConstructor;
323
324/**
325 * A time column
326 */
327export const TIME: AbstractDataTypeConstructor;
328
329/**
330 * A datetime column
331 */
332export const DATE: DateDataTypeConstructor;
333
334interface DateDataTypeConstructor extends AbstractDataTypeConstructor {
335 new (length?: string | number): DateDataType;
336 new (options?: DateDataTypeOptions): DateDataType;
337 (length?: string | number): DateDataType;
338 (options?: DateDataTypeOptions): DateDataType;
339}
340
341export interface DateDataType extends AbstractDataType {
342 options: DateDataTypeOptions;
343}
344
345export interface DateDataTypeOptions {
346 length?: string | number;
347}
348
349/**
350 * A date only column
351 */
352export const DATEONLY: DateOnlyDataTypeConstructor;
353
354interface DateOnlyDataTypeConstructor extends AbstractDataTypeConstructor {
355 new (): DateOnlyDataType;
356 (): DateOnlyDataType;
357}
358
359export interface DateOnlyDataType extends AbstractDataType {
360}
361
362
363/**
364 * A key / value column. Only available in postgres.
365 */
366export const HSTORE: AbstractDataTypeConstructor;
367
368/**
369 * A JSON string column. Only available in postgres.
370 */
371export const JSON: AbstractDataTypeConstructor;
372/**
373 * A pre-processed JSON data column. Only available in postgres.
374 */
375export const JSONB: AbstractDataTypeConstructor;
376
377/**
378 * A default value of the current timestamp
379 */
380export const NOW: AbstractDataTypeConstructor;
381
382/**
383 * Binary storage. Available lengths: `tiny`, `medium`, `long`
384 */
385export const BLOB: BlobDataTypeConstructor;
386
387export type BlobSize = 'tiny' | 'medium' | 'long';
388
389interface BlobDataTypeConstructor extends AbstractDataTypeConstructor {
390 new (length?: BlobSize): BlobDataType;
391 new (options?: BlobDataTypeOptions): BlobDataType;
392 (length?: BlobSize): BlobDataType;
393 (options?: BlobDataTypeOptions): BlobDataType;
394}
395
396export interface BlobDataType extends AbstractDataType {
397 options: BlobDataTypeOptions;
398 escape: boolean;
399}
400
401export interface BlobDataTypeOptions {
402 length?: BlobSize;
403 escape?: boolean;
404}
405
406/**
407 * Range types are data types representing a range of values of some element type (called the range's subtype).
408 * Only available in postgres.
409 *
410 * See [Postgres documentation](http://www.postgresql.org/docs/9.4/static/rangetypes.html) for more details
411 */
412export const RANGE: RangeDataTypeConstructor;
413
414export type RangeableDataType =
415 | IntegerDataTypeConstructor
416 | IntegerDataType
417 | BigIntDataTypeConstructor
418 | BigIntDataType
419 | DecimalDataTypeConstructor
420 | DecimalDataType
421 | DateOnlyDataTypeConstructor
422 | DateOnlyDataType
423 | DateDataTypeConstructor
424 | DateDataType;
425
426interface RangeDataTypeConstructor extends AbstractDataTypeConstructor {
427 new <T extends RangeableDataType>(subtype?: T): RangeDataType<T>;
428 new <T extends RangeableDataType>(options: RangeDataTypeOptions<T>): RangeDataType<T>;
429 <T extends RangeableDataType>(subtype?: T): RangeDataType<T>;
430 <T extends RangeableDataType>(options: RangeDataTypeOptions<T>): RangeDataType<T>;
431}
432
433export interface RangeDataType<T extends RangeableDataType> extends AbstractDataType {
434 options: RangeDataTypeOptions<T>;
435}
436
437export interface RangeDataTypeOptions<T extends RangeableDataType> {
438 subtype?: T;
439}
440
441/**
442 * A column storing a unique universal identifier. Use with `UUIDV1` or `UUIDV4` for default values.
443 */
444export const UUID: AbstractDataTypeConstructor;
445
446/**
447 * A default unique universal identifier generated following the UUID v1 standard
448 */
449export const UUIDV1: AbstractDataTypeConstructor;
450
451/**
452 * A default unique universal identifier generated following the UUID v4 standard
453 */
454export const UUIDV4: AbstractDataTypeConstructor;
455
456/**
457 * 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.
458 *
459 * You could also use it to validate a value before permuting and storing it. Checking password length before hashing it for example:
460 * ```js
461 * class User extends Model {}
462 * User.init({
463 * password_hash: DataTypes.STRING,
464 * password: {
465 * type: DataTypes.VIRTUAL,
466 * set (val) {
467 * this.setDataValue('password', val); // Remember to set the data value, otherwise it won't be validated
468 * this.setDataValue('password_hash', this.salt + val);
469 * },
470 * validate: {
471 * isLongEnough (val) {
472 * if (val.length < 7) {
473 * throw new Error("Please choose a longer password")
474 * }
475 * }
476 * }
477 * }
478 * }, { sequelize });
479 * ```
480 *
481 * VIRTUAL also takes a return type and dependency fields as arguments
482 * If a virtual attribute is present in `attributes` it will automatically pull in the extra fields as well.
483 * Return type is mostly useful for setups that rely on types like GraphQL.
484 * ```js
485 * {
486 * active: {
487 * type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['createdAt']),
488 * get() {
489 * return this.get('createdAt') > Date.now() - (7 * 24 * 60 * 60 * 1000)
490 * }
491 * }
492 * }
493 * ```
494 *
495 * 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.
496 */
497export const VIRTUAL: VirtualDataTypeConstructor;
498
499interface VirtualDataTypeConstructor extends AbstractDataTypeConstructor {
500 new <T extends AbstractDataTypeConstructor | AbstractDataType>(ReturnType: T, fields?: string[]): VirtualDataType<
501 T
502 >;
503 <T extends AbstractDataTypeConstructor | AbstractDataType>(ReturnType: T, fields?: string[]): VirtualDataType<T>;
504}
505
506export interface VirtualDataType<T extends AbstractDataTypeConstructor | AbstractDataType> extends AbstractDataType {
507 returnType: T;
508 fields: string[];
509}
510
511/**
512 * An enumeration. `DataTypes.ENUM('value', 'another value')`.
513 */
514export const ENUM: EnumDataTypeConstructor;
515
516interface EnumDataTypeConstructor extends AbstractDataTypeConstructor {
517 new <T extends string>(...values: T[]): EnumDataType<T>;
518 new <T extends string>(options: EnumDataTypeOptions<T>): EnumDataType<T>;
519 <T extends string>(...values: T[]): EnumDataType<T>;
520 <T extends string>(options: EnumDataTypeOptions<T>): EnumDataType<T>;
521}
522
523export interface EnumDataType<T extends string> extends AbstractDataType {
524 values: T[];
525 options: EnumDataTypeOptions<T>;
526}
527
528export interface EnumDataTypeOptions<T extends string> {
529 values: T[];
530}
531
532/**
533 * An array of `type`, e.g. `DataTypes.ARRAY(DataTypes.DECIMAL)`. Only available in postgres.
534 */
535export const ARRAY: ArrayDataTypeConstructor;
536
537interface ArrayDataTypeConstructor extends AbstractDataTypeConstructor {
538 new <T extends AbstractDataTypeConstructor | AbstractDataType>(type: T): ArrayDataType<T>;
539 new <T extends AbstractDataTypeConstructor | AbstractDataType>(options: ArrayDataTypeOptions<T>): ArrayDataType<T>;
540 <T extends AbstractDataTypeConstructor | AbstractDataType>(type: T): ArrayDataType<T>;
541 <T extends AbstractDataTypeConstructor | AbstractDataType>(options: ArrayDataTypeOptions<T>): ArrayDataType<T>;
542 is<T extends AbstractDataTypeConstructor | AbstractDataType>(obj: unknown, type: T): obj is ArrayDataType<T>;
543}
544
545export interface ArrayDataType<T extends AbstractDataTypeConstructor | AbstractDataType> extends AbstractDataType {
546 options: ArrayDataTypeOptions<T>;
547}
548
549export interface ArrayDataTypeOptions<T extends AbstractDataTypeConstructor | AbstractDataType> {
550 type: T;
551}
552
553/**
554 * A geometry datatype represents two dimensional spacial objects.
555 */
556export const GEOMETRY: GeometryDataTypeConstructor;
557
558interface GeometryDataTypeConstructor extends AbstractDataTypeConstructor {
559 new (type: string, srid?: number): GeometryDataType;
560 new (options: GeometryDataTypeOptions): GeometryDataType;
561 (type: string, srid?: number): GeometryDataType;
562 (options: GeometryDataTypeOptions): GeometryDataType;
563}
564
565export interface GeometryDataType extends AbstractDataType {
566 options: GeometryDataTypeOptions;
567 type: string;
568 srid?: number;
569 escape: boolean;
570}
571
572export interface GeometryDataTypeOptions {
573 type: string;
574 srid?: number;
575}
576
577/**
578 * A geography datatype represents two dimensional spacial objects in an elliptic coord system.
579 */
580export const GEOGRAPHY: GeographyDataTypeConstructor;
581
582interface GeographyDataTypeConstructor extends AbstractDataTypeConstructor {
583 new (type: string, srid?: number): GeographyDataType;
584 new (options: GeographyDataTypeOptions): GeographyDataType;
585 (type: string, srid?: number): GeographyDataType;
586 (options: GeographyDataTypeOptions): GeographyDataType;
587}
588
589export interface GeographyDataType extends AbstractDataType {
590 options: GeographyDataTypeOptions;
591 type: string;
592 srid?: number;
593 escape: boolean;
594}
595
596export interface GeographyDataTypeOptions {
597 type: string;
598 srid?: number;
599}
600
601export const CIDR: AbstractDataTypeConstructor;
602
603export const INET: AbstractDataTypeConstructor;
604
605export const MACADDR: AbstractDataTypeConstructor;
606
607/**
608 * Case-insensitive text
609 */
610export const CITEXT: AbstractDataTypeConstructor;
611
612/**
613 * Full text search vector. Only available in postgres.
614 */
615export const TSVECTOR: AbstractDataTypeConstructor;
616
617// umzug compatibility
618export type DataTypeAbstract = AbstractDataTypeConstructor;