All files / src/associations include-sub-query.ts

96.15% Statements 25/26
95% Branches 19/20
100% Functions 13/13
95.23% Lines 20/21

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          131x   131x   131x 67x     131x 12x     131x       111x 82x   29x 38x                     131x                                 111x             25x                             5x   3x     5x 5x     5x             5x      
import type { ModelDefinition } from '../model/model-definition.ts';
import { SqlAst, type ExprNode, type SelectNode } from '../sql/ast.ts';
import type { IncludeNode } from './include.ts';
import type { IncludeJoins } from './include-joins.ts';
 
const ast = new SqlAst();
 
const isMulti = (node: IncludeNode): boolean => node.association.isMultiAssociation;
 
const aliasesOf = (nodes: readonly IncludeNode[]): readonly string[] =>
  nodes.flatMap((node) => [node.alias, ...aliasesOf(node.children)]);
 
/** v6 `hasRequired`: the include or one of its direct (non-separate) children is required. */
export const hasRequired = (node: IncludeNode): boolean =>
  node.required || node.children.some((child) => !child.separate && child.required);
 
/** True when a condition references a joined include (`$Include.attr$`) as a table. */
const referencesIncludes = (
  where: ExprNode | undefined,
  includes: readonly IncludeNode[],
): boolean => {
  if (where === undefined) {
    return false;
  }
  const text = JSON.stringify(where);
  return aliasesOf(includes).some((alias) => text.includes(`"path":[${JSON.stringify(alias)},`));
};
 
/** Decides the v6 sub-query form of a find with includes and builds its `EXISTS` filters. */
export class IncludeSubQuery {
  readonly #joins: IncludeJoins;
 
  /**
   * @param joins - Builds the joins the `EXISTS` filters are made of.
   */
  constructor(joins: IncludeJoins) {
    this.#joins = joins;
  }
 
  /**
   * Whether `limit`/`offset` must go into a subquery of the main table (v6 rules). A top-level
   * `where` that references a joined include cannot run inside that subquery, so it disables it.
   *
   * @param includes - Joined includes.
   * @param options - Limit / offset / subQuery of the find.
   * @param where - Compiled top-level condition.
   * @returns True for the sub-query form.
   */
  needsSubQuery(
    includes: readonly IncludeNode[],
    options: Readonly<{ limit?: number | null; offset?: number | null; subQuery?: boolean }>,
    where?: ExprNode,
  ): boolean {
    return (
      options.subQuery !== false &&
      !referencesIncludes(where, includes) &&
      ((options.limit !== undefined && options.limit !== null) ||
        (options.offset !== undefined && options.offset !== null)) &&
      includes.some(
        (node) =>
          !node.separate && (isMulti(node) || node.children.some((child) => isMulti(child))),
      )
    );
  }
 
  /**
   * `EXISTS` condition that keeps the rows of the main table having a matching required include
   * (also of a required include nested under an optional one, v6 `subQueryFilter`).
   *
   * @param parent - Main model definition.
   * @param parentAlias - Main table alias.
   * @param node - Required include.
   * @returns Condition.
   */
  existsFilter(parent: ModelDefinition, parentAlias: string, node: IncludeNode): ExprNode {
    const joins = this.#joins.joins(parent, parentAlias, {
      ...node,
      children: node.children.filter((child) => child.required),
      required: true,
    });
    const [first, ...rest] = joins;
    Iif (first === undefined) {
      return ast.raw('TRUE');
    }
    const query: SelectNode = {
      kind: 'select',
      columns: [ast.raw('1')],
      from: first.table,
      ...(rest.length === 0 ? {} : { joins: rest }),
      where: first.on,
    };
    return { kind: 'prefix', op: 'EXISTS', expr: { kind: 'subquery', query } };
  }
}