Skip to content

Commit aca7541

Browse files
committed
feat: 添加 HookRuntimeContext,定义钩子执行期间可用的上下文信息
1 parent 2214795 commit aca7541

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

packages/spec/src/data/hook.zod.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,72 @@ export const HookSchema = z.object({
9595
onError: z.enum(['abort', 'log']).default('abort').describe('Error handling strategy'),
9696
});
9797

98+
/**
99+
* Hook Runtime Context
100+
* Defines what is available to the hook handler during execution.
101+
*
102+
* Best Practices:
103+
* - **Immutability**: `object`, `event`, `id` are immutable.
104+
* - **Mutability**: `input` and `result` are mutable to allow transformation.
105+
* - **Encapsulation**: `session` isolates auth info; `transaction` ensures atomicity.
106+
*/
107+
export const HookContextSchema = z.object({
108+
/** Tracing ID */
109+
id: z.string().optional().describe('Unique execution ID for tracing'),
110+
111+
/** Target Object Name */
112+
object: z.string(),
113+
114+
/** Current Lifecycle Event */
115+
event: HookEvent,
116+
117+
/**
118+
* Input Parameters (Mutable)
119+
* Modify this to change the behavior of the operation.
120+
*
121+
* - find: { query: QueryAST, options: DriverOptions }
122+
* - insert: { doc: Record, options: DriverOptions }
123+
* - update: { id: ID, doc: Record, options: DriverOptions }
124+
* - delete: { id: ID, options: DriverOptions }
125+
*/
126+
input: z.record(z.any()).describe('Mutable input parameters'),
127+
128+
/**
129+
* Operation Result (Mutable)
130+
* Available in 'after*' events. Modify this to transform the output.
131+
*/
132+
result: z.any().optional().describe('Operation result (After hooks only)'),
133+
134+
/**
135+
* Data Snapshot
136+
* The state of the record BEFORE the operation (for update/delete).
137+
*/
138+
previous: z.record(z.any()).optional().describe('Record state before operation'),
139+
140+
/**
141+
* Execution Session
142+
* Contains authentication and tenancy information.
143+
*/
144+
session: z.object({
145+
userId: z.string().optional(),
146+
tenantId: z.string().optional(),
147+
roles: z.array(z.string()).optional(),
148+
accessToken: z.string().optional(),
149+
}).optional().describe('Current session context'),
150+
151+
/**
152+
* Transaction Handle
153+
* If the operation is part of a transaction, use this handle for side-effects.
154+
*/
155+
transaction: z.any().optional().describe('Database transaction handle'),
156+
157+
/**
158+
* Engine Access
159+
* Reference to the ObjectQL engine for performing side effects.
160+
*/
161+
ql: z.any().describe('ObjectQL Engine Reference'),
162+
});
163+
98164
export type Hook = z.infer<typeof HookSchema>;
99165
export type HookEventType = z.infer<typeof HookEvent>;
166+
export type HookContext = z.infer<typeof HookContextSchema>;

0 commit comments

Comments
 (0)