|
| 1 | +/** |
| 2 | + * ObjectQL Plugin System |
| 3 | + * Copyright (c) 2026-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Runtime context passed to plugin lifecycle hooks |
| 11 | + * |
| 12 | + * This context provides access to the kernel/engine instance |
| 13 | + * and allows plugins to interact with the ObjectStack runtime. |
| 14 | + */ |
| 15 | +export interface RuntimeContext { |
| 16 | + /** |
| 17 | + * The ObjectStack kernel/engine instance |
| 18 | + * |
| 19 | + * This provides access to: |
| 20 | + * - metadata registry |
| 21 | + * - hook manager |
| 22 | + * - action manager |
| 23 | + * - CRUD operations |
| 24 | + */ |
| 25 | + engine: any; // Using 'any' to avoid circular dependency |
| 26 | + |
| 27 | + /** |
| 28 | + * Get the kernel instance (alternative accessor) |
| 29 | + * Some implementations may use getKernel() instead of engine |
| 30 | + */ |
| 31 | + getKernel?: () => any; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * RuntimePlugin Interface |
| 36 | + * |
| 37 | + * Defines the standard plugin contract for ObjectStack/ObjectQL ecosystem. |
| 38 | + * All plugins (protocol adapters, data drivers, feature extensions) should |
| 39 | + * implement this interface to ensure consistent lifecycle management. |
| 40 | + * |
| 41 | + * Lifecycle Order: |
| 42 | + * 1. install() - Called during kernel initialization |
| 43 | + * 2. onStart() - Called when kernel starts |
| 44 | + * 3. onStop() - Called when kernel stops/shuts down |
| 45 | + * |
| 46 | + * @example |
| 47 | + * ```typescript |
| 48 | + * export class MyPlugin implements RuntimePlugin { |
| 49 | + * name = '@myorg/my-plugin'; |
| 50 | + * version = '1.0.0'; |
| 51 | + * |
| 52 | + * async install(ctx: RuntimeContext): Promise<void> { |
| 53 | + * // Register hooks, load configuration |
| 54 | + * console.log('Plugin installed'); |
| 55 | + * } |
| 56 | + * |
| 57 | + * async onStart(ctx: RuntimeContext): Promise<void> { |
| 58 | + * // Start servers, connect to services |
| 59 | + * console.log('Plugin started'); |
| 60 | + * } |
| 61 | + * |
| 62 | + * async onStop(ctx: RuntimeContext): Promise<void> { |
| 63 | + * // Cleanup resources, disconnect |
| 64 | + * console.log('Plugin stopped'); |
| 65 | + * } |
| 66 | + * } |
| 67 | + * ``` |
| 68 | + */ |
| 69 | +export interface RuntimePlugin { |
| 70 | + /** |
| 71 | + * Unique plugin identifier |
| 72 | + * |
| 73 | + * Should follow npm package naming convention |
| 74 | + * Examples: '@objectql/plugin-security', '@myorg/my-plugin' |
| 75 | + */ |
| 76 | + name: string; |
| 77 | + |
| 78 | + /** |
| 79 | + * Plugin version (semantic versioning) |
| 80 | + * |
| 81 | + * Optional but recommended for debugging and compatibility tracking |
| 82 | + * Example: '1.0.0', '2.1.3-beta' |
| 83 | + */ |
| 84 | + version?: string; |
| 85 | + |
| 86 | + /** |
| 87 | + * Install hook - called during kernel initialization |
| 88 | + * |
| 89 | + * Use this phase to: |
| 90 | + * - Register hooks and event handlers |
| 91 | + * - Initialize plugin state |
| 92 | + * - Load configuration |
| 93 | + * - Register metadata (objects, fields, actions) |
| 94 | + * - Validate dependencies |
| 95 | + * |
| 96 | + * This is called BEFORE the kernel starts, so services may not be available yet. |
| 97 | + * |
| 98 | + * @param ctx - Runtime context with access to kernel/engine |
| 99 | + */ |
| 100 | + install?(ctx: RuntimeContext): void | Promise<void>; |
| 101 | + |
| 102 | + /** |
| 103 | + * Start hook - called when kernel starts |
| 104 | + * |
| 105 | + * Use this phase to: |
| 106 | + * - Start background processes (servers, workers, schedulers) |
| 107 | + * - Connect to external services (databases, APIs, message queues) |
| 108 | + * - Initialize runtime resources |
| 109 | + * - Perform health checks |
| 110 | + * |
| 111 | + * This is called AFTER install() and AFTER all plugins are installed. |
| 112 | + * |
| 113 | + * @param ctx - Runtime context with access to kernel/engine |
| 114 | + */ |
| 115 | + onStart?(ctx: RuntimeContext): void | Promise<void>; |
| 116 | + |
| 117 | + /** |
| 118 | + * Stop hook - called when kernel stops/shuts down |
| 119 | + * |
| 120 | + * Use this phase to: |
| 121 | + * - Stop background processes |
| 122 | + * - Disconnect from external services |
| 123 | + * - Cleanup resources (file handles, connections, timers) |
| 124 | + * - Flush pending operations |
| 125 | + * - Save state if needed |
| 126 | + * |
| 127 | + * This is called during graceful shutdown. Ensure cleanup completes quickly. |
| 128 | + * |
| 129 | + * @param ctx - Runtime context with access to kernel/engine |
| 130 | + */ |
| 131 | + onStop?(ctx: RuntimeContext): void | Promise<void>; |
| 132 | +} |
0 commit comments