All files / src/associations association-accessors.ts

100% Statements 14/14
90% Branches 9/10
100% Functions 8/8
100% Lines 14/14

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              131x                                             521x 521x 521x                                 517x 517x 3588x 3588x       3462x                   1042x       227x       294x                                   3462x 67x        
import type { AnyModel } from '../model/options.ts';
import type { AssociationName, AssociationType } from './types.ts';
 
type Instance = AnyModel;
type AccessorImplementation = (instance: Instance, ...args: never[]) => unknown;
 
/** Accessor key (see {@link Association.accessors}) → association method implementing it. */
const ACCESSOR_METHODS: Readonly<Record<string, string>> = {
  get: 'get',
  set: 'set',
  create: 'create',
  add: 'add',
  addMultiple: 'add',
  remove: 'remove',
  removeMultiple: 'remove',
  hasSingle: 'has',
  hasAll: 'has',
  count: 'count',
};
 
/** Names of the accessor methods of an association and their installation on a prototype. */
export class AssociationAccessors {
  /**
   * Accessor method names of an association.
   *
   * @param type - Kind of association.
   * @param name - Singular and plural alias forms.
   * @returns Accessor key → method name.
   */
  namesFor(type: AssociationType, name: AssociationName): Readonly<Record<string, string>> {
    const singular = this.#upperFirst(name.singular);
    const plural = this.#upperFirst(name.plural);
    return type === 'BelongsTo' || type === 'HasOne'
      ? this.#single(singular)
      : this.#multi(singular, plural);
  }
 
  /**
   * Installs the accessor methods on a model prototype, leaving existing methods untouched.
   *
   * @param association - Association whose methods implement the accessors.
   * @param prototype - Source model prototype.
   * @param accessors - Accessor key → method name.
   */
  install(
    association: object,
    prototype: object,
    accessors: Readonly<Record<string, string>>,
  ): void {
    const methods = association as Readonly<Record<string, unknown>>;
    Object.entries(accessors).forEach(([key, methodName]) => {
      const implementation = methods[ACCESSOR_METHODS[key] ?? ''];
      if (
        typeof implementation === 'function' &&
        !Object.prototype.hasOwnProperty.call(prototype, methodName)
      ) {
        Object.defineProperty(prototype, methodName, {
          configurable: true,
          writable: true,
          value: this.#instanceMethod(association, implementation as AccessorImplementation),
        });
      }
    });
  }
 
  #upperFirst(value: string): string {
    return value.charAt(0).toUpperCase() + value.slice(1);
  }
 
  #single(singular: string): Readonly<Record<string, string>> {
    return { get: `get${singular}`, set: `set${singular}`, create: `create${singular}` };
  }
 
  #multi(singular: string, plural: string): Readonly<Record<string, string>> {
    return {
      get: `get${plural}`,
      set: `set${plural}`,
      addMultiple: `add${plural}`,
      add: `add${singular}`,
      create: `create${singular}`,
      remove: `remove${singular}`,
      removeMultiple: `remove${plural}`,
      hasSingle: `has${singular}`,
      hasAll: `has${plural}`,
      count: `count${plural}`,
    };
  }
 
  #instanceMethod(
    association: object,
    implementation: AccessorImplementation,
  ): (this: Instance, ...args: never[]) => unknown {
    return function accessor(this: Instance, ...args: never[]): unknown {
      return implementation.call(association, this, ...args);
    };
  }
}