All files / src/associations include-order.ts

94.23% Statements 49/52
85.71% Branches 42/49
100% Functions 15/15
93.87% Lines 46/49

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 137                131x 131x 131x             131x 26x                                     111x 41x   70x 70x 111x 111x 93x 93x                     93x 93x 26x     67x     93x         93x     93x       26x 26x     26x 26x 26x   1x         26x 26x 26x 26x 26x     26x 26x                 26x   29x 29x             29x       29x 5x   24x 20x   4x 2x   2x     2x 2x      
import { SqlExpression } from '../expressions/methods.ts';
import type { ModelDefinition } from '../model/model-definition.ts';
import { AttributeColumns } from '../model/attribute-columns.ts';
import { SelectQueryBuilder } from '../model/select-query-builder.ts';
import { SqlAst, type OrderNode } from '../sql/ast.ts';
import { OrderDirection } from '../sql/order.ts';
import type { IncludeNode, IncludeOptions } from './include.ts';
 
const ast = new SqlAst();
const attributeColumns = new AttributeColumns();
const selects = new SelectQueryBuilder(attributeColumns);
 
type WhereContext = ReturnType<AttributeColumns['whereCompiler']>;
 
/** ORDER BY nodes: all of them, and those that belong inside the main-table subquery. */
export type OrderNodes = Readonly<{ all: readonly OrderNode[]; main: readonly OrderNode[] }>;
 
const field = (definition: ModelDefinition, name: string): string =>
  definition.attributes.get(name)?.field ?? name;
 
/** Resolves `order` items that reference an include (`[Include, 'attr', 'DESC']`). */
export class IncludeOrder {
  /**
   * Order nodes of a find, resolving items that go through includes.
   *
   * @param definition - Main model definition.
   * @param includes - Joined includes.
   * @param order - `order` option.
   * @param mainAlias - Main table alias.
   * @returns All order nodes and the ones for the subquery.
   */
  nodes(
    definition: ModelDefinition,
    includes: readonly IncludeNode[],
    order: unknown,
    mainAlias: string,
  ): OrderNodes {
    if (order === undefined || order === null) {
      return { all: [], main: [] };
    }
    const ctx = attributeColumns.whereCompiler(definition, mainAlias);
    const items: readonly unknown[] = Array.isArray(order) ? order : [order];
    const resolved = items.map((item) => this.#resolve(definition, includes, item, mainAlias, ctx));
    return {
      all: resolved.map((entry) => entry.node),
      main: resolved.filter((entry) => entry.main).map((entry) => entry.node),
    };
  }
 
  #resolve(
    definition: ModelDefinition,
    includes: readonly IncludeNode[],
    item: unknown,
    mainAlias: string,
    ctx: WhereContext,
  ): Readonly<{ node: OrderNode; main: boolean }> {
    const viaInclude = this.#viaInclude(includes, item);
    if (viaInclude !== undefined) {
      return { node: viaInclude, main: false };
    }
    const node =
      item instanceof SqlExpression
        ? { expr: attributeColumns.where(item, ctx) ?? ast.raw('1') }
        : selects.orderItem(definition, mainAlias, ctx, item);
    return { node, main: true };
  }
 
  #viaInclude(includes: readonly IncludeNode[], item: unknown): OrderNode | undefined {
    const throughInclude =
      Array.isArray(item) &&
      (item.length > 2 ||
        (item.length === 2 && typeof item[0] !== 'string' && !(item[0] instanceof SqlExpression)));
    return throughInclude ? this.#includeOrder(includes, item as readonly unknown[]) : undefined;
  }
 
  #hasDirection(item: readonly unknown[]): boolean {
    const last = item[item.length - 1];
    Iif (typeof last !== 'string') {
      return false;
    }
    try {
      OrderDirection.parse(last);
      return true;
    } catch {
      return false;
    }
  }
 
  #includeOrder(includes: readonly IncludeNode[], item: readonly unknown[]): OrderNode | undefined {
    const hasDirection = this.#hasDirection(item);
    const parts = hasDirection ? item.slice(0, -1) : item;
    const attribute = parts.at(-1);
    const references = parts.slice(0, -1);
    Iif (references.length === 0 || typeof attribute !== 'string') {
      return undefined;
    }
    const node = this.#walk(includes, references);
    return node === undefined
      ? undefined
      : {
          expr: ast.ident(node.alias, field(node.definition, attribute)),
          ...(hasDirection ? OrderDirection.parse(item[item.length - 1]) : {}),
        };
  }
 
  #walk(includes: readonly IncludeNode[], references: readonly unknown[]): IncludeNode | undefined {
    return references.reduce<Readonly<{ nodes: readonly IncludeNode[]; node?: IncludeNode }>>(
      (acc, reference) => {
        const found = this.#find(acc.nodes, reference);
        return { nodes: found?.children ?? [], node: found };
      },
      { nodes: includes },
    ).node;
  }
 
  #find(nodes: readonly IncludeNode[], reference: unknown): IncludeNode | undefined {
    return nodes.find((node) => this.#matches(node, reference));
  }
 
  #matches(node: IncludeNode, reference: unknown): boolean {
    if (typeof reference === 'string') {
      return node.as === reference;
    }
    if (typeof reference === 'function') {
      return node.model === reference;
    }
    if (node.association === reference) {
      return true;
    }
    Iif (typeof reference !== 'object' || reference === null || !('model' in reference)) {
      return false;
    }
    const options = reference as IncludeOptions;
    return node.model === options.model && (options.as === undefined || node.as === options.as);
  }
}