All files / src/query-interface query-generator.ts

66.66% Statements 2/3
66.66% Branches 2/3
100% Functions 1/1
66.66% Lines 2/3

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                                                                1x         1x      
import type { FindOptions, ModelStatic, AnyModel } from '../model/options.ts';
import { finder } from '../model/model-operations.ts';
 
/**
 * Sequelize v6 `queryInterface.queryGenerator`: renders finder options to SQL text. Only
 * {@link QueryGenerator.selectQuery} is provided; postgrelize compiles its SQL through its own AST.
 *
 * @example
 * ```ts
 * const sql = sequelize.getQueryInterface().queryGenerator.selectQuery('users', { where: { id: 1 } }, User);
 * ```
 */
export class QueryGenerator {
  /**
   * Renders the SELECT of a finder without running it. Values are written into the text as escaped
   * literals like v6. No scope, `paranoid` filter or hook is applied, as in v6.
   *
   * @param tableName - Table name; ignored, the table of `model` is used.
   * @param options - Finder options (`attributes`, `where`, `include`, `order`, `limit`, ...).
   * @param model - Model whose table is selected from.
   * @returns SQL ending with `;`.
   * @throws Error when `model` is missing.
   * @example
   * ```ts
   * const sql = sequelize.getQueryInterface().queryGenerator.selectQuery('users', { where: { id: 1 } }, User);
   * ```
   */
  selectQuery(
    tableName: unknown,
    options: FindOptions = {},
    model?: ModelStatic<AnyModel>,
  ): string {
    Iif (model === undefined) {
      throw new Error(
        `selectQuery needs the model of "${String(tableName)}" as the third argument`,
      );
    }
    return finder.selectSql(model, options);
  }
}