|
1 | | -import { QueryAST } from '@objectstack/spec/data'; |
| 1 | +import { QueryAST, HookContext, HookEvent } from '@objectstack/spec/data'; |
2 | 2 | import { ObjectStackManifest } from '@objectstack/spec/system'; |
3 | 3 | import { DriverInterface, DriverOptions } from '@objectstack/spec/driver'; |
4 | 4 | import { SchemaRegistry } from './registry'; |
5 | 5 |
|
6 | 6 | // Export Registry for consumers |
7 | 7 | export { SchemaRegistry } from './registry'; |
8 | 8 |
|
9 | | -/** |
10 | | - * Hook Context |
11 | | - */ |
12 | | -export interface HookContext { |
13 | | - object: string; |
14 | | - driver: DriverInterface; |
15 | | - method: 'find' | 'insert' | 'update' | 'delete' | 'count'; |
16 | | - args: any; // The arguments passed to the method (can be modified) |
17 | | - result?: any; // The result of the operation (for after hooks) |
18 | | - error?: any; // The error if one occurred (for error hooks) |
19 | | -} |
20 | | - |
21 | 9 | export type HookHandler = (context: HookContext) => Promise<void> | void; |
22 | 10 |
|
23 | 11 | /** |
@@ -242,23 +230,23 @@ export class ObjectQL { |
242 | 230 | // Trigger Before Hook |
243 | 231 | const hookContext: HookContext = { |
244 | 232 | object, |
245 | | - driver, |
246 | | - method: 'find', |
247 | | - args: { ast, options } // Hooks can modify AST here |
| 233 | + event: 'beforeFind', |
| 234 | + input: { ast, options }, // Hooks can modify AST here |
| 235 | + ql: this |
248 | 236 | }; |
249 | 237 | await this.triggerHooks('beforeFind', hookContext); |
250 | 238 |
|
251 | 239 | try { |
252 | | - const result = await driver.find(object, hookContext.args.ast, hookContext.args.options); |
| 240 | + const result = await driver.find(object, hookContext.input.ast, hookContext.input.options); |
253 | 241 |
|
254 | 242 | // Trigger After Hook |
| 243 | + hookContext.event = 'afterFind'; |
255 | 244 | hookContext.result = result; |
256 | 245 | await this.triggerHooks('afterFind', hookContext); |
257 | 246 |
|
258 | 247 | return hookContext.result; |
259 | 248 | } catch (e) { |
260 | | - hookContext.error = e; |
261 | | - // await this.triggerHooks('error', hookContext); |
| 249 | + // hookContext.error = e; |
262 | 250 | throw e; |
263 | 251 | } |
264 | 252 | } |
|
0 commit comments