All files / src/sequelize sequelize-hooks.ts

100% Statements 17/17
100% Branches 4/4
100% Functions 9/9
100% Lines 17/17

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          131x 1x       131x                           131x 131x                                 26x 26x       26x                               4x 4x                                 10x                             4x                               3x     131x 4454x               3x       131x    
import type { HookFunction, HookType } from '../hooks/hooks.ts';
import type { InstanceHookAdders } from './instance-hook-adders.ts';
import { NAMED_HOOKS } from '../model/mixins/named-hooks.ts';
import type { SequelizeConstructor } from './sequelize-constructor.ts';
 
const missingHook = (): never => {
  throw new Error('addHook: hook function is required');
};
 
/** Hook types that get a named adder on `Sequelize` instances (`sequelize.beforeCreate(fn)`): the run hooks that apply to an instance. */
const INSTANCE_HOOKS = [
  ...NAMED_HOOKS,
  'beforeConnect',
  'afterConnect',
  'beforeBulkSync',
  'afterBulkSync',
] as const satisfies readonly (HookType & keyof InstanceHookAdders)[];
 
/**
 * Hook methods of `Sequelize`: `addHook`, `removeHook`, `hasHook`, `hasHooks`, `runHooks` and the named adders.
 *
 * @internal
 */
// oxlint-disable-next-line max-lines-per-function, typescript/explicit-module-boundary-types -- a mixin factory's body is its class (each method is still size-checked); its type can only be inferred.
export const SequelizeHooks = <TBase extends SequelizeConstructor>(Base: TBase) => {
  const Mixin = class HooksCapability extends Base {
    /**
     * Registers a global hook that runs for every model of this instance, or a connection hook such as `beforeConnect`. Global hooks are not fired for models that never use the hook type.
     *
     * @param type - Hook type, e.g. `'beforeCreate'`.
     * @param nameOrFn - Hook name (to remove it later) or the hook function.
     * @param hook - Hook function when a name is given.
     * @returns This instance.
     *
     * @example
     * ```ts
     * sequelize.addHook('beforeCreate', 'stamp', (instance: any) => {
     *   instance.createdBy = 'system';
     * });
     * ```
     */
    addHook(type: string, nameOrFn: string | HookFunction, hook?: HookFunction): this {
      const { internals } = this;
      internals.hooks =
        typeof nameOrFn === 'string'
          ? internals.hooks.add(type, hook ?? missingHook(), nameOrFn)
          : internals.hooks.add(type, nameOrFn);
      return this;
    }
 
    /**
     * Unregisters global hooks by name or by function reference.
     *
     * @param type - Hook type.
     * @param nameOrFn - Hook name or function; all hooks of the type when omitted.
     * @returns This instance.
     *
     * @example
     * ```ts
     * sequelize.removeHook('beforeCreate', 'stamp');
     * ```
     */
    removeHook(type: string, nameOrFn?: string | HookFunction): this {
      this.internals.hooks = this.internals.hooks.remove(type, nameOrFn);
      return this;
    }
 
    /**
     * Tells whether this instance has at least one global hook of the given type.
     *
     * @param type - Hook type.
     * @returns True when at least one hook exists.
     *
     * @example
     * ```ts
     * if (!sequelize.hasHook('beforeCreate')) {
     *   sequelize.addHook('beforeCreate', () => {});
     * }
     * ```
     */
    hasHook(type: string): boolean {
      return this.internals.hooks.has(type);
    }
 
    /**
     * Alias of {@link Sequelize.hasHook}, kept for Sequelize v6 compatibility.
     *
     * @param type - Hook type.
     * @returns True when at least one hook exists.
     *
     * @example
     * ```ts
     * const registered = sequelize.hasHooks('afterUpdate');
     * ```
     */
    hasHooks(type: string): boolean {
      return this.hasHook(type);
    }
 
    /**
     * Runs the global hooks of a type with the given arguments.
     *
     * @param type - Hook type.
     * @param args - Hook arguments.
     * @returns Resolves when all hooks finished.
     *
     * @example
     * ```ts
     * await sequelize.runHooks('beforeConnect', { host: 'localhost' });
     * ```
     */
    runHooks(type: string, ...args: readonly unknown[]): Promise<void> {
      return this.internals.hooks.run(type as HookType, this, ...args);
    }
  };
  INSTANCE_HOOKS.forEach((type) => {
    Object.defineProperty(Mixin.prototype, type, {
      configurable: true,
      writable: true,
      value(
        this: InstanceType<typeof Mixin>,
        nameOrFn: string | HookFunction,
        fn?: HookFunction,
      ): void {
        this.addHook(type, nameOrFn, fn);
      },
    });
  });
  return Mixin;
};