|
| 1 | +import { Plugin, PluginContext } from './types.js'; |
| 2 | +import { SchemaRegistry, ObjectQL } from '@objectstack/objectql'; |
| 3 | + |
| 4 | +/** |
| 5 | + * AppManifestPlugin |
| 6 | + * |
| 7 | + * Wraps an ObjectStack app manifest (objectstack.config.ts export) |
| 8 | + * as a Plugin so it can be loaded in the MiniKernel architecture. |
| 9 | + * |
| 10 | + * Handles: |
| 11 | + * - Registering the app/plugin in SchemaRegistry |
| 12 | + * - Registering all objects defined in the manifest |
| 13 | + * - Seeding data if provided |
| 14 | + * |
| 15 | + * Dependencies: ['com.objectstack.engine.objectql'] |
| 16 | + */ |
| 17 | +export class AppManifestPlugin implements Plugin { |
| 18 | + name: string; |
| 19 | + version?: string; |
| 20 | + dependencies = ['com.objectstack.engine.objectql']; |
| 21 | + |
| 22 | + private manifest: any; |
| 23 | + |
| 24 | + constructor(manifest: any) { |
| 25 | + this.manifest = manifest; |
| 26 | + this.name = manifest.id || manifest.name || 'unnamed-app'; |
| 27 | + this.version = manifest.version; |
| 28 | + } |
| 29 | + |
| 30 | + async init(ctx: PluginContext) { |
| 31 | + ctx.logger.log(`[AppManifestPlugin] Loading App Manifest: ${this.manifest.id || this.manifest.name}`); |
| 32 | + |
| 33 | + // Register the app/plugin in the schema registry |
| 34 | + SchemaRegistry.registerPlugin(this.manifest); |
| 35 | + |
| 36 | + // Register all objects defined in the manifest |
| 37 | + if (this.manifest.objects) { |
| 38 | + for (const obj of this.manifest.objects) { |
| 39 | + SchemaRegistry.registerObject(obj); |
| 40 | + ctx.logger.log(`[AppManifestPlugin] Registered Object: ${obj.name}`); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + async start(ctx: PluginContext) { |
| 46 | + // Seed data if provided |
| 47 | + if (this.manifest.data && Array.isArray(this.manifest.data)) { |
| 48 | + ctx.logger.log(`[AppManifestPlugin] Seeding data for ${this.manifest.name || this.manifest.id}...`); |
| 49 | + |
| 50 | + const objectql = ctx.getService<ObjectQL>('objectql'); |
| 51 | + |
| 52 | + for (const seed of this.manifest.data) { |
| 53 | + try { |
| 54 | + // Check if data already exists |
| 55 | + const existing = await objectql.find(seed.object, { top: 1 }); |
| 56 | + if (existing.length === 0) { |
| 57 | + ctx.logger.log(`[AppManifestPlugin] Inserting ${seed.records.length} records into ${seed.object}`); |
| 58 | + for (const record of seed.records) { |
| 59 | + await objectql.insert(seed.object, record); |
| 60 | + } |
| 61 | + } |
| 62 | + } catch (e) { |
| 63 | + ctx.logger.warn(`[AppManifestPlugin] Failed to seed ${seed.object}`, e); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments