All files / src/associations belongs-to.ts

100% Statements 26/26
100% Branches 15/15
100% Functions 7/7
100% Lines 25/25

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                  131x                             178x 178x 178x                                     7x 1x   6x             2x 1x       1x 1x 2x   1x 1x   1x       6x 6x                                         3x       3x 1x   2x                                     1x       1x 1x 1x      
import { Op } from '../expressions/op.ts';
import type { AnyModel, ModelStatic, Values } from '../model/options.ts';
import { Association, type AccessorOptions } from './association.ts';
import { AssociationKeys } from './association-keys.ts';
import { OwnerKeys } from './owner-keys.ts';
import type { AssociationOptions } from './types.ts';
 
type Instance = AnyModel;
 
const ownerKeys = new OwnerKeys(new AssociationKeys());
 
/** `source.belongsTo(target)` (Sequelize v6 `BelongsTo`): the foreign key lives on the source. */
export class BelongsTo<
  S extends AnyModel = AnyModel,
  T extends AnyModel = AnyModel,
> extends Association<S, T> {
  /**
   * Adds the foreign key attribute to the source and registers the association.
   *
   * @param source - Model the association is defined on.
   * @param target - Associated model.
   * @param options - Association options.
   */
  constructor(source: ModelStatic<S>, target: ModelStatic<T>, options: AssociationOptions = {}) {
    const resolved = ownerKeys.belongsTo(source, target, options);
    super('BelongsTo', source, target, options, resolved.name, resolved.keys);
    this.install();
  }
 
  /**
   * Loads the associated instance (`getX`); given an array of source instances it loads all of them with one query.
   *
   * @param instance - Source instance, or an array of them.
   * @param options - Find options.
   * @returns Associated instance, or `null`; for an array, an object keyed by foreign key value (`'null'` for missing keys) with the instance or `null`.
   */
  get(
    instances: readonly Instance[],
    options?: AccessorOptions,
  ): Promise<Record<string, Instance | null>>;
  get(instance: Instance, options?: AccessorOptions): Promise<Instance | null>;
  get(
    instance: Instance | readonly Instance[],
    options: AccessorOptions = {},
  ): Promise<Instance | null | Record<string, Instance | null>> {
    if (Array.isArray(instance)) {
      return this.#getMany(instance as readonly Instance[], options);
    }
    return this.#getOne(instance as Instance, options);
  }
 
  async #getMany(
    instances: readonly Instance[],
    options: AccessorOptions,
  ): Promise<Record<string, Instance | null>> {
    const keys = instances.map((instance) => instance.get(this.foreignKey));
    const found = await this.target.findAll({
      ...options,
      where: this.andWhere(options.where, this.scope, { [this.targetKey]: { [Op.in]: keys } }),
    } as never);
    const result: Record<string, Instance | null> = {};
    for (const key of keys) {
      result[String(key)] = null;
    }
    for (const row of found as readonly Instance[]) {
      result[String(row.get(this.targetKey))] = row;
    }
    return result;
  }
 
  #getOne(instance: Instance, options: AccessorOptions): Promise<Instance | null> {
    const value = instance.get(this.foreignKey);
    return value === null || value === undefined
      ? Promise.resolve(null)
      : this.target.findOne({
          ...options,
          where: this.andWhere(options.where, this.scope, { [this.targetKey]: value }),
        } as never);
  }
 
  /**
   * Points the foreign key at `associated` and saves the source (`setX`).
   *
   * @param instance - Source instance.
   * @param associated - Instance, key value or `null`.
   * @param options - Query options; `save: false` skips saving.
   * @returns The saved source instance, or `undefined` with `save: false` (like v6).
   */
  async set(
    instance: Instance,
    associated: unknown,
    options: AccessorOptions & Readonly<{ save?: boolean }> = {},
  ): Promise<Instance | undefined> {
    instance.set(
      this.foreignKey,
      associated === null ? null : this.keyOf(associated, this.targetKey),
    );
    if (options.save === false) {
      return undefined;
    }
    return (await instance.save({
      ...this.passThrough(options),
      fields: [this.foreignKey],
    } as never)) as Instance;
  }
 
  /**
   * Creates the associated instance and points the foreign key at it (`createX`).
   *
   * @param instance - Source instance.
   * @param values - Values of the new instance.
   * @param options - Query options.
   * @returns The created instance.
   */
  async create(
    instance: Instance,
    values: Values = {},
    options: AccessorOptions = {},
  ): Promise<Instance> {
    const created = await this.target.create(
      { ...values, ...this.scope } as never,
      this.passThrough(options) as never,
    );
    instance.set(this.foreignKey, created.get(this.targetKey));
    await instance.save({ ...this.passThrough(options), fields: [this.foreignKey] } as never);
    return created;
  }
}