All files / src/model instance-destroyer.ts

100% Statements 41/41
87.5% Branches 21/24
100% Functions 8/8
100% Lines 40/40

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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                                                  131x 131x                           26x 26x 26x 26x 26x   25x 25x 26x     24x 24x   24x                             5x 5x 5x 5x 3x   4x 4x 4x 3x   4x                     3x 3x       8x 8x 1x   7x               10x 10x         15x 15x 5x 5x   3x   4x                   15x 15x 14x                      
import type { InstanceLifecycleServices } from './instance-lifecycle-services.ts';
import type { InstanceLifecycle } from './instance-lifecycle.ts';
import type { ModelState } from './model-registry.ts';
import type { AnyModel, InstanceDestroyOptions, MutableOptions, Values } from './options.ts';
import type { QueryOptionsBase } from './runtime.ts';
 
type CascadeAssociation = Readonly<{
  options: Readonly<{ onDelete?: string; hooks?: boolean }>;
  get(instance: AnyModel, options: object): Promise<AnyModel | AnyModel[] | null>;
}>;
 
/**
 * Instance `destroy` (DELETE, or soft delete of paranoid models), `restore` and `isSoftDeleted`.
 *
 * @internal
 */
export class InstanceDestroyer {
  readonly #services: InstanceLifecycleServices;
  readonly #lifecycle: InstanceLifecycle;
 
  /**
   * @param services - Collaborators.
   * @param lifecycle - Hook runner.
   */
  constructor(services: InstanceLifecycleServices, lifecycle: InstanceLifecycle) {
    this.#services = services;
    this.#lifecycle = lifecycle;
  }
 
  /**
   * Deletes the row of an instance (sets `deletedAt` for paranoid models unless `force`).
   *
   * @param instance - Instance.
   * @param options - Destroy options.
   * @returns The instance after a soft delete (like v6), nothing after a DELETE.
   */
  async destroy(
    instance: AnyModel,
    options: InstanceDestroyOptions,
  ): Promise<AnyModel | undefined> {
    const model = instance.constructor;
    const state = this.#services.registry.state(model);
    const opts: MutableOptions = { hooks: true, force: false, ...options };
    Eif (opts['hooks'] === true) {
      await this.#lifecycle.runHooks(model, 'beforeDestroy', instance, opts);
    }
    const { deletedAt } = state.definition;
    const soft = deletedAt !== undefined && opts['force'] !== true;
    await (soft
      ? this.#softDelete(instance, deletedAt, options)
      : this.#deleteRow(instance, state, options));
    Eif (opts['hooks'] === true) {
      await this.#lifecycle.runHooks(model, 'afterDestroy', instance, opts);
    }
    return soft ? instance : undefined;
  }
 
  /**
   * Restores a soft-deleted instance of a paranoid model.
   *
   * @param instance - Instance.
   * @param options - Query options; `hooks: false` skips restore hooks.
   * @returns The restored instance (like v6).
   * @throws Error when the model is not paranoid.
   */
  async restore(
    instance: AnyModel,
    options: QueryOptionsBase & Readonly<{ hooks?: boolean }>,
  ): Promise<AnyModel> {
    const model = instance.constructor;
    const deletedAt = this.#deletedAt(model);
    const hooks = options.hooks !== false;
    if (hooks) {
      await this.#lifecycle.runHooks(model, 'beforeRestore', instance, options);
    }
    instance.setDataValue(deletedAt, null);
    await instance.save({ ...this.#services.values.queryOptions(options), hooks: false });
    if (hooks) {
      await this.#lifecycle.runHooks(model, 'afterRestore', instance, options);
    }
    return instance;
  }
 
  /**
   * Whether an instance of a paranoid model is soft-deleted.
   *
   * @param instance - Instance.
   * @returns True when `deletedAt` is set.
   * @throws Error when the model is not paranoid.
   */
  isSoftDeleted(instance: AnyModel): boolean {
    const value = (instance.dataValues as Values)[this.#deletedAt(instance.constructor)];
    return value !== null && value !== undefined;
  }
 
  #deletedAt(model: object): string {
    const { definition } = this.#services.registry.state(model);
    if (definition.deletedAt === undefined) {
      throw new Error('Model is not paranoid');
    }
    return definition.deletedAt;
  }
 
  async #softDelete(
    instance: AnyModel,
    deletedAt: string,
    options: InstanceDestroyOptions,
  ): Promise<void> {
    instance.setDataValue(deletedAt, new Date());
    await instance.save({ ...this.#services.values.queryOptions(options), hooks: false });
  }
 
  // `onDelete: 'CASCADE'` with `hooks: true`: destroy the associated rows one by one, with their hooks (v6).
  async #cascade(instance: AnyModel, options: InstanceDestroyOptions): Promise<void> {
    const { associations } = this.#services.registry.state(instance.constructor);
    for (const association of associations.values() as Iterable<CascadeAssociation>) {
      const { onDelete, hooks } = association.options;
      if (hooks === true && onDelete?.toLowerCase() === 'cascade') {
        // oxlint-disable-next-line no-await-in-loop -- one association after another, as in v6.
        const found = await association.get(instance, options);
        // oxlint-disable-next-line no-await-in-loop -- each row is destroyed with its hooks, one by one.
        for (const row of found === null ? [] : [found].flat()) await row.destroy(options);
      }
    }
  }
 
  async #deleteRow(
    instance: AnyModel,
    { definition, runtime }: ModelState,
    options: InstanceDestroyOptions,
  ): Promise<void> {
    const { compiler, deletes, columns } = this.#services;
    await this.#cascade(instance, options);
    await runtime.execute(
      compiler.compile(
        deletes.build(
          definition,
          columns.primaryKeyWhere(definition, instance.dataValues as Values),
        ),
      ),
      options,
    );
  }
}