-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.ts
More file actions
769 lines (704 loc) · 29.7 KB
/
plugin.ts
File metadata and controls
769 lines (704 loc) · 29.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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { ObjectQL } from './engine.js';
import { ObjectStackProtocolImplementation } from './protocol.js';
import { Plugin, PluginContext } from '@objectstack/core';
import { StorageNameMapping } from '@objectstack/spec/system';
export type { Plugin, PluginContext };
/**
* Protocol extension for DB-based metadata hydration.
* `loadMetaFromDb` is implemented by ObjectStackProtocolImplementation but
* is NOT (yet) part of the canonical ObjectStackProtocol wire-contract in
* `@objectstack/spec`, since it is a server-side bootstrap concern only.
*/
interface ProtocolWithDbRestore {
loadMetaFromDb(): Promise<{ loaded: number; errors: number }>;
}
/** Type guard — checks whether the service exposes `loadMetaFromDb`. */
function hasLoadMetaFromDb(service: unknown): service is ProtocolWithDbRestore {
return (
typeof service === 'object' &&
service !== null &&
typeof (service as Record<string, unknown>)['loadMetaFromDb'] === 'function'
);
}
/**
* Options for ObjectQLPlugin.
*
* `projectId` scopes all metadata writes + reads to a specific project.
* When set, `protocol.saveMetaItem` stamps `project_id = <projectId>` on
* new sys_metadata rows, and `protocol.loadMetaFromDb` filters by the same
* column. Leave undefined in single-kernel / self-hosted mode — rows land
* in the platform-global scope (project_id IS NULL).
*/
export interface ObjectQLPluginOptions {
/** Optional pre-built engine. When absent, one is lazily created in init. */
ql?: ObjectQL;
/** Passed to `new ObjectQL(...)` when `ql` is not supplied. */
hostContext?: Record<string, any>;
/** Scope sys_metadata reads/writes to this project. */
projectId?: string;
/**
* Override the kernel's default plugin-start timeout for this plugin.
* Defaults to 120000 (120s). Schema sync to a remote SQL backend
* (Neon/Postgres/Turso) is latency-bound — the SQL driver currently
* does NOT support `batchSchemaSync`, so it issues one round-trip per
* registered object × twice (Phase 1 + Phase 3 in `start()`). On a
* cold remote DB with N tables this can blow past the kernel's
* default 30s easily, even though everything is healthy.
*/
startupTimeout?: number;
/**
* Skip both `syncRegisteredSchemas()` calls inside `start()` and
* assume DDL is managed out-of-band (e.g. an `apps/cloud/scripts/migrate.ts`
* run before deploy that connects directly to the database and creates
* all `sys_*` + custom tables once).
*
* Use this on cold-start-sensitive runtimes (Cloudflare Containers,
* Lambda) where the platform's inbound-request budget is shorter than
* a fresh remote-DB schema sync. The plugin still hydrates the
* SchemaRegistry from `sys_metadata` (Phase 2), so custom user
* objects come up — they just aren't re-DDL'd on every cold boot.
*
* Falls back to `process.env.OS_SKIP_SCHEMA_SYNC === '1'` when the
* option is unset, so containers can flip it via their env without a
* code change.
*/
skipSchemaSync?: boolean;
}
export class ObjectQLPlugin implements Plugin {
name = 'com.objectstack.engine.objectql';
type = 'objectql';
version = '1.0.0';
/**
* Schema sync to remote SQL DBs is latency-bound (one round-trip per
* table × 2 phases). Default to 120s instead of the kernel's 30s so
* cold Neon/Turso starts don't get killed mid-sync.
*/
startupTimeout = 120_000;
private ql: ObjectQL | undefined;
private hostContext?: Record<string, any>;
private projectId?: string;
private skipSchemaSync = false;
constructor(qlOrOptions?: ObjectQL | ObjectQLPluginOptions, hostContext?: Record<string, any>) {
// Back-compat: legacy callers passed `(ObjectQL, hostContext)` positionally.
if (qlOrOptions instanceof ObjectQL) {
this.ql = qlOrOptions;
this.hostContext = hostContext;
return;
}
// New signature: options bag.
const opts = (qlOrOptions as ObjectQLPluginOptions | undefined) ?? {};
if (opts.ql) {
this.ql = opts.ql;
}
this.hostContext = opts.hostContext ?? hostContext;
this.projectId = opts.projectId;
if (typeof opts.startupTimeout === 'number' && opts.startupTimeout > 0) {
this.startupTimeout = opts.startupTimeout;
}
this.skipSchemaSync =
typeof opts.skipSchemaSync === 'boolean'
? opts.skipSchemaSync
: process.env.OS_SKIP_SCHEMA_SYNC === '1';
}
init = async (ctx: PluginContext) => {
if (!this.ql) {
// Pass kernel logger to engine to avoid creating a separate logger instance
const hostCtx = { ...this.hostContext, logger: ctx.logger };
this.ql = new ObjectQL(hostCtx);
}
// Register as provider for Core Kernel Services
ctx.registerService('objectql', this.ql);
ctx.registerService('data', this.ql); // ObjectQL implements IDataEngine
// Register manifest service for direct app/package registration.
// Plugins call ctx.getService('manifest').register(manifestData)
// instead of the legacy ctx.registerService('app.<id>', manifestData) convention.
const ql = this.ql;
ctx.registerService('manifest', {
register: (manifest: any) => {
ql.registerApp(manifest);
ctx.logger.debug('Manifest registered via manifest service', {
id: manifest.id || manifest.name
});
}
});
ctx.logger.info('ObjectQL engine registered', {
services: ['objectql', 'data', 'manifest'],
});
// Register Protocol Implementation
const protocolShim = new ObjectStackProtocolImplementation(
this.ql,
() => ctx.getServices ? ctx.getServices() : new Map(),
undefined,
this.projectId,
);
ctx.registerService('protocol', protocolShim);
ctx.logger.info('Protocol service registered');
// Register an `analytics` service adapter that maps the dispatcher's
// expected interface (query / getMeta / generateSql) onto the
// protocol shim's `analyticsQuery`. Without this, HttpDispatcher's
// `handleAnalytics` cannot resolve a service and `/api/v1/analytics/*`
// returns ROUTE_NOT_FOUND, even though discovery advertises the route
// (objectql's getDiscovery hardcodes `analytics: enabled:true`). The
// adapter delegates `query` to the cube → engine.aggregate translator
// already implemented in protocol.ts; getMeta/generateSql return a
// structured "not implemented" payload so callers see something
// useful instead of a 500.
ctx.registerService('analytics', {
query: (body: any) => protocolShim.analyticsQuery(body),
getMeta: async () => ({
cubes: [],
message: 'Analytics meta endpoint not implemented by ObjectQL adapter',
}),
generateSql: async (_body: any) => ({
sql: null,
message: 'Analytics SQL generation not implemented by ObjectQL adapter',
}),
});
}
start = async (ctx: PluginContext) => {
ctx.logger.info('ObjectQL engine starting...');
// Sync from external metadata service (e.g. MetadataPlugin) if available
try {
const metadataService = ctx.getService('metadata') as any;
if (metadataService && typeof metadataService.loadMany === 'function' && this.ql) {
await this.loadMetadataFromService(metadataService, ctx);
}
} catch (e: any) {
ctx.logger.debug('No external metadata service to sync from');
}
// Discover features from Kernel Services
if (ctx.getServices && this.ql) {
const services = ctx.getServices();
for (const [name, service] of services.entries()) {
if (name.startsWith('driver.')) {
// Register Driver
this.ql.registerDriver(service);
ctx.logger.debug('Discovered and registered driver service', { serviceName: name });
}
if (name.startsWith('app.')) {
// Legacy fallback: discover app.* services (DEPRECATED)
ctx.logger.warn(
`[DEPRECATED] Service "${name}" uses legacy app.* convention. ` +
`Migrate to ctx.getService('manifest').register(data).`
);
this.ql.registerApp(service); // service is Manifest
ctx.logger.debug('Discovered and registered app service (legacy)', { serviceName: name });
}
}
// Bridge realtime service from kernel service registry to ObjectQL.
// RealtimeServicePlugin registers as 'realtime' service during init().
// This enables ObjectQL to publish data change events.
try {
const realtimeService = ctx.getService('realtime');
if (realtimeService && typeof realtimeService === 'object' && 'publish' in realtimeService) {
ctx.logger.info('[ObjectQLPlugin] Bridging realtime service to ObjectQL for event publishing');
this.ql.setRealtimeService(realtimeService as any);
}
} catch (e: any) {
ctx.logger.debug('[ObjectQLPlugin] No realtime service found — data events will not be published', {
error: e.message,
});
}
}
// Initialize drivers (calls driver.connect() which sets up persistence)
await this.ql?.init();
// Phase 1: Sync built-in schemas so sys_metadata table exists before reading it.
//
// Cold-start-sensitive runtimes (Cloudflare Containers, Lambda) can
// opt out via `skipSchemaSync` / `OS_SKIP_SCHEMA_SYNC=1`. In that
// mode an out-of-band migration must have already created every
// table; we only assume the DDL is in place and skip straight to
// hydration. This avoids one round-trip per table × N objects on
// every cold boot.
if (this.skipSchemaSync) {
ctx.logger.info('Skipping schema sync (OS_SKIP_SCHEMA_SYNC=1) — assuming DDL is managed out-of-band');
} else {
await this.syncRegisteredSchemas(ctx);
}
// Phase 2: Hydrate SchemaRegistry from sys_metadata (loads custom/template objects).
// Project kernels (projectId set) never persist sys_metadata locally —
// metadata is sourced from the artifact (MetadataPlugin) or routed to the
// control plane via ControlPlaneProxyDriver. Skip to avoid querying a table
// that does not exist on local project DBs.
if (this.projectId === undefined) {
await this.restoreMetadataFromDb(ctx);
} else {
ctx.logger.info('Project kernel — skipping sys_metadata hydration (metadata sourced from artifact)');
}
// Phase 3: Sync any new schemas that were just hydrated from the DB
// (e.g. CRM objects seeded via template — they must have tables before use).
if (!this.skipSchemaSync) {
await this.syncRegisteredSchemas(ctx);
}
// Bridge all SchemaRegistry objects to metadata service.
//
// `SchemaRegistry` is a process-wide singleton, so project kernels in a
// multi-project server would otherwise inherit every object ever
// registered by any sibling project. When this plugin was constructed
// with a `projectId`, the kernel is project-scoped — its
// metadata comes from the artifact (MetadataPlugin) or the
// control-plane proxy, not from local sys_metadata. The bridge would
// only pollute its metadata service with cross-project leakage, so
// skip it in that case.
if (this.projectId === undefined) {
await this.bridgeObjectsToMetadataService(ctx);
}
// Register built-in audit hooks
this.registerAuditHooks(ctx);
// Tenant isolation is now handled by `@objectstack/plugin-security`
// via the `member_default` permission set's RLS rule
// (`organization_id = current_user.organization_id`, with
// field-existence guards). The legacy hard-coded `tenant_id` filter
// middleware was removed because it (a) collided with the
// SecurityPlugin RLS pipeline and (b) blindly filtered tables that
// don't have a `tenant_id` column (e.g. `sys_organization`),
// returning 0 rows instead of all rows.
ctx.logger.info('ObjectQL engine started', {
driversRegistered: this.ql?.['drivers']?.size || 0,
objectsRegistered: this.ql?.registry?.getAllObjects?.()?.length || 0
});
}
/**
* Register built-in audit hooks for auto-stamping created_by/updated_by
* and fetching previousData for update/delete operations. These are
* declared as canonical `Hook` metadata and bound through the same
* `bindHooksToEngine` path used by `defineStack({ hooks })`, so the
* engine's built-ins flow through the same rails as user code
* (dogfooding the protocol).
*/
private registerAuditHooks(ctx: PluginContext) {
if (!this.ql) return;
const stamp = () => new Date().toISOString();
/**
* Returns true when the resolved object schema declares a field with the
* given name. Audit fields (`created_by`, `updated_by`, `tenant_id`) are
* NOT auto-injected by the SQL driver, so we must only stamp values for
* fields the user has explicitly declared on the object — otherwise the
* driver will issue an INSERT against a column that does not exist in
* the physical table (e.g. `table lead has no column named created_by`).
*
* `created_at`/`updated_at` are unconditional because driver-sql creates
* them as built-in columns on every table.
*/
const hasField = (objectName: string, field: string): boolean => {
try {
const schema: any = this.ql?.getSchema?.(objectName);
if (!schema || typeof schema !== 'object') return false;
const fields = schema.fields;
if (!fields || typeof fields !== 'object') return false;
return Object.prototype.hasOwnProperty.call(fields, field);
} catch {
return false;
}
};
const applyToRecord = (
record: Record<string, any>,
objectName: string,
session: any,
isInsert: boolean,
) => {
const now = stamp();
if (isInsert) {
record.created_at = record.created_at ?? now;
}
record.updated_at = now;
if (session?.userId) {
if (isInsert && hasField(objectName, 'created_by')) {
record.created_by = record.created_by ?? session.userId;
}
if (hasField(objectName, 'updated_by')) {
record.updated_by = session.userId;
}
}
if (isInsert && session?.tenantId && hasField(objectName, 'tenant_id')) {
record.tenant_id = record.tenant_id ?? session.tenantId;
}
};
const stampData = (
data: unknown,
objectName: string,
session: any,
isInsert: boolean,
) => {
if (Array.isArray(data)) {
for (const row of data) {
if (row && typeof row === 'object') {
applyToRecord(row as Record<string, any>, objectName, session, isInsert);
}
}
} else if (data && typeof data === 'object') {
applyToRecord(data as Record<string, any>, objectName, session, isInsert);
}
};
const builtinHooks: any[] = [
{
name: 'sys_stamp_audit_insert',
object: '*',
events: ['beforeInsert'],
priority: 10,
description: 'Auto-stamp created_by / updated_by / created_at / updated_at / tenant_id on insert (only when the field exists on the object schema)',
handler: async (hookCtx: any) => {
if (hookCtx.input?.data) {
stampData(hookCtx.input.data, hookCtx.object, hookCtx.session, true);
}
},
},
{
name: 'sys_stamp_audit_update',
object: '*',
events: ['beforeUpdate'],
priority: 10,
description: 'Auto-stamp updated_by / updated_at on update (only when the field exists on the object schema)',
handler: async (hookCtx: any) => {
if (hookCtx.input?.data) {
stampData(hookCtx.input.data, hookCtx.object, hookCtx.session, false);
}
},
},
{
name: 'sys_fetch_previous_update',
object: '*',
events: ['beforeUpdate'],
priority: 5,
description: 'Auto-fetch the previous record for update hooks',
handler: async (hookCtx: any) => {
if (hookCtx.input?.id && !hookCtx.previous) {
try {
const existing = await this.ql!.findOne(hookCtx.object, {
where: { id: hookCtx.input.id },
context: {
roles: [],
permissions: [],
isSystem: true,
...(hookCtx.transaction ? { transaction: hookCtx.transaction } : {}),
} as any,
});
if (existing) hookCtx.previous = existing;
} catch (_e) {
// Non-fatal: some objects may not support findOne
}
}
},
},
{
name: 'sys_fetch_previous_delete',
object: '*',
events: ['beforeDelete'],
priority: 5,
description: 'Auto-fetch the previous record for delete hooks',
handler: async (hookCtx: any) => {
if (hookCtx.input?.id && !hookCtx.previous) {
try {
const existing = await this.ql!.findOne(hookCtx.object, {
where: { id: hookCtx.input.id },
context: {
roles: [],
permissions: [],
isSystem: true,
...(hookCtx.transaction ? { transaction: hookCtx.transaction } : {}),
} as any,
});
if (existing) hookCtx.previous = existing;
} catch (_e) {
// Non-fatal
}
}
},
},
];
if (typeof (this.ql as any).bindHooks === 'function') {
(this.ql as any).bindHooks(builtinHooks, { packageId: 'sys:audit' });
} else {
// Defensive fallback if binder isn't available (older builds).
for (const h of builtinHooks) {
for (const event of h.events) {
this.ql.registerHook(event, h.handler, {
object: h.object,
priority: h.priority,
packageId: 'sys:audit',
});
}
}
}
ctx.logger.debug('Audit hooks registered via binder (created_by/updated_by, previousData)');
}
/**
* Tenant isolation moved to `@objectstack/plugin-security`'s
* `member_default` permission set RLS
* (`organization_id = current_user.organization_id`, with
* field-existence guards). The legacy `registerTenantMiddleware`
* method was removed because it (a) collided with SecurityPlugin's
* RLS pipeline and (b) blindly filtered tables that don't have a
* `tenant_id` column (e.g. `sys_organization`), returning 0 rows
* instead of all rows.
*/
/**
* Synchronize all registered object schemas to the database.
*
* Groups objects by their responsible driver, then:
* - If the driver advertises `supports.batchSchemaSync` and implements
* `syncSchemasBatch()`, submits all schemas in a single call (reducing
* network round-trips for remote drivers like Turso).
* - Otherwise falls back to sequential `syncSchema()` per object.
*
* This is idempotent — drivers must tolerate repeated calls without
* duplicating tables or erroring out.
*
* Drivers that do not implement `syncSchema` are silently skipped.
*/
private async syncRegisteredSchemas(ctx: PluginContext) {
if (!this.ql) return;
const allObjects = this.ql.registry?.getAllObjects?.() ?? [];
if (allObjects.length === 0) return;
let synced = 0;
let skipped = 0;
// Group objects by driver for potential batch optimization
const driverGroups = new Map<any, Array<{ obj: any; tableName: string }>>();
for (const obj of allObjects) {
const driver = this.ql.getDriverForObject(obj.name);
if (!driver) {
ctx.logger.debug('No driver available for object, skipping schema sync', {
object: obj.name,
});
skipped++;
continue;
}
if (typeof driver.syncSchema !== 'function') {
ctx.logger.debug('Driver does not support syncSchema, skipping', {
object: obj.name,
driver: driver.name,
});
skipped++;
continue;
}
const tableName = StorageNameMapping.resolveTableName(obj);
let group = driverGroups.get(driver);
if (!group) {
group = [];
driverGroups.set(driver, group);
}
group.push({ obj, tableName });
}
// Process each driver group
for (const [driver, entries] of driverGroups) {
// Batch path: driver supports batch schema sync
if (
driver.supports?.batchSchemaSync &&
typeof driver.syncSchemasBatch === 'function'
) {
const batchPayload = entries.map((e) => ({
object: e.tableName,
schema: e.obj,
}));
try {
await driver.syncSchemasBatch(batchPayload);
synced += entries.length;
ctx.logger.debug('Batch schema sync succeeded', {
driver: driver.name,
count: entries.length,
});
} catch (e: unknown) {
ctx.logger.warn('Batch schema sync failed, falling back to sequential', {
driver: driver.name,
error: e instanceof Error ? e.message : String(e),
});
// Fallback: sequential sync for this driver's objects
for (const { obj, tableName } of entries) {
try {
await driver.syncSchema(tableName, obj);
synced++;
} catch (seqErr: unknown) {
ctx.logger.warn('Failed to sync schema for object', {
object: obj.name,
tableName,
driver: driver.name,
error: seqErr instanceof Error ? seqErr.message : String(seqErr),
});
}
}
}
} else {
// Sequential path: no batch support
for (const { obj, tableName } of entries) {
try {
await driver.syncSchema(tableName, obj);
synced++;
} catch (e: unknown) {
ctx.logger.warn('Failed to sync schema for object', {
object: obj.name,
tableName,
driver: driver.name,
error: e instanceof Error ? e.message : String(e),
});
}
}
}
}
if (synced > 0 || skipped > 0) {
ctx.logger.info('Schema sync complete', { synced, skipped, total: allObjects.length });
}
}
/**
* Restore persisted metadata from the database (sys_metadata) on startup.
*
* Calls `protocol.loadMetaFromDb()` to bulk-load all active metadata
* records (objects, views, apps, etc.) into the in-memory SchemaRegistry.
* This closes the persistence loop so that user-created schemas survive
* kernel cold starts and redeployments.
*
* Gracefully degrades when:
* - The protocol service is unavailable (e.g., in-memory-only mode).
* - `loadMetaFromDb` is not implemented by the protocol shim.
* - The underlying driver/table does not exist yet (first-run scenario).
*/
private async restoreMetadataFromDb(ctx: PluginContext): Promise<void> {
// Phase 1: Resolve protocol service (separate from DB I/O for clearer diagnostics)
let protocol: ProtocolWithDbRestore;
try {
const service = ctx.getService('protocol');
if (!service || !hasLoadMetaFromDb(service)) {
ctx.logger.debug('Protocol service does not support loadMetaFromDb, skipping DB restore');
return;
}
protocol = service;
} catch (e: unknown) {
ctx.logger.debug('Protocol service unavailable, skipping DB restore', {
error: e instanceof Error ? e.message : String(e),
});
return;
}
// Phase 2: DB hydration (loads into SchemaRegistry)
try {
const { loaded, errors } = await protocol.loadMetaFromDb();
if (loaded > 0 || errors > 0) {
ctx.logger.info('Metadata restored from database to SchemaRegistry', { loaded, errors });
} else {
ctx.logger.debug('No persisted metadata found in database');
}
} catch (e: unknown) {
// Non-fatal: first-run or in-memory driver may not have sys_metadata yet
ctx.logger.debug('DB metadata restore failed (non-fatal)', {
error: e instanceof Error ? e.message : String(e),
});
}
}
/**
* Bridge all SchemaRegistry objects to the metadata service.
*
* This ensures objects registered by plugins and loaded from sys_metadata
* are visible to AI tools and other consumers that query IMetadataService.
*
* Runs after both restoreMetadataFromDb() and syncRegisteredSchemas() to
* catch all objects in the SchemaRegistry regardless of their source.
*/
private async bridgeObjectsToMetadataService(ctx: PluginContext): Promise<void> {
try {
const metadataService = ctx.getService<any>('metadata');
if (!metadataService || typeof metadataService.register !== 'function') {
ctx.logger.debug('Metadata service unavailable for bridging, skipping');
return;
}
if (!this.ql?.registry) {
ctx.logger.debug('SchemaRegistry unavailable for bridging, skipping');
return;
}
const objects = this.ql.registry.getAllObjects();
let bridged = 0;
for (const obj of objects) {
try {
// Check if object is already in metadata service to avoid duplicates
const existing = await metadataService.getObject(obj.name);
if (!existing) {
// Register object that exists in SchemaRegistry but not in metadata service
await metadataService.register('object', obj.name, obj);
bridged++;
}
} catch (e: unknown) {
ctx.logger.debug('Failed to bridge object to metadata service', {
object: obj.name,
error: e instanceof Error ? e.message : String(e),
});
}
}
if (bridged > 0) {
ctx.logger.info('Bridged objects from SchemaRegistry to metadata service', {
count: bridged,
total: objects.length
});
} else {
ctx.logger.debug('No objects needed bridging (all already in metadata service)');
}
} catch (e: unknown) {
ctx.logger.debug('Failed to bridge objects to metadata service', {
error: e instanceof Error ? e.message : String(e),
});
}
}
/**
* Load metadata from external metadata service into ObjectQL registry
* This enables ObjectQL to use file-based or remote metadata
*/
private async loadMetadataFromService(metadataService: any, ctx: PluginContext) {
ctx.logger.info('Syncing metadata from external service into ObjectQL registry...');
// Metadata types to sync
const metadataTypes = ['object', 'view', 'app', 'flow', 'workflow', 'function', 'hook'];
let totalLoaded = 0;
for (const type of metadataTypes) {
try {
// Check if service has loadMany method
if (typeof metadataService.loadMany === 'function') {
const items = await metadataService.loadMany(type);
if (items && items.length > 0) {
// Functions arrive as JSON-safe records ({name, handler})
// where `handler` is a function reference or compiled code
// already attached by the metadata pipeline. Register them
// BEFORE binding hooks so string-named hook handlers can
// resolve.
if (type === 'function' && this.ql && typeof (this.ql as any).registerFunction === 'function') {
for (const item of items) {
if (item?.name && typeof item.handler === 'function') {
(this.ql as any).registerFunction(item.name, item.handler, 'metadata-service');
}
}
}
items.forEach((item: any) => {
// Determine key field (usually 'name' or 'id')
const keyField = item.id ? 'id' : 'name';
// For objects, use the ownership-aware registration
if (type === 'object' && this.ql) {
// Objects are registered differently (ownership model)
// Skip for now - handled by app registration
return;
}
// Register other types in the registry
if (this.ql?.registry?.registerItem) {
this.ql.registry.registerItem(type, item, keyField);
}
});
// Hooks need to be wired into the execution pipeline,
// not just stored in the registry. Funnel through the
// canonical binder so declarative semantics (condition,
// retry, timeout, async, onError, priority, packageId)
// are honoured uniformly with the AppPlugin path.
if (type === 'hook' && this.ql && typeof (this.ql as any).bindHooks === 'function') {
(this.ql as any).bindHooks(items, {
packageId: 'metadata-service',
});
}
totalLoaded += items.length;
ctx.logger.info(`Synced ${items.length} ${type}(s) from metadata service`);
}
}
} catch (e: any) {
// Type might not exist in metadata service - that's ok
ctx.logger.debug(`No ${type} metadata found or error loading`, {
error: e.message
});
}
}
if (totalLoaded > 0) {
ctx.logger.info(`Metadata sync complete: ${totalLoaded} items loaded into ObjectQL registry`);
}
}
}