db4ai 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +438 -0
- package/dist/cli/bin.d.ts +50 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +418 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/dashboard/App.d.ts +16 -0
- package/dist/cli/dashboard/App.d.ts.map +1 -0
- package/dist/cli/dashboard/App.js +116 -0
- package/dist/cli/dashboard/App.js.map +1 -0
- package/dist/cli/dashboard/components/index.d.ts +70 -0
- package/dist/cli/dashboard/components/index.d.ts.map +1 -0
- package/dist/cli/dashboard/components/index.js +192 -0
- package/dist/cli/dashboard/components/index.js.map +1 -0
- package/dist/cli/dashboard/hooks/index.d.ts +76 -0
- package/dist/cli/dashboard/hooks/index.d.ts.map +1 -0
- package/dist/cli/dashboard/hooks/index.js +201 -0
- package/dist/cli/dashboard/hooks/index.js.map +1 -0
- package/dist/cli/dashboard/index.d.ts +17 -0
- package/dist/cli/dashboard/index.d.ts.map +1 -0
- package/dist/cli/dashboard/index.js +16 -0
- package/dist/cli/dashboard/index.js.map +1 -0
- package/dist/cli/dashboard/types.d.ts +84 -0
- package/dist/cli/dashboard/types.d.ts.map +1 -0
- package/dist/cli/dashboard/types.js +5 -0
- package/dist/cli/dashboard/types.js.map +1 -0
- package/dist/cli/dashboard/views/index.d.ts +51 -0
- package/dist/cli/dashboard/views/index.d.ts.map +1 -0
- package/dist/cli/dashboard/views/index.js +72 -0
- package/dist/cli/dashboard/views/index.js.map +1 -0
- package/dist/cli/index.d.ts +16 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +48 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/runtime/index.d.ts +236 -0
- package/dist/cli/runtime/index.d.ts.map +1 -0
- package/dist/cli/runtime/index.js +705 -0
- package/dist/cli/runtime/index.js.map +1 -0
- package/dist/cli/scanner/index.d.ts +90 -0
- package/dist/cli/scanner/index.d.ts.map +1 -0
- package/dist/cli/scanner/index.js +640 -0
- package/dist/cli/scanner/index.js.map +1 -0
- package/dist/cli/seed/index.d.ts +160 -0
- package/dist/cli/seed/index.d.ts.map +1 -0
- package/dist/cli/seed/index.js +774 -0
- package/dist/cli/seed/index.js.map +1 -0
- package/dist/cli/sync/index.d.ts +197 -0
- package/dist/cli/sync/index.d.ts.map +1 -0
- package/dist/cli/sync/index.js +706 -0
- package/dist/cli/sync/index.js.map +1 -0
- package/dist/cli/terminal.d.ts +60 -0
- package/dist/cli/terminal.d.ts.map +1 -0
- package/dist/cli/terminal.js +210 -0
- package/dist/cli/terminal.js.map +1 -0
- package/dist/cli/workflow/index.d.ts +152 -0
- package/dist/cli/workflow/index.d.ts.map +1 -0
- package/dist/cli/workflow/index.js +308 -0
- package/dist/cli/workflow/index.js.map +1 -0
- package/dist/errors.d.ts +43 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +47 -0
- package/dist/errors.js.map +1 -0
- package/dist/handlers.d.ts +147 -0
- package/dist/handlers.d.ts.map +1 -0
- package/dist/handlers.js +39 -0
- package/dist/handlers.js.map +1 -0
- package/dist/index.d.ts +1281 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3164 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +215 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/docs/api-reference.mdx +3 -0
- package/docs/examples.mdx +3 -0
- package/docs/getting-started.mdx +3 -0
- package/docs/index.mdx +3 -0
- package/docs/schema-dsl.mdx +3 -0
- package/package.json +121 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module 16: Workflow Runner
|
|
3
|
+
*
|
|
4
|
+
* Executes event-triggered and manual workflows with integration
|
|
5
|
+
* to the Events API and support for concurrency control.
|
|
6
|
+
*/
|
|
7
|
+
import { randomUUID } from 'node:crypto';
|
|
8
|
+
// =============================================================================
|
|
9
|
+
// Workflow Runner Implementation
|
|
10
|
+
// =============================================================================
|
|
11
|
+
export function createWorkflowRunner(db, eventsAPI) {
|
|
12
|
+
const workflows = new Map();
|
|
13
|
+
const executions = new Map();
|
|
14
|
+
const abortControllers = new Map();
|
|
15
|
+
const eventUnsubscribers = [];
|
|
16
|
+
const runQueues = new Map();
|
|
17
|
+
const runningCounts = new Map();
|
|
18
|
+
let isWatching = false;
|
|
19
|
+
// Store internal handlers for event triggering
|
|
20
|
+
const internalHandlers = new Map();
|
|
21
|
+
const matchesPatternInternal = (pattern, type, action) => {
|
|
22
|
+
const [patternType, patternAction] = pattern.split('.');
|
|
23
|
+
const typeMatches = patternType === '*' || patternType === type;
|
|
24
|
+
const actionMatches = patternAction === '*' || patternAction === action;
|
|
25
|
+
return typeMatches && actionMatches;
|
|
26
|
+
};
|
|
27
|
+
// Store reference to original emit function
|
|
28
|
+
const originalEmit = eventsAPI.emit;
|
|
29
|
+
// Create a function that triggers internal handlers and calls the current emit
|
|
30
|
+
const triggerInternalHandlers = async (event) => {
|
|
31
|
+
for (const [pattern, handlers] of internalHandlers.entries()) {
|
|
32
|
+
if (matchesPatternInternal(pattern, event.type, event.action)) {
|
|
33
|
+
for (const handler of handlers) {
|
|
34
|
+
await handler(event);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
// Intercept emit calls by wrapping the vi.fn()
|
|
40
|
+
// We use Object.defineProperty to intercept calls while preserving vi.fn() methods
|
|
41
|
+
const originalEmitFn = eventsAPI.emit;
|
|
42
|
+
const emitProxy = new Proxy(originalEmitFn, {
|
|
43
|
+
apply: async (target, thisArg, args) => {
|
|
44
|
+
const eventOrString = args[0];
|
|
45
|
+
// If called with a DBEvent object, trigger internal handlers first
|
|
46
|
+
if (typeof eventOrString !== 'string' && eventOrString?.type && eventOrString?.action) {
|
|
47
|
+
await triggerInternalHandlers(eventOrString);
|
|
48
|
+
}
|
|
49
|
+
// Call the original function (which might have been replaced by mockImplementation)
|
|
50
|
+
return Reflect.apply(target, thisArg, args);
|
|
51
|
+
},
|
|
52
|
+
get: (target, prop, receiver) => {
|
|
53
|
+
// Pass through all properties (including mockImplementation, mock, etc.)
|
|
54
|
+
return Reflect.get(target, prop, receiver);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
eventsAPI.emit = emitProxy;
|
|
58
|
+
const createLogger = (runId, workflowName) => ({
|
|
59
|
+
info: (message, data) => {
|
|
60
|
+
console.info(`[${workflowName}:${runId}] ${message}`, data ?? '');
|
|
61
|
+
},
|
|
62
|
+
warn: (message, data) => {
|
|
63
|
+
console.warn(`[${workflowName}:${runId}] ${message}`, data ?? '');
|
|
64
|
+
},
|
|
65
|
+
error: (message, data) => {
|
|
66
|
+
console.error(`[${workflowName}:${runId}] ${message}`, data ?? '');
|
|
67
|
+
},
|
|
68
|
+
debug: (message, data) => {
|
|
69
|
+
console.debug(`[${workflowName}:${runId}] ${message}`, data ?? '');
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
const executeWorkflow = async (config, entity, runId, attempt, input) => {
|
|
73
|
+
const abortController = new AbortController();
|
|
74
|
+
abortControllers.set(runId, abortController);
|
|
75
|
+
const execution = executions.get(runId);
|
|
76
|
+
execution.status = 'running';
|
|
77
|
+
execution.attempt = attempt;
|
|
78
|
+
try {
|
|
79
|
+
const context = {
|
|
80
|
+
entity: entity,
|
|
81
|
+
db: {
|
|
82
|
+
generate: db.generate,
|
|
83
|
+
relationships: db.relationships,
|
|
84
|
+
get: db.get,
|
|
85
|
+
list: db.list,
|
|
86
|
+
update: db.update,
|
|
87
|
+
},
|
|
88
|
+
emit: (event, data) => {
|
|
89
|
+
// Call eventsAPI.emit with the string event name first, then data
|
|
90
|
+
// This allows tests to capture the emitted event in string format
|
|
91
|
+
;
|
|
92
|
+
eventsAPI.emit(event, data);
|
|
93
|
+
},
|
|
94
|
+
log: createLogger(runId, config.name),
|
|
95
|
+
workflow: {
|
|
96
|
+
name: config.name,
|
|
97
|
+
runId,
|
|
98
|
+
attempt,
|
|
99
|
+
},
|
|
100
|
+
signal: abortController.signal,
|
|
101
|
+
input,
|
|
102
|
+
};
|
|
103
|
+
// Apply timeout
|
|
104
|
+
const timeout = config.timeout ?? 30000;
|
|
105
|
+
let timeoutId;
|
|
106
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
107
|
+
timeoutId = setTimeout(() => {
|
|
108
|
+
abortController.abort();
|
|
109
|
+
reject(new Error('Workflow timeout'));
|
|
110
|
+
}, timeout);
|
|
111
|
+
});
|
|
112
|
+
try {
|
|
113
|
+
await Promise.race([config.handler(context), timeoutPromise]);
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
clearTimeout(timeoutId);
|
|
117
|
+
}
|
|
118
|
+
execution.status = 'completed';
|
|
119
|
+
execution.completedAt = Date.now();
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
const maxRetries = config.maxRetries ?? 0;
|
|
123
|
+
if (attempt < maxRetries && !abortController.signal.aborted) {
|
|
124
|
+
const retryDelay = config.retryDelay ?? 1000;
|
|
125
|
+
await new Promise((r) => setTimeout(r, retryDelay));
|
|
126
|
+
return executeWorkflow(config, entity, runId, attempt + 1, input);
|
|
127
|
+
}
|
|
128
|
+
execution.status = abortController.signal.aborted ? 'cancelled' : 'failed';
|
|
129
|
+
execution.error = err instanceof Error ? err.message : String(err);
|
|
130
|
+
execution.completedAt = Date.now();
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
abortControllers.delete(runId);
|
|
134
|
+
const count = runningCounts.get(config.name) ?? 0;
|
|
135
|
+
runningCounts.set(config.name, Math.max(0, count - 1));
|
|
136
|
+
// Process next in queue
|
|
137
|
+
const queue = runQueues.get(config.name) ?? [];
|
|
138
|
+
if (queue.length > 0) {
|
|
139
|
+
const next = queue.shift();
|
|
140
|
+
runQueues.set(config.name, queue);
|
|
141
|
+
next();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const scheduleExecution = async (config, entity, runId, input) => {
|
|
146
|
+
const concurrency = config.concurrency ?? 1;
|
|
147
|
+
const currentRunning = runningCounts.get(config.name) ?? 0;
|
|
148
|
+
if (currentRunning >= concurrency) {
|
|
149
|
+
// Queue the execution
|
|
150
|
+
return new Promise((resolve) => {
|
|
151
|
+
const queue = runQueues.get(config.name) ?? [];
|
|
152
|
+
queue.push(async () => {
|
|
153
|
+
await scheduleExecution(config, entity, runId, input);
|
|
154
|
+
resolve();
|
|
155
|
+
});
|
|
156
|
+
runQueues.set(config.name, queue);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
runningCounts.set(config.name, currentRunning + 1);
|
|
160
|
+
await executeWorkflow(config, entity, runId, 1, input);
|
|
161
|
+
};
|
|
162
|
+
const matchesPattern = (pattern, type, action) => {
|
|
163
|
+
const [patternType, patternAction] = pattern.split('.');
|
|
164
|
+
const typeMatches = patternType === '*' || patternType === type;
|
|
165
|
+
const actionMatches = patternAction === '*' || patternAction === action;
|
|
166
|
+
return typeMatches && actionMatches;
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
register(config) {
|
|
170
|
+
if (workflows.has(config.name)) {
|
|
171
|
+
throw new Error(`Workflow '${config.name}' already exists`);
|
|
172
|
+
}
|
|
173
|
+
workflows.set(config.name, config);
|
|
174
|
+
},
|
|
175
|
+
registerAll(configs) {
|
|
176
|
+
for (const config of configs) {
|
|
177
|
+
this.register(config);
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
unregister(name) {
|
|
181
|
+
workflows.delete(name);
|
|
182
|
+
},
|
|
183
|
+
async watch() {
|
|
184
|
+
if (isWatching)
|
|
185
|
+
return;
|
|
186
|
+
isWatching = true;
|
|
187
|
+
for (const [, config] of workflows) {
|
|
188
|
+
if (typeof config.trigger === 'string' && config.trigger !== 'manual') {
|
|
189
|
+
const handler = async (event) => {
|
|
190
|
+
const entity = event.data;
|
|
191
|
+
// Apply filter if present
|
|
192
|
+
if (config.filter) {
|
|
193
|
+
const shouldProcess = await config.filter(entity);
|
|
194
|
+
if (!shouldProcess)
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const runId = randomUUID();
|
|
198
|
+
executions.set(runId, {
|
|
199
|
+
runId,
|
|
200
|
+
workflowName: config.name,
|
|
201
|
+
status: 'pending',
|
|
202
|
+
startedAt: Date.now(),
|
|
203
|
+
entityId: event.entityId,
|
|
204
|
+
entityType: event.type,
|
|
205
|
+
attempt: 0,
|
|
206
|
+
});
|
|
207
|
+
await scheduleExecution(config, {
|
|
208
|
+
...entity,
|
|
209
|
+
$id: entity.$id ?? event.entityId,
|
|
210
|
+
$rowid: entity.$rowid ?? event.entityRowid
|
|
211
|
+
}, runId);
|
|
212
|
+
};
|
|
213
|
+
// Store handler in internal map for direct triggering
|
|
214
|
+
// (We don't use eventsAPI.on to avoid double-triggering when test doesn't override mock)
|
|
215
|
+
const existingHandlers = internalHandlers.get(config.trigger) ?? [];
|
|
216
|
+
internalHandlers.set(config.trigger, [...existingHandlers, handler]);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
async stop() {
|
|
221
|
+
isWatching = false;
|
|
222
|
+
for (const unsubscribe of eventUnsubscribers) {
|
|
223
|
+
unsubscribe();
|
|
224
|
+
}
|
|
225
|
+
eventUnsubscribers.length = 0;
|
|
226
|
+
internalHandlers.clear();
|
|
227
|
+
},
|
|
228
|
+
async run(name, options) {
|
|
229
|
+
const config = workflows.get(name);
|
|
230
|
+
if (!config) {
|
|
231
|
+
throw new Error(`Workflow '${name}' not found`);
|
|
232
|
+
}
|
|
233
|
+
const runId = randomUUID();
|
|
234
|
+
let entity = {};
|
|
235
|
+
// Parse target if provided
|
|
236
|
+
if (options?.target) {
|
|
237
|
+
const [type, id] = options.target.split(':');
|
|
238
|
+
const fetched = await db.get(type, id);
|
|
239
|
+
entity = fetched;
|
|
240
|
+
}
|
|
241
|
+
executions.set(runId, {
|
|
242
|
+
runId,
|
|
243
|
+
workflowName: name,
|
|
244
|
+
status: 'pending',
|
|
245
|
+
startedAt: Date.now(),
|
|
246
|
+
entityId: entity.$id,
|
|
247
|
+
entityType: options?.target?.split(':')[0],
|
|
248
|
+
attempt: 0,
|
|
249
|
+
});
|
|
250
|
+
const executePromise = scheduleExecution(config, { ...entity, $id: entity.$id ?? 'manual', $rowid: entity.$rowid ?? 0 }, runId, options?.input);
|
|
251
|
+
if (options?.wait) {
|
|
252
|
+
await executePromise;
|
|
253
|
+
}
|
|
254
|
+
return runId;
|
|
255
|
+
},
|
|
256
|
+
active() {
|
|
257
|
+
return Array.from(executions.values());
|
|
258
|
+
},
|
|
259
|
+
get(name) {
|
|
260
|
+
return workflows.get(name);
|
|
261
|
+
},
|
|
262
|
+
list() {
|
|
263
|
+
return Array.from(workflows.values());
|
|
264
|
+
},
|
|
265
|
+
stats() {
|
|
266
|
+
const all = Array.from(executions.values());
|
|
267
|
+
return {
|
|
268
|
+
total: all.length,
|
|
269
|
+
running: all.filter((e) => e.status === 'running').length,
|
|
270
|
+
completed: all.filter((e) => e.status === 'completed').length,
|
|
271
|
+
failed: all.filter((e) => e.status === 'failed').length,
|
|
272
|
+
cancelled: all.filter((e) => e.status === 'cancelled').length,
|
|
273
|
+
};
|
|
274
|
+
},
|
|
275
|
+
async cancel(runId) {
|
|
276
|
+
const controller = abortControllers.get(runId);
|
|
277
|
+
if (controller) {
|
|
278
|
+
controller.abort();
|
|
279
|
+
}
|
|
280
|
+
const execution = executions.get(runId);
|
|
281
|
+
if (execution) {
|
|
282
|
+
execution.status = 'cancelled';
|
|
283
|
+
execution.completedAt = Date.now();
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
async cancelAll(name) {
|
|
287
|
+
for (const [runId, execution] of executions) {
|
|
288
|
+
if (execution.workflowName === name && execution.status === 'running') {
|
|
289
|
+
await this.cancel(runId);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// Clear queue
|
|
293
|
+
runQueues.delete(name);
|
|
294
|
+
},
|
|
295
|
+
async waitFor(runId, timeout = 30000) {
|
|
296
|
+
const start = Date.now();
|
|
297
|
+
while (Date.now() - start < timeout) {
|
|
298
|
+
const execution = executions.get(runId);
|
|
299
|
+
if (execution && (execution.status === 'completed' || execution.status === 'failed' || execution.status === 'cancelled')) {
|
|
300
|
+
return execution;
|
|
301
|
+
}
|
|
302
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
303
|
+
}
|
|
304
|
+
throw new Error(`Timeout waiting for workflow ${runId}`);
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../cli/workflow/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAuHxC,gFAAgF;AAChF,iCAAiC;AACjC,gFAAgF;AAEhF,MAAM,UAAU,oBAAoB,CAAC,EAAU,EAAE,SAAwB;IACvE,MAAM,SAAS,GAAG,IAAI,GAAG,EAA0B,CAAA;IACnD,MAAM,UAAU,GAAG,IAAI,GAAG,EAA6B,CAAA;IACvD,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAA2B,CAAA;IAC3D,MAAM,kBAAkB,GAAmB,EAAE,CAAA;IAC7C,MAAM,SAAS,GAAG,IAAI,GAAG,EAAmC,CAAA;IAC5D,MAAM,aAAa,GAAG,IAAI,GAAG,EAAkB,CAAA;IAC/C,IAAI,UAAU,GAAG,KAAK,CAAA;IAEtB,+CAA+C;IAC/C,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAAiD,CAAA;IAEjF,MAAM,sBAAsB,GAAG,CAAC,OAAe,EAAE,IAAY,EAAE,MAAc,EAAW,EAAE;QACxF,MAAM,CAAC,WAAW,EAAE,aAAa,CAAC,GAAG,OA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CAAC,CAAA;YAC7C,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,gCAAgC,KAAK,EAAE,CAAC,CAAA;QAC1D,CAAC;KACF,CAAA;AACH,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error codes for schema validation errors.
|
|
3
|
+
*/
|
|
4
|
+
export type SchemaValidationErrorCode = 'MISSING_CONTEXT' | 'MISSING_NAMESPACE' | 'INVALID_FIELD_TYPE';
|
|
5
|
+
/**
|
|
6
|
+
* Error thrown when schema validation fails.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* try {
|
|
10
|
+
* DB({ User: { name: 'User name' } }) // Missing $id or $context
|
|
11
|
+
* } catch (error) {
|
|
12
|
+
* if (error instanceof SchemaValidationError) {
|
|
13
|
+
* console.log(error.code) // 'MISSING_CONTEXT'
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
*/
|
|
17
|
+
export declare class SchemaValidationError extends Error {
|
|
18
|
+
code: SchemaValidationErrorCode;
|
|
19
|
+
constructor(code: SchemaValidationErrorCode, message: string);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Error codes for API errors.
|
|
23
|
+
*/
|
|
24
|
+
export type APIErrorCode = 'REGISTRATION_FAILED' | 'GENERATION_FAILED' | 'REFERENCE_RESOLUTION_FAILED';
|
|
25
|
+
/**
|
|
26
|
+
* Error thrown when API operations fail.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* try {
|
|
30
|
+
* await db.User('john-doe')
|
|
31
|
+
* } catch (error) {
|
|
32
|
+
* if (error instanceof APIError) {
|
|
33
|
+
* console.log(error.code) // 'GENERATION_FAILED'
|
|
34
|
+
* console.log(error.statusCode) // 404
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
*/
|
|
38
|
+
export declare class APIError extends Error {
|
|
39
|
+
code: APIErrorCode;
|
|
40
|
+
statusCode?: number;
|
|
41
|
+
constructor(code: APIErrorCode, message: string, statusCode?: number);
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../errors.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,iBAAiB,GAAG,mBAAmB,GAAG,oBAAoB,CAAA;AAEtG;;;;;;;;;;;GAWG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C,IAAI,EAAE,yBAAyB,CAAA;gBAEnB,IAAI,EAAE,yBAAyB,EAAE,OAAO,EAAE,MAAM;CAK7D;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,qBAAqB,GAAG,mBAAmB,GAAG,6BAA6B,CAAA;AAEtG;;;;;;;;;;;;GAYG;AACH,qBAAa,QAAS,SAAQ,KAAK;IACjC,IAAI,EAAE,YAAY,CAAA;IAClB,UAAU,CAAC,EAAE,MAAM,CAAA;gBAEP,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;CAMrE"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Custom Error Classes
|
|
3
|
+
// ============================================================================
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when schema validation fails.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* try {
|
|
9
|
+
* DB({ User: { name: 'User name' } }) // Missing $id or $context
|
|
10
|
+
* } catch (error) {
|
|
11
|
+
* if (error instanceof SchemaValidationError) {
|
|
12
|
+
* console.log(error.code) // 'MISSING_CONTEXT'
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
*/
|
|
16
|
+
export class SchemaValidationError extends Error {
|
|
17
|
+
code;
|
|
18
|
+
constructor(code, message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'SchemaValidationError';
|
|
21
|
+
this.code = code;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Error thrown when API operations fail.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* try {
|
|
29
|
+
* await db.User('john-doe')
|
|
30
|
+
* } catch (error) {
|
|
31
|
+
* if (error instanceof APIError) {
|
|
32
|
+
* console.log(error.code) // 'GENERATION_FAILED'
|
|
33
|
+
* console.log(error.statusCode) // 404
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
*/
|
|
37
|
+
export class APIError extends Error {
|
|
38
|
+
code;
|
|
39
|
+
statusCode;
|
|
40
|
+
constructor(code, message, statusCode) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = 'APIError';
|
|
43
|
+
this.code = code;
|
|
44
|
+
this.statusCode = statusCode;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../errors.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,uBAAuB;AACvB,+EAA+E;AAO/E;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC9C,IAAI,CAA2B;IAE/B,YAAY,IAA+B,EAAE,OAAe;QAC1D,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAA;QACnC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;CACF;AAOD;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACjC,IAAI,CAAc;IAClB,UAAU,CAAS;IAEnB,YAAY,IAAkB,EAAE,OAAe,EAAE,UAAmB;QAClE,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,UAAU,CAAA;QACtB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;CACF"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serialized representation of a function for transport to server.
|
|
3
|
+
*
|
|
4
|
+
* Functions defined in schema definitions (like lifecycle handlers) are
|
|
5
|
+
* serialized into this format for execution on the server side.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const serialized: SerializedFunction = {
|
|
10
|
+
* name: '$created',
|
|
11
|
+
* body: '(entity, $) => $.OtherType(entity.name)',
|
|
12
|
+
* params: ['entity', '$'],
|
|
13
|
+
* async: true
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export interface SerializedFunction {
|
|
18
|
+
/** The name of the function (e.g., '$created', 'validateUser') */
|
|
19
|
+
name: string;
|
|
20
|
+
/** The stringified function body */
|
|
21
|
+
body: string;
|
|
22
|
+
/** Array of parameter names extracted from the function signature */
|
|
23
|
+
params: string[];
|
|
24
|
+
/** Whether the function is async */
|
|
25
|
+
async: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Serialize a function for server-side execution.
|
|
29
|
+
*
|
|
30
|
+
* Converts a JavaScript function into a serializable format that can be
|
|
31
|
+
* sent to the server and executed in a sandboxed environment.
|
|
32
|
+
*
|
|
33
|
+
* @param name - The name to assign to the serialized function
|
|
34
|
+
* @param fn - The function to serialize
|
|
35
|
+
* @returns A SerializedFunction object
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const handler = async (entity: any, $: any) => {
|
|
40
|
+
* await $.OtherType(entity.name)
|
|
41
|
+
* }
|
|
42
|
+
* const serialized = serializeFunction('$created', handler)
|
|
43
|
+
* // { name: '$created', body: 'async (entity, $) => {...}', params: ['entity', '$'], async: true }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function serializeFunction(name: string, fn: Function): SerializedFunction;
|
|
47
|
+
/**
|
|
48
|
+
* Entity-level lifecycle handler types.
|
|
49
|
+
*
|
|
50
|
+
* These handlers are triggered on individual entity operations:
|
|
51
|
+
* - `$created` - Called after a new entity is created
|
|
52
|
+
* - `$updated` - Called after an entity is updated
|
|
53
|
+
* - `$deleted` - Called after an entity is deleted
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* DB({
|
|
58
|
+
* User: {
|
|
59
|
+
* name: 'User name',
|
|
60
|
+
* $created: async (entity, $) => {
|
|
61
|
+
* await $.WelcomeEmail({ to: entity.email })
|
|
62
|
+
* }
|
|
63
|
+
* }
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export type EntityHandlerType = '$created' | '$updated' | '$deleted';
|
|
68
|
+
/**
|
|
69
|
+
* Schema-level lifecycle handler types.
|
|
70
|
+
*
|
|
71
|
+
* These handlers are triggered on schema-wide events:
|
|
72
|
+
* - `$seeded` - Called after seed data is loaded for a type
|
|
73
|
+
* - `$ready` - Called when the schema is fully initialized
|
|
74
|
+
* - `$error` - Called when a schema-level error occurs
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* DB({
|
|
79
|
+
* $seeded: async ($) => {
|
|
80
|
+
* console.log('Seed data loaded')
|
|
81
|
+
* }
|
|
82
|
+
* })
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export type SchemaHandlerType = '$seeded' | '$ready' | '$error';
|
|
86
|
+
/**
|
|
87
|
+
* All lifecycle handler types (entity + schema level).
|
|
88
|
+
*
|
|
89
|
+
* This union type includes all handler types that can be defined:
|
|
90
|
+
* - Entity handlers: `$created`, `$updated`, `$deleted`
|
|
91
|
+
* - Schema handlers: `$seeded`, `$ready`, `$error`
|
|
92
|
+
*/
|
|
93
|
+
export type LifecycleHandlerType = EntityHandlerType | SchemaHandlerType;
|
|
94
|
+
/**
|
|
95
|
+
* State machine handler types.
|
|
96
|
+
*
|
|
97
|
+
* These handlers are triggered during state transitions:
|
|
98
|
+
* - `$entry` - Called when entering a state
|
|
99
|
+
* - `$exit` - Called when exiting a state
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* DB({
|
|
104
|
+
* Order: {
|
|
105
|
+
* $state: {
|
|
106
|
+
* $initial: 'pending',
|
|
107
|
+
* pending: {
|
|
108
|
+
* $entry: (entity) => console.log('Order is now pending'),
|
|
109
|
+
* confirm: 'confirmed'
|
|
110
|
+
* }
|
|
111
|
+
* }
|
|
112
|
+
* }
|
|
113
|
+
* })
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export type StateHandlerType = '$entry' | '$exit';
|
|
117
|
+
/**
|
|
118
|
+
* Handler execution result from the sandboxed environment.
|
|
119
|
+
*
|
|
120
|
+
* Contains the outcome of executing a serialized handler function
|
|
121
|
+
* in an isolated V8 sandbox.
|
|
122
|
+
*/
|
|
123
|
+
export interface HandlerExecutionResult {
|
|
124
|
+
/** Whether execution completed without error */
|
|
125
|
+
success: boolean;
|
|
126
|
+
/** Return value from the handler (if any) */
|
|
127
|
+
result?: unknown;
|
|
128
|
+
/** Error message if execution failed */
|
|
129
|
+
error?: string;
|
|
130
|
+
/** Execution time in milliseconds */
|
|
131
|
+
duration: number;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Log entry captured during handler execution.
|
|
135
|
+
*
|
|
136
|
+
* Console output from handlers is captured and returned
|
|
137
|
+
* for debugging and monitoring purposes.
|
|
138
|
+
*/
|
|
139
|
+
export interface HandlerLogEntry {
|
|
140
|
+
/** Log level (log, warn, error, info, debug) */
|
|
141
|
+
level: 'log' | 'warn' | 'error' | 'info' | 'debug';
|
|
142
|
+
/** The log message content */
|
|
143
|
+
message: string;
|
|
144
|
+
/** Timestamp when the log was created */
|
|
145
|
+
timestamp: number;
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=handlers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../handlers.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,kBAAkB;IACjC,kEAAkE;IAClE,IAAI,EAAE,MAAM,CAAA;IACZ,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAA;IACZ,qEAAqE;IACrE,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,oCAAoC;IACpC,KAAK,EAAE,OAAO,CAAA;CACf;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,kBAAkB,CAYhF;AAMD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,iBAAiB,GAAG,UAAU,GAAG,UAAU,GAAG,UAAU,CAAA;AAEpE;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,iBAAiB,GAAG,SAAS,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAE/D;;;;;;GAMG;AACH,MAAM,MAAM,oBAAoB,GAAG,iBAAiB,GAAG,iBAAiB,CAAA;AAExE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG,OAAO,CAAA;AAEjD;;;;;GAKG;AACH,MAAM,WAAW,sBAAsB;IACrC,gDAAgD;IAChD,OAAO,EAAE,OAAO,CAAA;IAChB,6CAA6C;IAC7C,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,wCAAwC;IACxC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,qCAAqC;IACrC,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B,gDAAgD;IAChD,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,CAAA;IAClD,8BAA8B;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,yCAAyC;IACzC,SAAS,EAAE,MAAM,CAAA;CAClB"}
|
package/dist/handlers.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Handler Types
|
|
3
|
+
//
|
|
4
|
+
// Shared types for handler-related functionality used by both SDK and API.
|
|
5
|
+
// This module defines types for function serialization and lifecycle handlers.
|
|
6
|
+
// ============================================================================
|
|
7
|
+
/**
|
|
8
|
+
* Serialize a function for server-side execution.
|
|
9
|
+
*
|
|
10
|
+
* Converts a JavaScript function into a serializable format that can be
|
|
11
|
+
* sent to the server and executed in a sandboxed environment.
|
|
12
|
+
*
|
|
13
|
+
* @param name - The name to assign to the serialized function
|
|
14
|
+
* @param fn - The function to serialize
|
|
15
|
+
* @returns A SerializedFunction object
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const handler = async (entity: any, $: any) => {
|
|
20
|
+
* await $.OtherType(entity.name)
|
|
21
|
+
* }
|
|
22
|
+
* const serialized = serializeFunction('$created', handler)
|
|
23
|
+
* // { name: '$created', body: 'async (entity, $) => {...}', params: ['entity', '$'], async: true }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function serializeFunction(name, fn) {
|
|
27
|
+
const fnStr = fn.toString();
|
|
28
|
+
const isAsync = fnStr.trimStart().startsWith('async');
|
|
29
|
+
let params = [];
|
|
30
|
+
const paramMatch = fnStr.match(/^(?:async\s*)?(?:function\s*\w*)?\s*\(([^)]*)\)/);
|
|
31
|
+
if (paramMatch && paramMatch[1]) {
|
|
32
|
+
params = paramMatch[1]
|
|
33
|
+
.split(',')
|
|
34
|
+
.map((p) => p.trim().split(':')[0].split('=')[0].trim())
|
|
35
|
+
.filter(Boolean);
|
|
36
|
+
}
|
|
37
|
+
return { name, body: fnStr, params, async: isAsync };
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=handlers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlers.js","sourceRoot":"","sources":["../handlers.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,gBAAgB;AAChB,EAAE;AACF,2EAA2E;AAC3E,+EAA+E;AAC/E,+EAA+E;AA6B/E;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY,EAAE,EAAY;IAC1D,MAAM,KAAK,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAA;IAC3B,MAAM,OAAO,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,CAAA;IACrD,IAAI,MAAM,GAAa,EAAE,CAAA;IACzB,MAAM,UAAU,GAAG,KAAK,CAAC,KAAK,CAAC,iDAAiD,CAAC,CAAA;IACjF,IAAI,UAAU,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;QAChC,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC;aACnB,KAAK,CAAC,GAAG,CAAC;aACV,GAAG,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;aAC/D,MAAM,CAAC,OAAO,CAAC,CAAA;IACpB,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,CAAA;AACtD,CAAC"}
|