-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine.ts
More file actions
1452 lines (1285 loc) · 48.7 KB
/
engine.ts
File metadata and controls
1452 lines (1285 loc) · 48.7 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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { QueryAST, HookContext, ServiceObject } from '@objectstack/spec/data';
import {
EngineQueryOptions,
DataEngineInsertOptions,
EngineUpdateOptions,
EngineDeleteOptions,
EngineAggregateOptions,
EngineCountOptions
} from '@objectstack/spec/data';
import { ExecutionContext, ExecutionContextSchema } from '@objectstack/spec/kernel';
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;
/**
* Per-object hook entry with priority support
*/
export interface HookEntry {
handler: HookHandler;
object?: string | string[]; // undefined = global hook
priority: number;
packageId?: string;
}
/**
* Operation Context for Middleware Chain
*/
export interface OperationContext {
object: string;
operation: 'find' | 'findOne' | 'insert' | 'update' | 'delete' | 'count' | 'aggregate';
ast?: QueryAST;
data?: any;
options?: any;
context?: ExecutionContext;
result?: any;
}
/**
* Engine Middleware (Onion model)
*/
export type EngineMiddleware = (
ctx: OperationContext,
next: () => Promise<void>
) => Promise<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;
// Per-object hooks with priority support
private hooks: Map<string, HookEntry[]> = new Map([
['beforeFind', []], ['afterFind', []],
['beforeInsert', []], ['afterInsert', []],
['beforeUpdate', []], ['afterUpdate', []],
['beforeDelete', []], ['afterDelete', []],
]);
// Middleware chain (onion model)
private middlewares: Array<{
fn: EngineMiddleware;
object?: string;
}> = [];
// Action registry: key = "objectName:actionName"
private actions = new Map<string, { handler: (ctx: any) => Promise<any> | any; package?: string }>();
// 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
* @param options Optional: target object(s) and priority
*/
registerHook(event: string, handler: HookHandler, options?: {
object?: string | string[];
priority?: number;
packageId?: string;
}) {
if (!this.hooks.has(event)) {
this.hooks.set(event, []);
}
const entries = this.hooks.get(event)!;
entries.push({
handler,
object: options?.object,
priority: options?.priority ?? 100,
packageId: options?.packageId,
});
// Sort by priority (lower runs first)
entries.sort((a, b) => a.priority - b.priority);
this.logger.debug('Registered hook', { event, object: options?.object, priority: options?.priority ?? 100, totalHandlers: entries.length });
}
public async triggerHooks(event: string, context: HookContext) {
const entries = this.hooks.get(event) || [];
if (entries.length === 0) {
this.logger.debug('No hooks registered for event', { event });
return;
}
this.logger.debug('Triggering hooks', { event, count: entries.length });
for (const entry of entries) {
// Per-object matching
if (entry.object) {
const targets = Array.isArray(entry.object) ? entry.object : [entry.object];
if (!targets.includes('*') && !targets.includes(context.object)) {
continue; // Skip non-matching hooks
}
}
await entry.handler(context);
}
}
// ========================================
// Action System
// ========================================
/**
* Register a named action on an object.
* Actions are custom business logic callable via `repo.execute(actionName, params)`.
*
* @param objectName Target object
* @param actionName Unique action name within the object
* @param handler Handler function
* @param packageName Optional package owner (for cleanup)
*/
registerAction(objectName: string, actionName: string, handler: (ctx: any) => Promise<any> | any, packageName?: string): void {
const key = `${objectName}:${actionName}`;
this.actions.set(key, { handler, package: packageName });
this.logger.debug('Registered action', { objectName, actionName, package: packageName });
}
/**
* Execute a named action on an object.
*/
async executeAction(objectName: string, actionName: string, ctx: any): Promise<any> {
const entry = this.actions.get(`${objectName}:${actionName}`);
if (!entry) {
throw new Error(`Action '${actionName}' on object '${objectName}' not found`);
}
return entry.handler(ctx);
}
/**
* Remove all actions registered by a specific package.
*/
removeActionsByPackage(packageName: string): void {
for (const [key, entry] of this.actions.entries()) {
if (entry.package === packageName) {
this.actions.delete(key);
}
}
}
/**
* Register a middleware function
* Middlewares execute in onion model around every data operation.
* @param fn The middleware function
* @param options Optional: target object filter
*/
registerMiddleware(fn: EngineMiddleware, options?: { object?: string }): void {
this.middlewares.push({ fn, object: options?.object });
this.logger.debug('Registered middleware', { object: options?.object, total: this.middlewares.length });
}
/**
* Execute an operation through the middleware chain
*/
private async executeWithMiddleware(ctx: OperationContext, executor: () => Promise<any>): Promise<any> {
const applicable = this.middlewares.filter(m =>
!m.object || m.object === '*' || m.object === ctx.object
);
let index = 0;
const next = async (): Promise<void> => {
if (index < applicable.length) {
const mw = applicable[index++];
await mw.fn(ctx, next);
} else {
ctx.result = await executor();
}
};
await next();
return ctx.result;
}
/**
* Build a HookContext.session from ExecutionContext
*/
private buildSession(execCtx?: ExecutionContext): HookContext['session'] {
if (!execCtx) return undefined;
return {
userId: execCtx.userId,
tenantId: execCtx.tenantId,
roles: execCtx.roles,
accessToken: execCtx.accessToken,
};
}
/**
* 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 });
}
}
// 7. Recursively register nested plugins
if (Array.isArray(manifest.plugins) && manifest.plugins.length > 0) {
this.logger.debug('Processing nested plugins', { id, count: manifest.plugins.length });
for (const plugin of manifest.plugins) {
if (plugin && typeof plugin === 'object') {
const pluginName = plugin.name || plugin.id || 'unnamed-plugin';
this.logger.debug('Registering nested plugin', { pluginName, parentId: id });
this.registerPlugin(plugin, id, namespace);
}
}
}
}
/**
* Register a nested plugin's metadata (objects, actions, views, etc.)
*
* Unlike registerApp(), this does NOT call SchemaRegistry.installPackage()
* because plugins are not formal manifests — they are lightweight config
* bundles with objects, actions, triggers, and navigation.
*
* @param plugin - The plugin config object
* @param parentId - The parent package ID (for ownership tracking)
* @param parentNamespace - The parent package's namespace (for FQN resolution)
*/
private registerPlugin(plugin: any, parentId: string, parentNamespace?: string) {
const pluginName = plugin.name || plugin.id || 'unnamed';
const pluginNamespace = plugin.namespace || parentNamespace;
// Use parentId as the owning package for namespace consistency.
// The parent package already claimed the namespace — nested plugins
// contribute objects UNDER the parent's ownership.
const ownerId = parentId;
// Register objects (supports both Array and Map formats)
if (plugin.objects) {
try {
if (Array.isArray(plugin.objects)) {
this.logger.debug('Registering plugin objects (Array)', { pluginName, count: plugin.objects.length });
for (const objDef of plugin.objects) {
const fqn = SchemaRegistry.registerObject(objDef, ownerId, pluginNamespace, 'own');
this.logger.debug('Registered Object', { fqn, from: pluginName });
}
} else {
const entries = Object.entries(plugin.objects);
this.logger.debug('Registering plugin objects (Map)', { pluginName, count: entries.length });
for (const [name, objDef] of entries) {
(objDef as any).name = name;
const fqn = SchemaRegistry.registerObject(objDef as any, ownerId, pluginNamespace, 'own');
this.logger.debug('Registered Object', { fqn, from: pluginName });
}
}
} catch (err: any) {
this.logger.warn('Failed to register plugin objects', { pluginName, error: err.message });
}
}
// Register plugin as app if it has navigation (for sidebar display)
if (plugin.name && plugin.navigation) {
try {
SchemaRegistry.registerApp(plugin, ownerId);
this.logger.debug('Registered plugin-as-app', { app: plugin.name, from: pluginName });
} catch (err: any) {
this.logger.warn('Failed to register plugin as app', { pluginName, error: err.message });
}
}
// Register metadata arrays (actions, views, triggers, etc.)
const metadataArrayKeys = [
'actions', 'views', 'pages', 'dashboards', 'reports', 'themes',
'flows', 'workflows', 'approvals', 'webhooks',
'roles', 'permissions', 'profiles', 'sharingRules', 'policies',
'agents', 'ragPipelines', 'apis',
'hooks', 'mappings', 'analyticsCubes', 'connectors',
];
for (const key of metadataArrayKeys) {
const items = (plugin as any)[key];
if (Array.isArray(items) && items.length > 0) {
for (const item of items) {
const itemName = item.name || item.id;
if (itemName) {
SchemaRegistry.registerItem(key, item, 'name' as any, ownerId);
}
}
}
}
}
/**
* 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): ServiceObject | undefined {
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) {
// Prefer the physical table name (e.g., 'sys_user') over the FQN
// (e.g., 'sys__user'). ObjectSchema.create() auto-derives tableName
// as {namespace}_{name} which matches the storage convention.
return schema.tableName || schema.name;
}
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())
});
const failedDrivers: string[] = [];
for (const [name, driver] of this.drivers) {
try {
await driver.connect();
this.logger.info('Driver connected successfully', { driverName: name });
} catch (e) {
failedDrivers.push(name);
this.logger.error('Failed to connect driver', e as Error, { driverName: name });
}
}
if (failedDrivers.length > 0) {
this.logger.warn(
`${failedDrivers.length} of ${this.drivers.size} driver(s) failed initial connect. ` +
`Operations may recover via lazy reconnection or fail at query time.`,
{ failedDrivers }
);
}
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: Expand Related Records
// ============================================
/** Maximum depth for recursive expand to prevent infinite loops */
private static readonly MAX_EXPAND_DEPTH = 3;
/**
* Post-process expand: resolve lookup/master_detail fields by batch-loading related records.
*
* This is a driver-agnostic implementation that uses secondary queries ($in batches)
* to load related records, then injects them into the result set.
*
* @param objectName - The source object name
* @param records - The records returned by the driver
* @param expand - The expand map from QueryAST (field name → nested QueryAST)
* @param depth - Current recursion depth (0-based)
* @returns Records with expanded lookup fields (IDs replaced by full objects)
*/
private async expandRelatedRecords(
objectName: string,
records: any[],
expand: Record<string, QueryAST>,
depth: number = 0,
): Promise<any[]> {
if (!records || records.length === 0) return records;
if (depth >= ObjectQL.MAX_EXPAND_DEPTH) return records;
const objectSchema = SchemaRegistry.getObject(objectName);
// If no schema registered, skip expand — return raw data
if (!objectSchema || !objectSchema.fields) return records;
for (const [fieldName, nestedAST] of Object.entries(expand)) {
const fieldDef = objectSchema.fields[fieldName];
// Skip if field not found or not a relationship type
if (!fieldDef || !fieldDef.reference) continue;
if (fieldDef.type !== 'lookup' && fieldDef.type !== 'master_detail') continue;
const referenceObject = fieldDef.reference;
// Collect all foreign key IDs from records (handle both single and multiple values)
const allIds: any[] = [];
for (const record of records) {
const val = record[fieldName];
if (val == null) continue;
if (Array.isArray(val)) {
allIds.push(...val.filter((id: any) => id != null));
} else if (typeof val === 'object') {
// Already expanded — skip
continue;
} else {
allIds.push(val);
}
}
// De-duplicate IDs
const uniqueIds = [...new Set(allIds)];
if (uniqueIds.length === 0) continue;
// Batch-load related records using $in query
try {
const relatedQuery: QueryAST = {
object: referenceObject,
where: { id: { $in: uniqueIds } },
...(nestedAST.fields ? { fields: nestedAST.fields } : {}),
...(nestedAST.orderBy ? { orderBy: nestedAST.orderBy } : {}),
};
const driver = this.getDriver(referenceObject);
const relatedRecords = await driver.find(referenceObject, relatedQuery) ?? [];
// Build a lookup map: id → record
const recordMap = new Map<string, any>();
for (const rec of relatedRecords) {
const id = rec.id;
if (id != null) recordMap.set(String(id), rec);
}
// Recursively expand nested relations if present
if (nestedAST.expand && Object.keys(nestedAST.expand).length > 0) {
const expandedRelated = await this.expandRelatedRecords(
referenceObject,
relatedRecords,
nestedAST.expand,
depth + 1,
);
// Rebuild map with expanded records
recordMap.clear();
for (const rec of expandedRelated) {
const id = rec.id;
if (id != null) recordMap.set(String(id), rec);
}
}
// Inject expanded records back into the original result set
for (const record of records) {
const val = record[fieldName];
if (val == null) continue;
if (Array.isArray(val)) {
record[fieldName] = val.map((id: any) => recordMap.get(String(id)) ?? id);
} else if (typeof val !== 'object') {
record[fieldName] = recordMap.get(String(val)) ?? val;
}
// If val is already an object, leave it as-is
}
} catch (e) {
// Graceful degradation: if expand fails, keep original IDs
this.logger.warn('Failed to expand relationship field; retaining foreign key IDs', {
object: objectName,
field: fieldName,
reference: referenceObject,
error: (e as Error).message,
});
}
}
return records;
}
// ============================================
// Data Access Methods (IDataEngine Interface)
// ============================================
async find(object: string, query?: EngineQueryOptions): Promise<any[]> {
object = this.resolveObjectName(object);
this.logger.debug('Find operation starting', { object, query });
const driver = this.getDriver(object);
const ast: QueryAST = { object, ...query };
// Remove context from the AST — it's not a driver concern
delete (ast as any).context;
// Normalize OData `top` alias → standard `limit`
if ((ast as any).top != null && ast.limit == null) {
ast.limit = (ast as any).top;
}
delete (ast as any).top;
const opCtx: OperationContext = {
object,
operation: 'find',
ast,
options: query,
context: query?.context,
};
await this.executeWithMiddleware(opCtx, async () => {
const hookContext: HookContext = {
object,
event: 'beforeFind',
input: { ast: opCtx.ast, options: opCtx.options },
session: this.buildSession(opCtx.context),
transaction: opCtx.context?.transaction,
ql: this
};
await this.triggerHooks('beforeFind', hookContext);
try {
let result = await driver.find(object, hookContext.input.ast as QueryAST, hookContext.input.options as any);
// Post-process: expand related records if expand is requested
if (ast.expand && Object.keys(ast.expand).length > 0 && Array.isArray(result)) {
result = await this.expandRelatedRecords(object, result, ast.expand, 0);
}
hookContext.event = 'afterFind';
hookContext.result = result;
await this.triggerHooks('afterFind', hookContext);
return hookContext.result;
} catch (e) {
this.logger.error('Find operation failed', e as Error, { object });
throw e;
}
});
return opCtx.result as any[];
}
async findOne(objectName: string, query?: EngineQueryOptions): Promise<any> {
objectName = this.resolveObjectName(objectName);
this.logger.debug('FindOne operation', { objectName });
const driver = this.getDriver(objectName);
const ast: QueryAST = { object: objectName, ...query, limit: 1 };
// Remove context and top alias from the AST
delete (ast as any).context;
delete (ast as any).top;
const opCtx: OperationContext = {
object: objectName,
operation: 'findOne',
ast,
options: query,
context: query?.context,
};
await this.executeWithMiddleware(opCtx, async () => {
let result = await driver.findOne(objectName, opCtx.ast as QueryAST);
// Post-process: expand related records if expand is requested
if (ast.expand && Object.keys(ast.expand).length > 0 && result != null) {
const expanded = await this.expandRelatedRecords(objectName, [result], ast.expand, 0);
result = expanded[0];
}
return result;
});
return opCtx.result;
}
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 opCtx: OperationContext = {
object,
operation: 'insert',
data,
options,
context: options?.context,
};
await this.executeWithMiddleware(opCtx, async () => {
const hookContext: HookContext = {
object,
event: 'beforeInsert',
input: { data: opCtx.data, options: opCtx.options },
session: this.buildSession(opCtx.context),
transaction: opCtx.context?.transaction,
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 as Record<string, unknown>, 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;
}
});
return opCtx.result;
}
async update(object: string, data: any, options?: EngineUpdateOptions): Promise<any> {
object = this.resolveObjectName(object);
this.logger.debug('Update operation starting', { object });
const driver = this.getDriver(object);
// 1. Extract ID from data or where if it's a single update by ID
let id = data.id;
if (!id && options?.where && typeof options.where === 'object' && 'id' in options.where) {
id = (options.where as Record<string, unknown>).id;
}
const opCtx: OperationContext = {
object,
operation: 'update',
data,
options,
context: options?.context,
};
await this.executeWithMiddleware(opCtx, async () => {
const hookContext: HookContext = {
object,
event: 'beforeUpdate',
input: { id, data: opCtx.data, options: opCtx.options },
session: this.buildSession(opCtx.context),
transaction: opCtx.context?.transaction,
ql: this
};
await this.triggerHooks('beforeUpdate', hookContext);
try {
let result;
if (hookContext.input.id) {
result = await driver.update(object, hookContext.input.id as string, hookContext.input.data as Record<string, unknown>, hookContext.input.options as any);
} else if (options?.multi && driver.updateMany) {
const ast: QueryAST = { object, where: options.where };
result = await driver.updateMany(object, ast, hookContext.input.data as Record<string, unknown>, 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;
}
});
return opCtx.result;
}
async delete(object: string, options?: EngineDeleteOptions): 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?.where && typeof options.where === 'object' && 'id' in options.where) {
id = (options.where as Record<string, unknown>).id;
}
const opCtx: OperationContext = {
object,
operation: 'delete',
options,
context: options?.context,
};
await this.executeWithMiddleware(opCtx, async () => {
const hookContext: HookContext = {
object,
event: 'beforeDelete',
input: { id, options: opCtx.options },
session: this.buildSession(opCtx.context),
transaction: opCtx.context?.transaction,
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: QueryAST = { object, where: options.where };
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;
}
});
return opCtx.result;