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

100% Statements 9/9
100% Branches 8/8
100% Functions 4/4
100% Lines 9/9

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                                                                          7609x 7609x                 5x                     464x 85x   379x                   8617x 1x   8616x      
/**
 * Description of a column type: its SQL type and conversions between JavaScript values and the
 * values sent to / received from PostgreSQL.
 */
export type DataType = Readonly<{
  /** Sequelize v6 type key, e.g. `'STRING'`. */
  key: string;
  /** PostgreSQL column type, e.g. `'VARCHAR(255)'`. */
  sqlType: string;
  /** True for `JSON` and `JSONB`; enables nested JSON where conditions. */
  isJson?: boolean;
  /** True for `VIRTUAL`: the attribute is not stored in the database. */
  isVirtual?: boolean;
  /** Element type for `ARRAY` and `RANGE`. */
  subtype?: DataType;
  /** Allowed values for `ENUM`. */
  values?: readonly string[];
  /** SQL type that `where` parameters of this type are cast to (types PostgreSQL cannot infer from an untyped parameter). */
  bindCast?: string;
  /** Converts a value read from the driver into the attribute value. */
  parse?: (value: unknown) => unknown;
  /** Converts an attribute value into a driver parameter. */
  serialize?: (value: unknown) => unknown;
}>;
 
/** A data type used as is (`DataTypes.INTEGER`) or after calling it (`DataTypes.STRING(64)`), or a raw PostgreSQL type name (`'vector(384)'`) like in Sequelize v6. */
export type DataTypeInput = string | DataType | ((...args: never[]) => DataType);
 
/**
 * Base of the built-in data type classes. Conversions (`parse`, `serialize`) are own function
 * properties, present only on types that convert values, so they can be read and called detached.
 *
 * @internal
 */
export abstract class AbstractDataType implements DataType {
  /** Sequelize v6 type key, e.g. `'STRING'`. */
  readonly key: string;
  /** PostgreSQL column type, e.g. `'VARCHAR(255)'`. */
  readonly sqlType: string;
 
  /**
   * @param key - Sequelize v6 type key.
   * @param sqlType - PostgreSQL column type.
   */
  constructor(key: string, sqlType: string) {
    this.key = key;
    this.sqlType = sqlType;
  }
 
  /**
   * PostgreSQL column type, like Sequelize v6 `toSql()`.
   *
   * @returns The SQL type, e.g. `'VARCHAR(255)'`.
   */
  toSql(): string {
    return this.sqlType;
  }
 
  /**
   * Validates a numeric type parameter (length, precision, scale, srid) before it reaches SQL.
   *
   * @param name - Parameter name for the error message.
   * @param value - Parameter value.
   * @returns The value.
   */
  protected static integer(name: string, value: number): number {
    if (!Number.isInteger(value) || value < 0) {
      throw new Error(`${name} must be a non-negative integer, got ${String(value)}`);
    }
    return value;
  }
 
  /**
   * Resolves a data type given bare or called (`DataTypes.STRING` / `DataTypes.STRING(64)`).
   *
   * @param input - Data type or callable data type.
   * @returns The data type; a callable type carries the properties of its default form.
   */
  static from(input: DataTypeInput): DataType {
    if (typeof input === 'string') {
      return { key: input, sqlType: input };
    }
    return typeof input === 'function' ? (input as unknown as DataType) : input;
  }
}