All files / src/data-types integer-type.ts

100% Statements 7/7
100% Branches 5/5
100% Functions 4/4
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                    852x 852x 852x                   18x 18x         5x         3x      
import { AbstractDataType, type DataType } from './abstract-data-type.ts';
 
/** Integer type with Sequelize v6 MySQL-only modifiers kept as no-ops for compatibility. */
export type IntegerDataType = DataType &
  Readonly<{
    /** No-op in PostgreSQL, kept for Sequelize v6 compatibility. */
    UNSIGNED: IntegerDataType;
    /** No-op in PostgreSQL, kept for Sequelize v6 compatibility. */
    ZEROFILL: IntegerDataType;
  }>;
 
/** What the integer types take when called, like v6: a display width or an options object. */
export type IntegerArgument =
  | number
  | Readonly<{ length?: number; zerofill?: boolean; unsigned?: boolean }>;
 
/**
 * Integer types (`INTEGER`, `BIGINT`, `SMALLINT`, ...).
 *
 * @internal
 */
export class IntegerType extends AbstractDataType implements IntegerDataType {
  /** Arguments the type was created with (v6 `options`). */
  readonly options: Readonly<{
    length?: number;
    zerofill?: boolean;
    unsigned?: boolean;
    decimals?: number;
    precision?: number;
    scale?: number;
  }>;
 
  /**
   * @param key - Sequelize v6 type key.
   * @param sqlType - PostgreSQL column type.
   * @param options - Display width and `UNSIGNED` / `ZEROFILL` flags.
   */
  constructor(
    key: string,
    sqlType: string,
    options: Readonly<{ length?: number; zerofill?: boolean; unsigned?: boolean }> = {},
  ) {
    super(key, sqlType);
    this.options = Object.freeze({ ...options });
    Object.freeze(this);
  }
 
  /**
   * The same type created with arguments, like `DataTypes.INTEGER(11)`; PostgreSQL ignores them.
   *
   * @param argument - Display width or options.
   * @returns A new type.
   */
  configured(argument?: IntegerArgument): IntegerType {
    const given = typeof argument === 'number' ? { length: argument } : (argument ?? {});
    return new IntegerType(this.key, this.sqlType, { ...this.options, ...given });
  }
 
  /** `UNSIGNED` flag: recorded in `options`, no effect in PostgreSQL (kept for Sequelize v6 compatibility). */
  get UNSIGNED(): IntegerType {
    return this.configured({ unsigned: true });
  }
 
  /** `ZEROFILL` flag: recorded in `options`, no effect in PostgreSQL (kept for Sequelize v6 compatibility). */
  get ZEROFILL(): IntegerType {
    return this.configured({ zerofill: true });
  }
}