|
1 | | -import { DriverInterface, DriverOptions, QueryAST } from '@objectstack/spec'; |
| 1 | +import { DriverInterface, DriverOptions, QueryAST, ObjectStackManifest, ManifestSchema } from '@objectstack/spec'; |
| 2 | +import { SchemaRegistry } from './registry'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Host Context provided to plugins |
| 6 | + */ |
| 7 | +export interface PluginContext { |
| 8 | + ql: ObjectQL; |
| 9 | + logger: Console; |
| 10 | + // Extensible map for host-specific globals (like HTTP Router, etc.) |
| 11 | + [key: string]: any; |
| 12 | +} |
2 | 13 |
|
3 | 14 | /** |
4 | 15 | * ObjectQL Engine |
5 | | - * |
6 | | - * The core orchestration layer that sits between the API/UI and the Data Driver. |
7 | | - * It handles: |
8 | | - * 1. Request Validation (using Schemas) |
9 | | - * 2. Security Enforcement (ACLs, Sharing Rules) |
10 | | - * 3. Workflow Triggers |
11 | | - * 4. Driver Delegation |
12 | 16 | */ |
13 | 17 | export class ObjectQL { |
14 | 18 | private drivers = new Map<string, DriverInterface>(); |
15 | 19 | private defaultDriver: string | null = null; |
| 20 | + private plugins = new Map<string, any>(); |
| 21 | + |
| 22 | + // Host provided context additions (e.g. Server router) |
| 23 | + private hostContext: Record<string, any> = {}; |
16 | 24 |
|
17 | | - constructor() { |
| 25 | + constructor(hostContext: Record<string, any> = {}) { |
| 26 | + this.hostContext = hostContext; |
18 | 27 | console.log(`[ObjectQL] Engine Instance Created`); |
19 | 28 | } |
20 | 29 |
|
| 30 | + /** |
| 31 | + * Load and Register a Plugin |
| 32 | + */ |
| 33 | + async use(manifestPart: any, runtimePart?: any) { |
| 34 | + // 1. Validate / Register Manifest |
| 35 | + if (manifestPart) { |
| 36 | + // In a real scenario, we might strictly parse this using Zod |
| 37 | + // For now, simple ID check |
| 38 | + const id = manifestPart.id; |
| 39 | + if (!id) { |
| 40 | + console.warn('[ObjectQL] Plugin manifest missing ID', manifestPart); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + console.log(`[ObjectQL] Loading Plugin: ${id}`); |
| 45 | + SchemaRegistry.registerPlugin(manifestPart as ObjectStackManifest); |
| 46 | + |
| 47 | + // Register contributions |
| 48 | + if (manifestPart.contributes?.kinds) { |
| 49 | + for (const kind of manifestPart.contributes.kinds) { |
| 50 | + SchemaRegistry.registerKind(kind); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // 2. Execute Runtime |
| 56 | + if (runtimePart) { |
| 57 | + const pluginDef = (runtimePart as any).default || runtimePart; |
| 58 | + if (pluginDef.onEnable) { |
| 59 | + const context: PluginContext = { |
| 60 | + ql: this, |
| 61 | + logger: console, |
| 62 | + // Expose the driver registry helper explicitly if needed |
| 63 | + drivers: this, // Since `registerDriver` is on `this`, we can alias it or expose `this` |
| 64 | + ...this.hostContext |
| 65 | + }; |
| 66 | + |
| 67 | + await pluginDef.onEnable(context); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
21 | 72 | /** |
22 | 73 | * Register a new storage driver |
23 | 74 | */ |
|
0 commit comments