All files / src/model/operations copy-from-source.ts

100% Statements 24/24
100% Branches 21/21
100% Functions 12/12
100% Lines 23/23

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                                                      131x                         11x 11x 20x   11x 11x 36x 1x     11x 34x   11x                 11x   14x   11x 1x   10x                     9x 18x     34x                               9x 2331x   10002x 10002x                
import type { CopyEncoder } from '../../connection/copy.ts';
import { SqlCompiler } from '../../sql/compile.ts';
import type { AttributeDefinition, ModelDefinition } from '../model-definition.ts';
import type { CopyFromOptions, Values } from '../options.ts';
 
type CopySource = Iterable<Values> | AsyncIterable<Values>;
 
/** Copied columns: attribute definitions in row order and the timestamps filled by `copyFrom`. */
export type CopyColumns = Readonly<{
  /** Attributes in row order. */
  attributes: readonly AttributeDefinition[];
  /** Timestamp attributes filled with the current time. */
  stamps: readonly string[];
}>;
 
/**
 * Columns, statement text and encoded lines of `Model.copyFrom` (`COPY ... FROM STDIN`).
 *
 * @internal
 */
export class CopyFromSource {
  readonly #encoder: CopyEncoder;
 
  /**
   * @param encoder - COPY text encoding of rows.
   */
  constructor(encoder: CopyEncoder) {
    this.#encoder = encoder;
  }
 
  /**
   * Resolves the copied columns: listed (or first-row) attributes plus missing timestamps.
   *
   * @param definition - Model definition.
   * @param rows - Row source.
   * @param options - `columns`.
   * @returns Attributes in row order and filled timestamps.
   * @throws Error without columns or for unknown attributes.
   */
  columns(definition: ModelDefinition, rows: CopySource, options: CopyFromOptions): CopyColumns {
    const listed = CopyFromSource.#listed(definition, rows, options);
    const stamps = [definition.createdAt, definition.updatedAt].filter(
      (name): name is string => name !== undefined && !listed.includes(name as never),
    );
    const columns = [...(listed as readonly string[]), ...stamps];
    columns.forEach((name) => {
      if (!definition.attributes.has(name)) {
        throw new Error(`copyFrom: unknown attribute "${name}" of model ${definition.name}`);
      }
    });
    const attributes = columns.map(
      (name) => definition.attributes.get(name) as AttributeDefinition,
    );
    return { attributes, stamps };
  }
 
  static #listed(
    definition: ModelDefinition,
    rows: CopySource,
    options: CopyFromOptions,
  ): readonly string[] {
    const listed =
      options.columns ??
      (Array.isArray(rows) && rows.length > 0
        ? Object.keys(rows[0] as Values).filter((name) => definition.attributes.has(name))
        : undefined);
    if (listed === undefined || listed.length === 0) {
      throw new Error('copyFrom: columns are required for non-array sources');
    }
    return listed;
  }
 
  /**
   * `COPY table (columns) FROM STDIN` text.
   *
   * @param definition - Model definition.
   * @param attributes - Copied attributes.
   * @returns Statement text.
   */
  sql(definition: ModelDefinition, attributes: readonly AttributeDefinition[]): string {
    const table = [definition.schema, definition.tableName]
      .filter((part): part is string => part !== undefined)
      .map(SqlCompiler.quoteIdent)
      .join('.');
    return `COPY ${table} (${attributes.map((attribute) => SqlCompiler.quoteIdent(attribute.field)).join(', ')}) FROM STDIN`;
  }
 
  /**
   * Encodes rows as COPY text lines, filling timestamps with `now`.
   *
   * @param rows - Row source.
   * @param columns - Copied columns.
   * @param now - Timestamp value.
   * @returns Async iterator of encoded lines.
   */
  async *lines(
    rows: CopySource,
    { attributes, stamps }: CopyColumns,
    now: Date,
  ): AsyncGenerator<string> {
    for await (const row of rows as AsyncIterable<Values>) {
      yield this.#encoder.encodeRow(
        attributes.map((attribute) => {
          const value = stamps.includes(attribute.fieldName) ? now : row[attribute.fieldName];
          return value === undefined || value === null || attribute.type.serialize === undefined
            ? value
            : attribute.type.serialize(value);
        }),
      );
    }
  }
}