All files / src/decorators association-applier.ts

100% Statements 21/21
94.11% Branches 16/17
100% Functions 7/7
100% Lines 21/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            131x 26x                           18x 18x 18x 7x         11x         14x 14x       18x   5x   7x   6x         7x 7x 7x 2x   5x             21x 26x   21x      
import type { AssociationOptions, BelongsToManyOptions } from '../associations/types.ts';
import type { AnyModel, ModelStatic } from '../model/options.ts';
import { metadata, type AssociationSpec } from './model-metadata.ts';
 
type Model = ModelStatic<AnyModel>;
 
const references = (target: Model, referenced: Model): boolean =>
  referenced === target || referenced.prototype instanceof target;
 
/**
 * Applies the associations collected by decorators with the same `hasOne` / `hasMany` /
 * `belongsTo` / `belongsToMany` calls an application would write by hand.
 *
 * @internal
 */
export class AssociationApplier {
  /**
   * @param model - Model that owns the property the association was declared on.
   * @param spec - Collected association.
   */
  apply(model: Model, spec: AssociationSpec): void {
    const target = spec.target();
    const options = { as: spec.key, ...this.#keys(model, target, spec), ...spec.options };
    if (spec.kind === 'belongsToMany') {
      model.belongsToMany(target, {
        ...options,
        through: this.#through(spec),
      } as BelongsToManyOptions);
    } else {
      model[spec.kind](target, options as AssociationOptions);
    }
  }
 
  #through(spec: AssociationSpec): BelongsToManyOptions['through'] {
    const { through } = spec;
    return typeof through === 'function' ? through() : (through as never);
  }
 
  #keys(model: Model, target: Model, spec: AssociationSpec): AssociationOptions {
    switch (spec.kind) {
      case 'belongsTo':
        return this.#key('foreignKey', model, target);
      case 'belongsToMany':
        return this.#manyKeys(model, target, spec);
      default:
        return this.#key('foreignKey', target, model);
    }
  }
 
  #manyKeys(model: Model, target: Model, spec: AssociationSpec): Record<string, string> {
    const through = this.#through(spec);
    const junction = typeof through === 'object' && 'model' in through ? through.model : through;
    if (typeof junction !== 'function') {
      return {};
    }
    return {
      ...this.#key('foreignKey', junction as Model, model),
      ...this.#key('otherKey', junction as Model, target),
    };
  }
 
  #key(name: string, holder: Model, referenced: Model): Record<string, string> {
    const found = Object.entries(metadata.collect(holder).foreignKeys).find(([, getter]) =>
      references(getter(), referenced),
    );
    return found === undefined ? {} : { [name]: found[0] };
  }
}