All files / src/model/operations copy-operation.ts

100% Statements 25/25
95% Branches 19/20
100% Functions 6/6
100% Lines 25/25

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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                                    131x                                                         131x 131x                                   11x 11x 11x 11x 11x                               32x 32x 32x 32x               32x 32x 32x 32x       32x 32x       32x 1x   31x                         32x       4x 2x   2x      
import type { IncludeResolver } from '../../associations/include-resolver.ts';
import type { CopyEncoder } from '../../connection/copy.ts';
import type { SqlLiteral } from '../../ddl/sql-literal.ts';
import type { SqlCompiler } from '../../sql/compile.ts';
import type { AttributeColumns } from '../attribute-columns.ts';
import type { FindQueryBuilder } from '../find-query-builder.ts';
import type { ModelRegistry } from '../model-registry.ts';
import type {
  AnyModel,
  CopyFromOptions,
  CopyToFormatOptions,
  CopyToOptions,
  FindOptions,
  ModelStatic,
  Values,
} from '../options.ts';
import { CopyFromSource } from './copy-from-source.ts';
 
const COPY_FORMATS: ReadonlySet<string> = new Set(['text', 'csv', 'binary']);
 
/** Collaborators of {@link CopyOperation}. */
export type CopyOperationServices = Readonly<{
  /** Model state and scopes. */
  registry: ModelRegistry;
  /** Paranoid filter. */
  columns: AttributeColumns;
  /** Finder SELECT builder. */
  finds: FindQueryBuilder;
  /** Include trees. */
  includes: IncludeResolver;
  /** COPY text encoding of rows. */
  encoder: CopyEncoder;
  /** Escaped literals (COPY takes no parameters). */
  literals: SqlLiteral;
  /** SQL compiler. */
  compiler: SqlCompiler;
}>;
 
/** `Model.copyFrom` (`COPY ... FROM STDIN`) and `Model.copyTo` (`COPY (SELECT ...) TO STDOUT`). */
export class CopyOperation {
  readonly #services: CopyOperationServices;
  readonly #source: CopyFromSource;
 
  /**
   * @param services - Collaborators.
   */
  constructor(services: CopyOperationServices) {
    this.#services = services;
    this.#source = new CopyFromSource(services.encoder);
  }
 
  /**
   * `Model.copyFrom`: bulk-loads rows with `COPY ... FROM STDIN` (text format); timestamps are
   * filled when the model has them.
   *
   * @param model - Model class.
   * @param rows - Array, iterable or async iterable of attribute values.
   * @param options - `columns` and query options.
   * @returns Number of copied rows.
   * @throws Error without columns or for unknown attributes.
   */
  async copyFrom(
    model: ModelStatic<AnyModel>,
    rows: Iterable<Values> | AsyncIterable<Values>,
    options: CopyFromOptions,
  ): Promise<number> {
    const { definition, runtime } = this.#services.registry.state(model);
    const columns = this.#source.columns(definition, rows, options);
    const text = this.#source.sql(definition, columns.attributes);
    const lines = this.#source.lines(rows, columns, new Date());
    return runtime.copyFrom(text, lines, options);
  }
 
  /**
   * `Model.copyTo`: streams `COPY (SELECT ...) TO STDOUT` output; values of the query are inlined
   * as escaped literals.
   *
   * @param model - Model class.
   * @param options - Find options and output format.
   * @returns Async iterator of raw output chunks.
   * @throws Error for unsupported formats or multi-character delimiters / quotes.
   */
  copyTo(
    model: ModelStatic<AnyModel>,
    options: CopyToOptions,
  ): AsyncGenerator<Buffer, void, undefined> {
    const { runtime } = this.#services.registry.state(model);
    const { compiler, literals } = this.#services;
    const select = this.#select(model, options);
    return runtime.copyTo(
      `COPY (${literals.inlineValues(compiler.compile(select))}) TO STDOUT${this.#formatSql(options)}`,
      options,
      'read',
    );
  }
 
  #select(model: ModelStatic<AnyModel>, options: CopyToOptions) {
    const { definition, runtime } = this.#services.registry.state(model);
    const { registry, columns, includes, finds } = this.#services;
    const scoped = registry.scoped(model, options);
    const query = {
      ...scoped,
      where: columns.paranoid(definition, scoped.where, scoped.paranoid),
    };
    const normalized = includes.normalize(model, query.include);
    return finds.build(definition, query as FindOptions, normalized, runtime.inAsAnyThreshold);
  }
 
  #formatSql(options: CopyToFormatOptions): string {
    if (options.format !== undefined && !COPY_FORMATS.has(options.format)) {
      throw new Error(`copyTo: unsupported format ${String(options.format)}`);
    }
    const parts = [
      ...(options.format === undefined ? [] : [`FORMAT ${options.format}`]),
      ...(options.header === undefined ? [] : [`HEADER ${options.header ? 'true' : 'false'}`]),
      ...(options.delimiter === undefined
        ? []
        : [`DELIMITER ${this.#singleCharacter(options.delimiter, 'delimiter')}`]),
      ...(options.quote === undefined
        ? []
        : [`QUOTE ${this.#singleCharacter(options.quote, 'quote')}`]),
      ...(options.nullString === undefined
        ? []
        : [`NULL ${this.#services.literals.escape(options.nullString)}`]),
    ];
    return parts.length === 0 ? '' : ` WITH (${parts.join(', ')})`;
  }
 
  #singleCharacter(value: string, name: string): string {
    if ([...value].length !== 1) {
      throw new Error(`copyTo: ${name} must be a single character`);
    }
    return this.#services.literals.escape(value);
  }
}