All files / src/model sync-hooks.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 2/2
100% Lines 8/8

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                      131x                     706x 2x 2x   704x 704x 704x 704x      
import type { SyncOptions } from '../query-interface/schema-synchronizer.ts';
import type { InstanceLifecycle } from './instance-lifecycle.ts';
 
/** Runs the `beforeSync` / `afterSync` hooks of a model around its table sync. */
export class SyncHooks {
  readonly #lifecycle: InstanceLifecycle;
 
  /**
   * @param lifecycle - Hook runner (model hooks, then Sequelize-level hooks).
   */
  constructor(lifecycle: InstanceLifecycle) {
    this.#lifecycle = lifecycle;
  }
 
  /**
   * Runs `run` between the model's sync hooks unless `options.hooks` is `false`.
   *
   * @param model - Model class.
   * @param options - Sync options passed to the hooks.
   * @param run - The table sync.
   */
  async around(model: object, options: SyncOptions, run: () => Promise<void>): Promise<void> {
    if (options.hooks === false) {
      await run();
      return;
    }
    const hookOptions = { ...options, hooks: true };
    await this.#lifecycle.runHooks(model, 'beforeSync', hookOptions);
    await run();
    await this.#lifecycle.runHooks(model, 'afterSync', hookOptions);
  }
}