|
| 1 | +/** |
| 2 | + * ObjectQL Formula Plugin |
| 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 | +import type { RuntimePlugin, RuntimeContext, ObjectStackKernel } from '@objectstack/runtime'; |
| 10 | +import { FormulaEngine } from './formula-engine'; |
| 11 | +import type { FormulaEngineConfig } from '@objectql/types'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Configuration for the Formula Plugin |
| 15 | + */ |
| 16 | +export interface FormulaPluginConfig extends FormulaEngineConfig { |
| 17 | + /** |
| 18 | + * Enable automatic formula evaluation on queries |
| 19 | + * @default true |
| 20 | + */ |
| 21 | + autoEvaluateOnQuery?: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Formula Plugin |
| 26 | + * |
| 27 | + * Wraps the ObjectQL Formula Engine as an ObjectStack plugin. |
| 28 | + * Registers formula evaluation capabilities into the kernel. |
| 29 | + */ |
| 30 | +export class FormulaPlugin implements RuntimePlugin { |
| 31 | + name = '@objectql/formulas'; |
| 32 | + version = '4.0.0'; |
| 33 | + |
| 34 | + private engine: FormulaEngine; |
| 35 | + private config: FormulaPluginConfig; |
| 36 | + |
| 37 | + constructor(config: FormulaPluginConfig = {}) { |
| 38 | + this.config = { |
| 39 | + autoEvaluateOnQuery: true, |
| 40 | + ...config |
| 41 | + }; |
| 42 | + |
| 43 | + // Initialize the formula engine with configuration |
| 44 | + this.engine = new FormulaEngine(config); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Install the plugin into the kernel |
| 49 | + * Registers formula evaluation capabilities |
| 50 | + */ |
| 51 | + async install(ctx: RuntimeContext): Promise<void> { |
| 52 | + const kernel = ctx.engine as ObjectStackKernel; |
| 53 | + |
| 54 | + console.log(`[${this.name}] Installing formula plugin...`); |
| 55 | + |
| 56 | + // Register formula provider if the kernel supports it |
| 57 | + this.registerFormulaProvider(kernel); |
| 58 | + |
| 59 | + // Register formula evaluation middleware if auto-evaluation is enabled |
| 60 | + if (this.config.autoEvaluateOnQuery !== false) { |
| 61 | + this.registerFormulaMiddleware(kernel); |
| 62 | + } |
| 63 | + |
| 64 | + console.log(`[${this.name}] Formula plugin installed`); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Register the formula provider with the kernel |
| 69 | + * @private |
| 70 | + */ |
| 71 | + private registerFormulaProvider(kernel: ObjectStackKernel): void { |
| 72 | + // Check if kernel supports formula provider registration |
| 73 | + // Note: Using type assertion since registerFormulaProvider may not be in the interface |
| 74 | + const kernelWithFormulas = kernel as any; |
| 75 | + |
| 76 | + if (typeof kernelWithFormulas.registerFormulaProvider === 'function') { |
| 77 | + kernelWithFormulas.registerFormulaProvider({ |
| 78 | + evaluate: (formula: string, context: any) => { |
| 79 | + // Delegate to the formula engine |
| 80 | + // Note: In a real implementation, we would need to properly construct |
| 81 | + // the FormulaContext from the provided context |
| 82 | + return this.engine.evaluate( |
| 83 | + formula, |
| 84 | + context, |
| 85 | + 'text', // default data type |
| 86 | + {} |
| 87 | + ); |
| 88 | + }, |
| 89 | + validate: (expression: string) => { |
| 90 | + return this.engine.validate(expression); |
| 91 | + }, |
| 92 | + extractMetadata: (fieldName: string, expression: string, dataType: any) => { |
| 93 | + return this.engine.extractMetadata(fieldName, expression, dataType); |
| 94 | + } |
| 95 | + }); |
| 96 | + } else { |
| 97 | + // If the kernel doesn't support formula provider registration yet, |
| 98 | + // we still register the engine for direct access |
| 99 | + kernelWithFormulas.formulaEngine = this.engine; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Register formula evaluation middleware |
| 105 | + * @private |
| 106 | + */ |
| 107 | + private registerFormulaMiddleware(kernel: ObjectStackKernel): void { |
| 108 | + // Check if kernel supports middleware hooks |
| 109 | + const kernelWithHooks = kernel as any; |
| 110 | + |
| 111 | + if (typeof kernelWithHooks.use === 'function') { |
| 112 | + // Register middleware to evaluate formulas after queries |
| 113 | + kernelWithHooks.use('afterQuery', async (context: any) => { |
| 114 | + // Formula evaluation logic would go here |
| 115 | + // This would automatically compute formula fields after data is retrieved |
| 116 | + if (context.results && context.metadata?.fields) { |
| 117 | + // Iterate through fields and evaluate formulas |
| 118 | + // const formulaFields = Object.entries(context.metadata.fields) |
| 119 | + // .filter(([_, fieldConfig]) => (fieldConfig as any).formula); |
| 120 | + // |
| 121 | + // for (const record of context.results) { |
| 122 | + // for (const [fieldName, fieldConfig] of formulaFields) { |
| 123 | + // const formula = (fieldConfig as any).formula; |
| 124 | + // const result = this.engine.evaluate(formula, /* context */, /* dataType */); |
| 125 | + // record[fieldName] = result.value; |
| 126 | + // } |
| 127 | + // } |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Get the formula engine instance for direct access |
| 135 | + */ |
| 136 | + getEngine(): FormulaEngine { |
| 137 | + return this.engine; |
| 138 | + } |
| 139 | +} |
0 commit comments