-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine.ts
More file actions
627 lines (552 loc) · 22.6 KB
/
engine.ts
File metadata and controls
627 lines (552 loc) · 22.6 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
import { QueryAST, HookContext } from '@objectstack/spec/data';
import {
DataEngineQueryOptions,
DataEngineInsertOptions,
DataEngineUpdateOptions,
DataEngineDeleteOptions,
DataEngineAggregateOptions,
DataEngineCountOptions
} from '@objectstack/spec/data';
import { DriverInterface, IDataEngine, Logger, createLogger } from '@objectstack/core';
import { CoreServiceName } from '@objectstack/spec/system';
import { SchemaRegistry } from './registry.js';
export type HookHandler = (context: HookContext) => Promise<void> | void;
/**
* Host Context provided to plugins (Internal ObjectQL Plugin System)
*/
export interface ObjectQLHostContext {
ql: ObjectQL;
logger: Logger;
// Extensible map for host-specific globals (like HTTP Router, etc.)
[key: string]: any;
}
/**
* ObjectQL Engine
*
* Implements the IDataEngine interface for data persistence.
* Acts as the reference implementation for:
* - CoreServiceName.data (CRUD)
* - CoreServiceName.metadata (Schema Registry)
*/
export class ObjectQL implements IDataEngine {
private drivers = new Map<string, DriverInterface>();
private defaultDriver: string | null = null;
private logger: Logger;
// Hooks Registry
private hooks: Record<string, HookHandler[]> = {
'beforeFind': [], 'afterFind': [],
'beforeInsert': [], 'afterInsert': [],
'beforeUpdate': [], 'afterUpdate': [],
'beforeDelete': [], 'afterDelete': [],
};
// Host provided context additions (e.g. Server router)
private hostContext: Record<string, any> = {};
constructor(hostContext: Record<string, any> = {}) {
this.hostContext = hostContext;
// Use provided logger or create a new one
this.logger = hostContext.logger || createLogger({ level: 'info', format: 'pretty' });
this.logger.info('ObjectQL Engine Instance Created');
}
/**
* Service Status Report
* Used by Kernel to verify health and capabilities.
*/
getStatus() {
return {
name: CoreServiceName.enum.data,
status: 'running',
version: '0.9.0',
features: ['crud', 'query', 'aggregate', 'transactions', 'metadata']
};
}
/**
* Expose the SchemaRegistry for plugins to register metadata
*/
get registry() {
return SchemaRegistry;
}
/**
* Load and Register a Plugin
*/
async use(manifestPart: any, runtimePart?: any) {
this.logger.debug('Loading plugin', {
hasManifest: !!manifestPart,
hasRuntime: !!runtimePart
});
// 1. Validate / Register Manifest
if (manifestPart) {
this.registerApp(manifestPart);
}
// 2. Execute Runtime
if (runtimePart) {
const pluginDef = (runtimePart as any).default || runtimePart;
if (pluginDef.onEnable) {
this.logger.debug('Executing plugin runtime onEnable');
const context: ObjectQLHostContext = {
ql: this,
logger: this.logger,
// Expose the driver registry helper explicitly if needed
drivers: {
register: (driver: DriverInterface) => this.registerDriver(driver)
},
...this.hostContext
};
await pluginDef.onEnable(context);
this.logger.debug('Plugin runtime onEnable completed');
}
}
}
/**
* Register a hook
* @param event The event name (e.g. 'beforeFind', 'afterInsert')
* @param handler The handler function
*/
registerHook(event: string, handler: HookHandler) {
if (!this.hooks[event]) {
this.hooks[event] = [];
}
this.hooks[event].push(handler);
this.logger.debug('Registered hook', { event, totalHandlers: this.hooks[event].length });
}
public async triggerHooks(event: string, context: HookContext) {
const handlers = this.hooks[event] || [];
if (handlers.length === 0) {
this.logger.debug('No hooks registered for event', { event });
return;
}
this.logger.debug('Triggering hooks', { event, count: handlers.length });
for (const handler of handlers) {
await handler(context);
}
}
/**
* Register contribution (Manifest)
*
* Installs the manifest as a Package (the unit of installation),
* then decomposes it into individual metadata items (objects, apps, actions, etc.)
* and registers each into the SchemaRegistry.
*
* Key: Package ≠ App. The manifest is the package. The apps[] array inside
* the manifest contains UI navigation definitions (AppSchema).
*/
registerApp(manifest: any) {
const id = manifest.id || manifest.name;
const namespace = manifest.namespace as string | undefined;
this.logger.debug('Registering package manifest', { id, namespace });
// 1. Register the Package (manifest + lifecycle state)
SchemaRegistry.installPackage(manifest);
this.logger.debug('Installed Package', { id: manifest.id, name: manifest.name, namespace });
// 2. Register owned objects
if (manifest.objects) {
if (Array.isArray(manifest.objects)) {
this.logger.debug('Registering objects from manifest (Array)', { id, objectCount: manifest.objects.length });
for (const objDef of manifest.objects) {
const fqn = SchemaRegistry.registerObject(objDef, id, namespace, 'own');
this.logger.debug('Registered Object', { fqn, from: id });
}
} else {
this.logger.debug('Registering objects from manifest (Map)', { id, objectCount: Object.keys(manifest.objects).length });
for (const [name, objDef] of Object.entries(manifest.objects)) {
// Ensure name in definition matches key
(objDef as any).name = name;
const fqn = SchemaRegistry.registerObject(objDef as any, id, namespace, 'own');
this.logger.debug('Registered Object', { fqn, from: id });
}
}
}
// 2b. Register object extensions (fields added to objects owned by other packages)
if (Array.isArray(manifest.objectExtensions) && manifest.objectExtensions.length > 0) {
this.logger.debug('Registering object extensions', { id, count: manifest.objectExtensions.length });
for (const ext of manifest.objectExtensions) {
const targetFqn = ext.extend;
const priority = ext.priority ?? 200;
// Create a partial object definition for the extension
const extDef = {
name: targetFqn, // Use the target FQN as name
fields: ext.fields,
label: ext.label,
pluralLabel: ext.pluralLabel,
description: ext.description,
validations: ext.validations,
indexes: ext.indexes,
};
// Register as extension (namespace is undefined since we're targeting by FQN)
SchemaRegistry.registerObject(extDef as any, id, undefined, 'extend', priority);
this.logger.debug('Registered Object Extension', { target: targetFqn, priority, from: id });
}
}
// 3. Register apps (UI navigation definitions) as their own metadata type
if (Array.isArray(manifest.apps) && manifest.apps.length > 0) {
this.logger.debug('Registering apps from manifest', { id, count: manifest.apps.length });
for (const app of manifest.apps) {
const appName = app.name || app.id;
if (appName) {
SchemaRegistry.registerApp(app, id);
this.logger.debug('Registered App', { app: appName, from: id });
}
}
}
// 4. If manifest itself looks like an App (has navigation), also register as app
// This handles the case where the manifest IS the app definition (legacy/simple packages)
if (manifest.name && manifest.navigation && !manifest.apps?.length) {
SchemaRegistry.registerApp(manifest, id);
this.logger.debug('Registered manifest-as-app', { app: manifest.name, from: id });
}
// 5. Register all other metadata types generically
const metadataArrayKeys = [
// UI Protocol
'actions', 'views', 'pages', 'dashboards', 'reports', 'themes',
// Automation Protocol
'flows', 'workflows', 'approvals', 'webhooks',
// Security Protocol
'roles', 'permissions', 'profiles', 'sharingRules', 'policies',
// AI Protocol
'agents', 'ragPipelines',
// API Protocol
'apis',
// Data Extensions
'hooks', 'mappings', 'analyticsCubes',
// Integration Protocol
'connectors',
];
for (const key of metadataArrayKeys) {
const items = (manifest as any)[key];
if (Array.isArray(items) && items.length > 0) {
this.logger.debug(`Registering ${key} from manifest`, { id, count: items.length });
for (const item of items) {
const itemName = item.name || item.id;
if (itemName) {
SchemaRegistry.registerItem(key, item, 'name' as any, id);
}
}
}
}
// 6. Register seed data as metadata (keyed by target object name)
const seedData = (manifest as any).data;
if (Array.isArray(seedData) && seedData.length > 0) {
this.logger.debug('Registering seed data datasets', { id, count: seedData.length });
for (const dataset of seedData) {
if (dataset.object) {
SchemaRegistry.registerItem('data', dataset, 'object' as any, id);
}
}
}
// 6. Register contributions
if (manifest.contributes?.kinds) {
this.logger.debug('Registering kinds from manifest', { id, kindCount: manifest.contributes.kinds.length });
for (const kind of manifest.contributes.kinds) {
SchemaRegistry.registerKind(kind);
this.logger.debug('Registered Kind', { kind: kind.name || kind.type, from: id });
}
}
}
/**
* Register a new storage driver
*/
registerDriver(driver: DriverInterface, isDefault: boolean = false) {
if (this.drivers.has(driver.name)) {
this.logger.warn('Driver already registered, skipping', { driverName: driver.name });
return;
}
this.drivers.set(driver.name, driver);
this.logger.info('Registered driver', {
driverName: driver.name,
version: driver.version
});
if (isDefault || this.drivers.size === 1) {
this.defaultDriver = driver.name;
this.logger.info('Set default driver', { driverName: driver.name });
}
}
/**
* Helper to get object definition
*/
getSchema(objectName: string) {
return SchemaRegistry.getObject(objectName);
}
/**
* Resolve an object name to its Fully Qualified Name (FQN).
*
* Short names like 'task' are resolved to FQN like 'todo__task'
* via SchemaRegistry lookup. If no match is found, the name is
* returned as-is (for ad-hoc / unregistered objects).
*
* This ensures that all driver operations use a consistent key
* regardless of whether the caller uses the short name or FQN.
*/
private resolveObjectName(name: string): string {
const schema = SchemaRegistry.getObject(name);
if (schema) {
return schema.name; // FQN from registry (e.g., 'todo__task')
}
return name; // Ad-hoc object, keep as-is
}
/**
* Helper to get the target driver
*/
private getDriver(objectName: string): DriverInterface {
const object = SchemaRegistry.getObject(objectName);
// 1. If object definition exists, check for explicit datasource
if (object) {
const datasourceName = object.datasource || 'default';
// If configured for 'default', try to find the default driver
if (datasourceName === 'default') {
if (this.defaultDriver && this.drivers.has(this.defaultDriver)) {
return this.drivers.get(this.defaultDriver)!;
}
} else {
// Specific datasource requested
if (this.drivers.has(datasourceName)) {
return this.drivers.get(datasourceName)!;
}
throw new Error(`[ObjectQL] Datasource '${datasourceName}' configured for object '${objectName}' is not registered.`);
}
}
// 2. Fallback for ad-hoc objects or missing definitions
if (this.defaultDriver) {
return this.drivers.get(this.defaultDriver)!;
}
throw new Error(`[ObjectQL] No driver available for object '${objectName}'`);
}
/**
* Initialize the engine and all registered drivers
*/
async init() {
this.logger.info('Initializing ObjectQL engine', {
driverCount: this.drivers.size,
drivers: Array.from(this.drivers.keys())
});
for (const [name, driver] of this.drivers) {
try {
await driver.connect();
this.logger.info('Driver connected successfully', { driverName: name });
} catch (e) {
this.logger.error('Failed to connect driver', e as Error, { driverName: name });
}
}
this.logger.info('ObjectQL engine initialization complete');
}
async destroy() {
this.logger.info('Destroying ObjectQL engine', { driverCount: this.drivers.size });
for (const [name, driver] of this.drivers.entries()) {
try {
await driver.disconnect();
} catch (e) {
this.logger.error('Error disconnecting driver', e as Error, { driverName: name });
}
}
this.logger.info('ObjectQL engine destroyed');
}
// ============================================
// Helper: Query Conversion
// ============================================
private toQueryAST(object: string, options?: DataEngineQueryOptions): QueryAST {
const ast: QueryAST = { object };
if (!options) return ast;
if (options.filter) {
ast.where = options.filter;
}
if (options.select) {
ast.fields = options.select;
}
if (options.sort) {
// Support DataEngineSortSchema variant
if (Array.isArray(options.sort)) {
// [{ field: 'a', order: 'asc' }]
ast.orderBy = options.sort;
} else {
// Record<string, 'asc' | 'desc' | 1 | -1>
ast.orderBy = Object.entries(options.sort).map(([field, order]) => ({
field,
order: (order === -1 || order === 'desc') ? 'desc' : 'asc'
}));
}
}
if (options.top !== undefined) ast.limit = options.top;
else if (options.limit !== undefined) ast.limit = options.limit;
if (options.skip !== undefined) ast.offset = options.skip;
// TODO: Handle populate/joins mapping if Driver supports it in QueryAST
return ast;
}
// ============================================
// Data Access Methods (IDataEngine Interface)
// ============================================
async find(object: string, query?: DataEngineQueryOptions): Promise<any[]> {
object = this.resolveObjectName(object);
this.logger.debug('Find operation starting', { object, query });
const driver = this.getDriver(object);
const ast = this.toQueryAST(object, query);
const hookContext: HookContext = {
object,
event: 'beforeFind',
input: { ast, options: undefined }, // Should map options?
ql: this
};
await this.triggerHooks('beforeFind', hookContext);
try {
const result = await driver.find(object, hookContext.input.ast as QueryAST, hookContext.input.options as any);
hookContext.event = 'afterFind';
hookContext.result = result;
await this.triggerHooks('afterFind', hookContext);
return hookContext.result as any[];
} catch (e) {
this.logger.error('Find operation failed', e as Error, { object });
throw e;
}
}
async findOne(objectName: string, query?: DataEngineQueryOptions): Promise<any> {
objectName = this.resolveObjectName(objectName);
this.logger.debug('FindOne operation', { objectName });
const driver = this.getDriver(objectName);
const ast = this.toQueryAST(objectName, query);
ast.limit = 1;
// Reuse find logic or call generic driver.findOne if available
// Assuming driver has findOne
return driver.findOne(objectName, ast);
}
async insert(object: string, data: any | any[], options?: DataEngineInsertOptions): Promise<any> {
object = this.resolveObjectName(object);
this.logger.debug('Insert operation starting', { object, isBatch: Array.isArray(data) });
const driver = this.getDriver(object);
const hookContext: HookContext = {
object,
event: 'beforeInsert',
input: { data, options },
ql: this
};
await this.triggerHooks('beforeInsert', hookContext);
try {
let result;
if (Array.isArray(hookContext.input.data)) {
// Bulk Create
if (driver.bulkCreate) {
result = await driver.bulkCreate(object, hookContext.input.data as any[], hookContext.input.options as any);
} else {
// Fallback loop
result = await Promise.all((hookContext.input.data as any[]).map((item: any) => driver.create(object, item, hookContext.input.options as any)));
}
} else {
result = await driver.create(object, hookContext.input.data, hookContext.input.options as any);
}
hookContext.event = 'afterInsert';
hookContext.result = result;
await this.triggerHooks('afterInsert', hookContext);
return hookContext.result;
} catch (e) {
this.logger.error('Insert operation failed', e as Error, { object });
throw e;
}
}
async update(object: string, data: any, options?: DataEngineUpdateOptions): Promise<any> {
object = this.resolveObjectName(object);
// NOTE: This signature is tricky because Driver expects (obj, id, data) usually.
// DataEngine protocol puts filter in options.
this.logger.debug('Update operation starting', { object });
const driver = this.getDriver(object);
// 1. Extract ID from data or filter if it's a single update by ID
// This is a simplification. Real implementation needs robust filter handling.
let id = data.id || data._id;
if (!id && options?.filter) {
// Optimization: If filter is simple ID check, extract it
if (typeof options.filter === 'string') id = options.filter;
else if (options.filter._id) id = options.filter._id;
else if (options.filter.id) id = options.filter.id;
}
const hookContext: HookContext = {
object,
event: 'beforeUpdate',
input: { id, data, options },
ql: this
};
await this.triggerHooks('beforeUpdate', hookContext);
try {
let result;
if (hookContext.input.id) {
// Single update by ID
result = await driver.update(object, hookContext.input.id as string, hookContext.input.data, hookContext.input.options as any);
} else if (options?.multi && driver.updateMany) {
// Bulk update by Query
const ast = this.toQueryAST(object, { filter: options.filter });
result = await driver.updateMany(object, ast, hookContext.input.data, hookContext.input.options as any);
} else {
throw new Error('Update requires an ID or options.multi=true');
}
hookContext.event = 'afterUpdate';
hookContext.result = result;
await this.triggerHooks('afterUpdate', hookContext);
return hookContext.result;
} catch (e) {
this.logger.error('Update operation failed', e as Error, { object });
throw e;
}
}
async delete(object: string, options?: DataEngineDeleteOptions): Promise<any> {
object = this.resolveObjectName(object);
this.logger.debug('Delete operation starting', { object });
const driver = this.getDriver(object);
// Extract ID logic similar to update
let id: any = undefined;
if (options?.filter) {
if (typeof options.filter === 'string') id = options.filter;
else if (options.filter._id) id = options.filter._id;
else if (options.filter.id) id = options.filter.id;
}
const hookContext: HookContext = {
object,
event: 'beforeDelete',
input: { id, options },
ql: this
};
await this.triggerHooks('beforeDelete', hookContext);
try {
let result;
if (hookContext.input.id) {
result = await driver.delete(object, hookContext.input.id as string, hookContext.input.options as any);
} else if (options?.multi && driver.deleteMany) {
const ast = this.toQueryAST(object, { filter: options.filter });
result = await driver.deleteMany(object, ast, hookContext.input.options as any);
} else {
throw new Error('Delete requires an ID or options.multi=true');
}
hookContext.event = 'afterDelete';
hookContext.result = result;
await this.triggerHooks('afterDelete', hookContext);
return hookContext.result;
} catch (e) {
this.logger.error('Delete operation failed', e as Error, { object });
throw e;
}
}
async count(object: string, query?: DataEngineCountOptions): Promise<number> {
object = this.resolveObjectName(object);
const driver = this.getDriver(object);
if (driver.count) {
const ast = this.toQueryAST(object, { filter: query?.filter });
return driver.count(object, ast);
}
// Fallback to find().length
const res = await this.find(object, { filter: query?.filter, select: ['_id'] });
return res.length;
}
async aggregate(object: string, query: DataEngineAggregateOptions): Promise<any[]> {
object = this.resolveObjectName(object);
const driver = this.getDriver(object);
this.logger.debug(`Aggregate on ${object} using ${driver.name}`, query);
// Driver needs support for raw aggregation or mapped aggregation
// For now, if driver supports 'execute', we might pass it down, or we need to add 'aggregate' to DriverInterface
// In this version, we'll assume driver might handle it via special 'find' or throw not implemented
throw new Error('Aggregate not yet fully implemented in ObjectQL->Driver mapping');
}
async execute(command: any, options?: Record<string, any>): Promise<any> {
// Direct pass-through implies we know which driver to use?
// Usually execute is tied to a specific object context OR we need a way to select driver.
// If command has 'object', we use that.
if (options?.object) {
const driver = this.getDriver(options.object);
if (driver.execute) {
return driver.execute(command, undefined, options);
}
}
throw new Error('Execute requires options.object to select driver');
}
}