-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrecord-change-trigger.ts
More file actions
195 lines (175 loc) · 8.36 KB
/
Copy pathrecord-change-trigger.ts
File metadata and controls
195 lines (175 loc) · 8.36 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import type { AutomationContext } from '@objectstack/spec/contracts';
import type { HookContext } from '@objectstack/spec/data';
/**
* Structural mirror of the automation engine's `FlowTriggerBinding`
* (service-automation/src/engine.ts). Declared locally so this trigger plugin
* stays decoupled from the automation package — same pattern the connector /
* messaging integrations use to avoid a hard build edge. The engine parses the
* flow's start node and hands us one of these per activated flow.
*/
export interface FlowTriggerBinding {
readonly flowName: string;
readonly object?: string;
readonly event?: string;
readonly condition?: string | { dialect?: string; source?: string; ast?: unknown };
readonly schedule?: unknown;
readonly config?: Record<string, unknown>;
}
/**
* Structural mirror of the engine's `FlowTrigger` extension point. The engine
* calls {@link start} with a parsed binding + a callback that runs the flow,
* and {@link stop} when the flow is unregistered/disabled.
*/
export interface FlowTrigger {
readonly type: string;
start(binding: FlowTriggerBinding, callback: (ctx: AutomationContext) => Promise<void>): void;
stop(flowName: string): void;
}
/**
* The slice of the ObjectQL data engine this trigger needs: subscribe to a
* lifecycle hook, and (for teardown) drop all hooks owned by a packageId.
* Typed structurally because `IDataEngine` (the public contract) doesn't model
* the hook surface, but the concrete engine implements both.
*/
export interface RecordChangeDataEngine {
registerHook(
event: string,
handler: (ctx: HookContext) => unknown | Promise<unknown>,
options?: { object?: string | string[]; priority?: number; packageId?: string },
): void;
unregisterHooksByPackage?(packageId: string): number;
}
/** Minimal logger surface (matches core's `ctx.logger`). */
export interface TriggerLogger {
info(msg: string, ...args: unknown[]): void;
warn(msg: string, ...args: unknown[]): void;
debug?(msg: string, ...args: unknown[]): void;
}
const TRIGGER_PREFIX = 'com.objectstack.trigger.record-change';
/**
* Map a flow start node's `triggerType` (e.g. `record-after-update`) to an
* ObjectQL `HookEvent` (e.g. `afterUpdate`). Returns `null` for anything that
* isn't a `record-(before|after)-(create|insert|update|delete)` token.
*/
export function triggerTypeToHookEvent(triggerType: string | undefined): string | null {
if (!triggerType) return null;
const m = /^record-(before|after)-(create|insert|update|delete)$/.exec(triggerType.trim());
if (!m) return null;
const phase = m[1]; // 'before' | 'after'
const op = m[2]; // create|insert|update|delete
const verb = op === 'create' || op === 'insert' ? 'Insert' : op.charAt(0).toUpperCase() + op.slice(1);
return `${phase}${verb}`; // e.g. 'afterUpdate', 'beforeDelete'
}
/**
* RecordChangeTrigger
*
* Bridges the automation engine's {@link FlowTrigger} extension point to
* ObjectQL lifecycle hooks. For each flow the engine activates, it subscribes
* to the matching hook event (filtered to the flow's target object) and, when
* the hook fires, builds an {@link AutomationContext} from the new/old record
* and invokes the engine-supplied callback (which runs the flow — the engine
* owns the start-node condition gate, so we don't re-evaluate it here).
*
* Each flow's hooks are registered under a per-flow packageId so {@link stop}
* can tear exactly that flow's subscription down via
* `unregisterHooksByPackage`, without touching other flows or audit hooks.
*/
export class RecordChangeTrigger implements FlowTrigger {
readonly type = 'record_change';
private readonly engine: RecordChangeDataEngine;
private readonly logger: TriggerLogger;
/** flowName → packageId used for its hook(s), so stop() can unregister it. */
private readonly bound = new Map<string, string>();
constructor(engine: RecordChangeDataEngine, logger: TriggerLogger) {
this.engine = engine;
this.logger = logger;
}
start(binding: FlowTriggerBinding, callback: (ctx: AutomationContext) => Promise<void>): void {
const hookEvent = triggerTypeToHookEvent(binding.event);
if (!hookEvent) {
this.logger.warn(
`[record-change] flow '${binding.flowName}' has unsupported trigger event '${binding.event ?? '(none)'}' — not bound`,
);
return;
}
// Idempotent: drop any prior subscription for this flow before re-binding
// (covers disable→enable cycles and hot reload).
this.stop(binding.flowName);
const packageId = `${TRIGGER_PREFIX}:${binding.flowName}`;
const handler = async (ctx: HookContext): Promise<void> => {
try {
const automationCtx = this.buildContext(binding, ctx);
await callback(automationCtx);
} catch (err) {
// Error isolation: a flow failure must NEVER break the CRUD write
// that triggered it. Log and swallow.
this.logger.warn(
`[record-change] flow '${binding.flowName}' execution failed: ${(err as Error)?.message ?? String(err)}`,
);
}
};
this.engine.registerHook(hookEvent, handler, {
object: binding.object,
packageId,
});
this.bound.set(binding.flowName, packageId);
this.logger.info(
`[record-change] bound flow '${binding.flowName}' → ${hookEvent}${binding.object ? ` on '${binding.object}'` : ''}`,
);
}
stop(flowName: string): void {
const packageId = this.bound.get(flowName);
if (!packageId) return;
try {
this.engine.unregisterHooksByPackage?.(packageId);
} catch (err) {
this.logger.warn(
`[record-change] failed to unbind flow '${flowName}': ${(err as Error)?.message ?? String(err)}`,
);
}
this.bound.delete(flowName);
this.logger.debug?.(`[record-change] unbound flow '${flowName}'`);
}
/**
* Build the flow execution context from an ObjectQL hook context. The new
* record comes from `ctx.result` (after-hooks) or falls back to the
* mutation input doc / previous row; the old record from `ctx.previous`
* (with the `__previous` stash audit also uses as a fallback).
*/
private buildContext(binding: FlowTriggerBinding, ctx: HookContext): AutomationContext {
// objectql lifecycle hooks carry the written row under `input.data` (insert /
// update payload); `id` is on update. (`doc` kept only as a defensive alias.)
const input = (ctx.input ?? {}) as { data?: Record<string, unknown>; doc?: Record<string, unknown>; id?: unknown };
const after = ctx.result as Record<string, unknown> | undefined;
const previous =
(ctx.previous as Record<string, unknown> | undefined) ??
((ctx as unknown as { __previous?: Record<string, unknown> }).__previous ?? undefined);
const inputDoc =
input.data && typeof input.data === 'object'
? input.data
: input.doc && typeof input.doc === 'object'
? input.doc
: undefined;
const record: Record<string, unknown> =
after && typeof after === 'object'
? // #1872 — overlay the after-row on the input doc so fields the
// driver did not echo back (notably `multiple: true` lookups,
// stored as an array column) stay visible to the flow's start
// condition and `{record.<field>}` interpolation. The after-row
// wins for every field it DOES return (id, DB-computed values).
{ ...(inputDoc ?? {}), ...after }
: inputDoc ?? (previous && typeof previous === 'object' ? previous : {});
const session = (ctx.session ?? {}) as { userId?: string };
return {
record,
previous,
object: binding.object ?? ctx.object,
event: binding.event,
userId: session.userId,
// Expose the record as params too, so flows with named `isInput`
// variables matching record fields get them seeded.
params: record,
};
}
}