All files / src/query-interface statement-runner.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 6/6
100% Lines 6/6

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                                      3096x                     5474x                     4944x 3225x                   506x 518x          
import type { QueryResult } from '../connection/connection-manager.ts';
import { runtimeKey, type QueryOptionsBase, type RuntimeOwner } from '../model/runtime.ts';
import type { CompiledQuery } from '../sql/compile.ts';
 
/** Result rows of a statement. */
export type StatementRows = readonly Readonly<Record<string, unknown>>[];
 
/**
 * Runs schema statements through the runtime of a `Sequelize` instance, one after another.
 *
 * @internal
 */
export class StatementRunner {
  readonly #owner: RuntimeOwner;
 
  /**
   * @param owner - Owning `Sequelize` instance.
   */
  constructor(owner: RuntimeOwner) {
    this.#owner = owner;
  }
 
  /**
   * Runs a compiled query.
   *
   * @param query - Compiled query.
   * @param options - Query options.
   * @returns Query result.
   */
  async execute(query: CompiledQuery, options: QueryOptionsBase): Promise<QueryResult> {
    return await this.#owner[runtimeKey].execute(query, options);
  }
 
  /**
   * Runs one statement without parameters.
   *
   * @param sql - SQL statement.
   * @param options - Query options.
   * @returns Result rows.
   */
  async run(sql: string, options: QueryOptionsBase = {}): Promise<StatementRows> {
    const result = await this.execute({ text: sql, values: [] }, options);
    return result.rows;
  }
 
  /**
   * Runs statements in order, each after the previous one finished.
   *
   * @param statements - SQL statements.
   * @param options - Query options.
   */
  async runAll(statements: readonly string[], options: QueryOptionsBase = {}): Promise<void> {
    await statements.reduce<Promise<unknown>>(
      (previous, sql) => previous.then(() => this.run(sql, options)),
      Promise.resolve(),
    );
  }
}