(hooks, ...hookArgs)
| 91 | }, |
| 92 | |
| 93 | async runHooks(hooks, ...hookArgs) { |
| 94 | if (!hooks) throw new Error('runHooks requires at least 1 argument'); |
| 95 | |
| 96 | let hookType; |
| 97 | |
| 98 | if (typeof hooks === 'string') { |
| 99 | hookType = hooks; |
| 100 | hooks = getHooks(this, hookType); |
| 101 | |
| 102 | if (this.sequelize) { |
| 103 | hooks = hooks.concat(getHooks(this.sequelize, hookType)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (!Array.isArray(hooks)) { |
| 108 | hooks = [hooks]; |
| 109 | } |
| 110 | |
| 111 | // synchronous hooks |
| 112 | if (hookTypes[hookType] && hookTypes[hookType].sync) { |
| 113 | for (let hook of hooks) { |
| 114 | if (typeof hook === 'object') { |
| 115 | hook = hook.fn; |
| 116 | } |
| 117 | |
| 118 | debug(`running hook(sync) ${hookType}`); |
| 119 | hook.apply(this, hookArgs); |
| 120 | } |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // asynchronous hooks (default) |
| 125 | for (let hook of hooks) { |
| 126 | if (typeof hook === 'object') { |
| 127 | hook = hook.fn; |
| 128 | } |
| 129 | |
| 130 | debug(`running hook ${hookType}`); |
| 131 | await hook.apply(this, hookArgs); |
| 132 | } |
| 133 | }, |
| 134 | |
| 135 | /** |
| 136 | * Add a hook to the model |
no test coverage detected