UNPKG

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