@@ -16,6 +16,7 @@ import { QueryAnalyzer } from './query/query-analyzer';
1616import { ObjectStackProtocolImplementation } from './protocol' ;
1717import type { Driver } from '@objectql/types' ;
1818import { createDefaultAiRegistry } from './ai' ;
19+ import { SchemaRegistry } from '@objectstack/objectql' ;
1920
2021/**
2122 * Extended kernel with ObjectQL services
@@ -304,33 +305,74 @@ export class ObjectQLPlugin implements RuntimePlugin {
304305 }
305306
306307 // --- Adapter for @objectstack/core compatibility ---
307- init = async ( kernel : any ) : Promise < void > => {
308- // The new core passes the kernel instance directly
309- // We wrap it to match the old RuntimeContext interface
308+ init = async ( pluginCtx : any ) : Promise < void > => {
309+ // The @objectstack /core kernel passes a PluginContext (with registerService, getKernel, etc.)
310+ // We extract the actual kernel and build a RuntimeContext wrapper that proxies registerService.
311+ const actualKernel = typeof pluginCtx . getKernel === 'function'
312+ ? pluginCtx . getKernel ( )
313+ : pluginCtx ;
314+
315+ // Create metadata facade on the kernel if not already present.
316+ // The raw ObjectKernel from @objectstack /core has no .metadata property;
317+ // it's normally created by the ObjectQL wrapper (app.ts).
318+ // When running via `objectstack serve`, we must create it here.
319+ if ( ! actualKernel . metadata ) {
320+ const unwrapContent = ( item : any ) => ( item && item . content ) ? item . content : item ;
321+ actualKernel . metadata = {
322+ register : ( type : string , item : any ) => SchemaRegistry . registerItem ( type , item , item . id ? 'id' : 'name' ) ,
323+ get : ( type : string , name : string ) => {
324+ const item = SchemaRegistry . getItem ( type , name ) as any ;
325+ return unwrapContent ( item ) ;
326+ } ,
327+ getEntry : ( type : string , name : string ) => SchemaRegistry . getItem ( type , name ) ,
328+ list : ( type : string ) => {
329+ const items = SchemaRegistry . listItems ( type ) ;
330+ return items . map ( unwrapContent ) ;
331+ } ,
332+ unregister : ( type : string , name : string ) => {
333+ if ( typeof SchemaRegistry . unregisterItem === 'function' ) {
334+ SchemaRegistry . unregisterItem ( type , name ) ;
335+ }
336+ } ,
337+ unregisterPackage : ( packageName : string ) => {
338+ if ( typeof ( SchemaRegistry as any ) . unregisterObjectsByPackage === 'function' ) {
339+ ( SchemaRegistry as any ) . unregisterObjectsByPackage ( packageName ) ;
340+ }
341+ } ,
342+ } ;
343+ this . logger . info ( 'Created metadata facade on kernel' ) ;
344+ }
345+
310346 const ctx : any = {
311- engine : kernel ,
312- getKernel : ( ) => kernel
347+ engine : actualKernel ,
348+ getKernel : ( ) => actualKernel ,
349+ // Proxy registerService from PluginContext so install() can register metadata/data/analytics
350+ registerService : typeof pluginCtx . registerService === 'function'
351+ ? pluginCtx . registerService . bind ( pluginCtx )
352+ : undefined ,
313353 } ;
314354
315355 // Register Protocol Service
316- // If kernel supports service registration (PluginContext or ExtendedKernel with custom registry)
317- if ( kernel && typeof kernel . registerService === 'function' ) {
356+ if ( typeof pluginCtx . registerService === 'function' ) {
318357 this . logger . info ( 'Registering protocol service...' ) ;
319- const protocolShim = new ObjectStackProtocolImplementation ( kernel ) ;
320- kernel . registerService ( 'protocol' , protocolShim ) ;
321-
322- // Register 'objectql' service for AppPlugin compatibility
323- kernel . registerService ( 'objectql' , this ) ;
324- this . logger . debug ( 'Registered objectql service' ) ;
358+ const protocolShim = new ObjectStackProtocolImplementation ( actualKernel ) ;
359+ pluginCtx . registerService ( 'protocol' , protocolShim ) ;
360+
361+ // Register 'objectql' service for AppPlugin compatibility
362+ pluginCtx . registerService ( 'objectql' , this ) ;
363+ this . logger . debug ( 'Registered objectql service' ) ;
325364 }
326365
327366 return this . install ( ctx ) ;
328367 }
329368
330- start = async ( kernel : any ) : Promise < void > => {
369+ start = async ( pluginCtx : any ) : Promise < void > => {
370+ const actualKernel = typeof pluginCtx . getKernel === 'function'
371+ ? pluginCtx . getKernel ( )
372+ : pluginCtx ;
331373 const ctx : any = {
332- engine : kernel ,
333- getKernel : ( ) => kernel
374+ engine : actualKernel ,
375+ getKernel : ( ) => actualKernel ,
334376 } ;
335377 return this . onStart ( ctx ) ;
336378 }
0 commit comments