# All files / framework/models/decorators hooks.ts

0% Statements 0/17
0% Branches 0/8
0% Functions 0/10
0% Lines 0/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

     export function BeforeCreate(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
      registerHook(target, 'beforeCreate', descriptor.value);
    }

    export function AfterCreate(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
      registerHook(target, 'afterCreate', descriptor.value);
    }

    export function BeforeUpdate(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
      registerHook(target, 'beforeUpdate', descriptor.value);
    }

    export function AfterUpdate(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
      registerHook(target, 'afterUpdate', descriptor.value);
    }

    export function BeforeDelete(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
      registerHook(target, 'beforeDelete', descriptor.value);
    }

    export function AfterDelete(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
      registerHook(target, 'afterDelete', descriptor.value);
    }

    export function BeforeSave(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
      registerHook(target, 'beforeSave', descriptor.value);
    }

    export function AfterSave(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
      registerHook(target, 'afterSave', descriptor.value);
    }

    function registerHook(target: any, hookName: string, hookFunction: Function): void {
      // Initialize hooks map if it doesn't exist
      if (!target.constructor.hooks) {
        target.constructor.hooks = new Map();
      }

      // Get existing hooks for this hook name
      const existingHooks = target.constructor.hooks.get(hookName) || [];

      // Add the new hook
      existingHooks.push(hookFunction);

      // Store updated hooks array
      target.constructor.hooks.set(hookName, existingHooks);

      console.log(`Registered ${hookName} hook for ${target.constructor.name}`);
    }

    // Utility function to get hooks for a specific event
    export function getHooks(target: any, hookName: string): Function[] {
      if (!target.constructor.hooks) {
        return [];
      }
      return target.constructor.hooks.get(hookName) || [];
    }

    // Export decorator types for TypeScript
    export type HookDecorator = (
      target: any,
      propertyKey: string,
      descriptor: PropertyDescriptor,
    ) => void;