All files / src/associations nested-creator.ts

100% Statements 21/21
84.21% Branches 16/19
100% Functions 9/9
100% Lines 19/19

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              131x                                 131x 131x 131x                     19x 13x                                       3020x                               1521x       19x 3x   16x       2x   19x 19x       16x 16x 16x             1x 1x            
import type { AnyModel, SaveOptions, Values } from '../model/options.ts';
import type { IncludeNode } from './include.ts';
import type { IncludeResolver } from './include-resolver.ts';
import { NestedSaver, type InstanceCheck } from './nested-saver.ts';
 
export type { InstanceCheck } from './nested-saver.ts';
 
const toArray = (value: unknown): readonly unknown[] => (Array.isArray(value) ? value : [value]);
 
/**
 * Nested creation (`Model.create(values, { include })`, Sequelize v6): builds nested instances
 * from the values, saves belongsTo parents before the instance and hasOne / hasMany /
 * belongsToMany children (with their junction rows) after it.
 */
export class NestedCreator {
  readonly #resolver: IncludeResolver;
  readonly #isInstance: InstanceCheck;
  readonly #saver: NestedSaver;
 
  /**
   * @param resolver - Converts include nodes back to options for the nested builds.
   * @param isInstance - Checks whether a value is a model instance.
   */
  constructor(resolver: IncludeResolver, isInstance: InstanceCheck) {
    this.#resolver = resolver;
    this.#isInstance = isInstance;
    this.#saver = new NestedSaver(isInstance);
  }
 
  /**
   * Builds the nested instance(s) of an include from the given values; instances are kept.
   *
   * @param node - Include node.
   * @param value - Values (a list for multi associations), an instance, or `null`.
   * @returns Nested instance(s), or `null`.
   */
  build(node: IncludeNode, value: unknown): unknown {
    const build = (item: unknown): unknown => this.#buildOne(node, item);
    return value === null
      ? null
      : node.association.isMultiAssociation
        ? toArray(value).map(build)
        : build(value);
  }
 
  /**
   * Saves nested belongsTo parents and sets the foreign keys on the instance.
   *
   * @param instance - Instance being created.
   * @param includes - Include tree of the instance.
   * @param options - Save options (query options are passed on).
   * @returns Resolves when the parents are saved.
   */
  saveBelongsTo(
    instance: AnyModel,
    includes: readonly IncludeNode[],
    options: SaveOptions,
  ): Promise<void> {
    return this.#saver.saveBelongsTo(instance, includes, options);
  }
 
  /**
   * Saves nested hasOne / hasMany / belongsToMany children after the instance, one at a time.
   *
   * @param instance - Saved instance.
   * @param includes - Include tree of the instance.
   * @param options - Save options (query options are passed on).
   * @returns Resolves when the children are saved.
   */
  saveChildren(
    instance: AnyModel,
    includes: readonly IncludeNode[],
    options: SaveOptions,
  ): Promise<void> {
    return this.#saver.saveChildren(instance, includes, options);
  }
 
  #buildOne(node: IncludeNode, item: unknown): unknown {
    if (this.#isInstance(item)) {
      return item;
    }
    const child = new node.model(
      item as Values,
      node.children.length === 0
        ? {}
        : { include: node.children.map((nested) => this.#resolver.toOptions(nested)) },
    );
    this.#attachThrough(child, node, item);
    return child;
  }
 
  #attachThrough(child: AnyModel, node: IncludeNode, item: unknown): void {
    const through = node.association.through?.model;
    const throughName = node.through?.definition.name;
    if (
      through !== undefined &&
      throughName !== undefined &&
      typeof item === 'object' &&
      item !== null &&
      throughName in item
    ) {
      const throughValue = (item as Values)[throughName];
      (child.dataValues as Values)[throughName] = this.#isInstance(throughValue)
        ? throughValue
        : new through(throughValue as Values);
    }
  }
}