All files / src/associations separate-include-loader.ts

100% Statements 27/27
92.85% Branches 26/28
100% Functions 14/14
100% Lines 24/24

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            131x                           131x                                 248x               149x 138x 138x   28x 11x     24x       138x 284x 284x                 11x       53x       2x           11x 11x 8x   3x 1x   2x               24x 71x   24x        
import { Op } from '../expressions/op.ts';
import type { AnyModel } from '../model/options.ts';
import { AttributeColumns } from '../model/attribute-columns.ts';
import type { IncludeNode } from './include.ts';
import type { IncludeResolver } from './include-resolver.ts';
 
const columns = new AttributeColumns();
 
type Instance = AnyModel;
type Values = Record<string, unknown>;
type FindOptions = Readonly<Record<string, unknown>>;
 
/** Loads `separate: true` includes with one extra query each. */
export class SeparateIncludeLoader {
  readonly #resolver: IncludeResolver;
 
  /**
   * @param resolver - Converts include nodes back to options for the separate queries.
   */
  constructor(resolver: IncludeResolver) {
    this.#resolver = resolver;
  }
 
  /**
   * Loads `separate: true` includes with one extra query each and attaches them, keeping at most
   * `limit` rows per parent.
   *
   * @param parents - Instances the includes belong to.
   * @param includes - Include tree.
   * @param options - Find options (`transaction` is passed on).
   * @returns Resolves when all separate includes are attached.
   */
  async load(
    parents: readonly Instance[],
    includes: readonly IncludeNode[],
    options: FindOptions,
  ): Promise<void> {
    await Promise.all(includes.map((node) => this.#loadNode(parents, node, options)));
  }
 
  async #loadNode(
    parents: readonly Instance[],
    node: IncludeNode,
    options: FindOptions,
  ): Promise<void> {
    if (!node.separate) {
      await this.load(this.#loaded(parents, node), node.children, options);
      return;
    }
    const keys = [...new Set(parents.map((parent) => parent.get(node.association.sourceKey)))];
    const rows = (await node.model.findAll(
      this.#query(node, keys, options) as never,
    )) as Instance[];
    parents.forEach((parent) => this.#attach(parent, node, rows));
  }
 
  #loaded(parents: readonly Instance[], node: IncludeNode): Instance[] {
    return parents.flatMap((parent) => {
      const value = (parent.dataValues as Values)[node.as];
      return Array.isArray(value)
        ? (value as Instance[])
        : value === null || value === undefined
          ? []
          : [value as Instance];
    });
  }
 
  #query(node: IncludeNode, keys: readonly unknown[], options: FindOptions): Values {
    return {
      ...(options['transaction'] === undefined ? {} : { transaction: options['transaction'] }),
      ...this.#scope(node, keys),
      attributes:
        node.attributes ?? columns.stored(node.definition).map((attribute) => attribute.fieldName),
      ...(node.order === undefined ? {} : { order: node.order }),
      ...(node.children.length === 0
        ? {}
        : { include: node.children.map((child) => this.#resolver.toOptions(child)) }),
      paranoid: node.paranoid,
    };
  }
 
  #scope(node: IncludeNode, keys: readonly unknown[]): Values {
    const own = node.where === undefined ? [] : [node.where];
    if (!node.lateral) {
      return { where: { [Op.and]: [{ [node.association.foreignKey]: keys }, ...own] } };
    }
    if (node.limit === undefined || node.children.length > 0) {
      throw new Error('A lateral include needs a limit and no nested includes');
    }
    return {
      ...(own.length === 0 ? {} : { where: { [Op.and]: own } }),
      limit: node.limit,
      lateralKeys: { attribute: node.association.foreignKey, values: keys },
    };
  }
 
  #attach(parent: Instance, node: IncludeNode, rows: readonly Instance[]): void {
    const own = rows.filter(
      (row) => row.get(node.association.foreignKey) === parent.get(node.association.sourceKey),
    );
    (parent.dataValues as Values)[node.as] =
      node.limit === undefined ? own : own.slice(0, node.limit);
  }
}