All files / src/model find-query-builder.ts

100% Statements 14/14
100% Branches 12/12
100% Functions 5/5
100% Lines 14/14

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                                  131x               131x 131x 131x                                       4643x 2x   4641x 4643x             4643x                   3932x                   4641x                                   2x 2x             2x      
import type { IncludeNode } from '../associations/include.ts';
import type { IncludeQueryBuilder } from '../associations/include-query-builder.ts';
import type { SelectNode } from '../sql/ast.ts';
import { LateralKeysBuilder, type LateralKeys } from './lateral-keys-builder.ts';
import type { CteBuilder } from './cte-builder.ts';
import type { ModelDefinition } from './model-definition.ts';
import type { FindOptions } from './options.ts';
import type { SelectOptions, SelectQueryBuilder } from './select-query-builder.ts';
 
/**
 * Builds the SELECT of a finder from resolved find options: the `source` / `with` / `from` CTE
 * options and the joins of includes (whose ordering is applied by the include builder).
 */
export class FindQueryBuilder {
  readonly #selects: SelectQueryBuilder;
  readonly #cte: CteBuilder;
  readonly #includes: IncludeQueryBuilder;
  readonly #laterals = new LateralKeysBuilder();
 
  /**
   * @param selects - SELECT builder.
   * @param cte - CTE options.
   * @param includes - Include joins.
   */
  constructor(selects: SelectQueryBuilder, cte: CteBuilder, includes: IncludeQueryBuilder) {
    this.#selects = selects;
    this.#cte = cte;
    this.#includes = includes;
  }
 
  /**
   * Builds the SELECT of resolved find options (scope and paranoid filter already applied).
   *
   * @param definition - Model definition.
   * @param query - Find options.
   * @param includes - Normalized include tree.
   * @param inAsAnyThreshold - Lists at least this long compile to `= ANY($n)`.
   * @param alias - Table alias instead of the model name.
   * @returns SELECT node.
   */
  build(
    definition: ModelDefinition,
    query: FindOptions,
    includes: readonly IncludeNode[],
    inAsAnyThreshold: number | undefined,
    alias?: string,
  ): SelectNode {
    if (query.lateralKeys !== undefined) {
      return this.#lateral(definition, query, inAsAnyThreshold, alias);
    }
    const tableAs = includes.length === 0 ? query.tableAs : undefined;
    const base = this.#base(
      definition,
      query,
      includes.length > 0,
      inAsAnyThreshold,
      alias ?? tableAs,
    );
    return includes.length === 0 ? base : this.#includes.build(definition, base, includes, query);
  }
 
  /**
   * Route of the built SELECT: the writer when a CTE modifies data.
   *
   * @param select - Built SELECT.
   * @returns `'read'` or `'write'`.
   */
  route(select: SelectNode): 'read' | 'write' {
    return this.#cte.route(select);
  }
 
  #base(
    definition: ModelDefinition,
    query: FindOptions,
    joined: boolean,
    inAsAnyThreshold: number | undefined,
    alias: string | undefined,
  ): SelectNode {
    return this.#selects.build(
      this.#cte.sourceDefinition(definition, query.source),
      {
        ...(joined ? { ...query, order: undefined } : query),
        ...this.#cte.selectOptions(query),
      } as SelectOptions,
      inAsAnyThreshold,
      undefined,
      alias,
    );
  }
 
  #lateral(
    definition: ModelDefinition,
    query: FindOptions,
    inAsAnyThreshold: number | undefined,
    alias: string | undefined,
  ): SelectNode {
    const name = alias ?? definition.name;
    const inner = this.#selects.build(
      definition,
      query as SelectOptions,
      inAsAnyThreshold,
      undefined,
      name,
    );
    return this.#laterals.build(definition, inner, query.lateralKeys as LateralKeys, name);
  }
}