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

100% Statements 30/30
93.33% Branches 14/15
100% Functions 10/10
100% Lines 28/28

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          139x 139x 139x                                 281x   208x                   83x 83x 1x   82x 82x 82x 82x 82x 172x   82x                 79x 166x   79x       208x 2x   206x 206x 436x         281x 1x   280x 280x 588x              
import { IdentifierQuoter } from '../sql/identifier-quoter.ts';
import { AbstractDataType, type DataType, type DataTypeInput } from './abstract-data-type.ts';
import { CompositeFields } from './composite-fields.ts';
import { RecordText } from './record-text.ts';
 
const quoter = new IdentifierQuoter();
const records = new RecordText();
const fieldCodec = new CompositeFields();
 
/** Value of a composite column: an object keyed by field name. */
export type CompositeValue = Readonly<Record<string, unknown>>;
 
/**
 * Composite type (`DataTypes.COMPOSITE('address', { street: DataTypes.TEXT, zip: DataTypes.INTEGER })`);
 * values are objects keyed by field name.
 *
 * @internal
 */
export class CompositeType extends AbstractDataType {
  /** SQL type that `where` parameters are cast to. */
  readonly bindCast: string;
  /** Field types by name, in declaration order. */
  readonly fields: Readonly<Record<string, DataType>>;
  /** Converts driver record text into an object. */
  readonly parse = (value: unknown): unknown => this.#parse(value);
  /** Converts an object into record text bound as one parameter. */
  readonly serialize = (value: unknown): unknown => this.#serialize(value);
  readonly #names: readonly string[];
  readonly #name: string;
 
  /**
   * @param name - Name of the composite type (quoted as one identifier).
   * @param fields - Field types by field name.
   * @throws Error when there are no fields.
   */
  constructor(name: string, fields: Readonly<Record<string, DataTypeInput>>) {
    const names = Object.keys(fields);
    if (names.length === 0) {
      throw new Error('COMPOSITE needs at least one field');
    }
    super('COMPOSITE', quoter.quote(name));
    this.bindCast = this.sqlType;
    this.#name = name;
    this.#names = names;
    this.fields = Object.fromEntries(
      names.map((field) => [field, AbstractDataType.from(fields[field] as DataTypeInput)]),
    );
    Object.freeze(this);
  }
 
  /**
   * SQL that creates the type; run it once with `sequelize.query()` before `sync()`.
   *
   * @returns `CREATE TYPE "name" AS ("field" TYPE, …)` with quoted names.
   */
  createSql(): string {
    const columns = this.#names.map(
      (field) => `${quoter.quote(field)} ${this.fields[field]?.sqlType ?? ''}`,
    );
    return `CREATE TYPE ${quoter.quote(this.#name)} AS (${columns.join(', ')})`;
  }
 
  #serialize(value: unknown): unknown {
    if (typeof value !== 'object' || value === null || Array.isArray(value)) {
      return value;
    }
    const source = value as CompositeValue;
    return records.format(
      this.#names.map((field) => fieldCodec.toText(this.fields[field] as DataType, source[field])),
    );
  }
 
  #parse(value: unknown): unknown {
    if (typeof value !== 'string' || !value.startsWith('(')) {
      return value;
    }
    const texts = records.parse(value);
    return Object.fromEntries(
      this.#names.map((field, index) => [
        field,
        fieldCodec.fromText(this.fields[field] as DataType, texts[index] ?? null),
      ]),
    );
  }
}