|
| 1 | +import { DriverInterface, DriverOptions, QueryAST } from '@objectstack/spec'; |
| 2 | + |
| 3 | +/** |
| 4 | + * 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 | + */ |
| 13 | +export class ObjectQL { |
| 14 | + constructor(private driver: DriverInterface) { |
| 15 | + console.log(`[ObjectQL] Initialized with driver: ${driver.name} v${driver.version}`); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Initialize the engine |
| 20 | + */ |
| 21 | + async init() { |
| 22 | + await this.driver.connect(); |
| 23 | + // In a real app, we would sync schemas here |
| 24 | + } |
| 25 | + |
| 26 | + async destroy() { |
| 27 | + await this.driver.disconnect(); |
| 28 | + } |
| 29 | + |
| 30 | + // ============================================ |
| 31 | + // Data Access Methods |
| 32 | + // ============================================ |
| 33 | + |
| 34 | + async find(object: string, filters: any = {}, options?: DriverOptions) { |
| 35 | + console.log(`[ObjectQL] Finding ${object}...`); |
| 36 | + |
| 37 | + // Transform simplified filters to QueryAST |
| 38 | + // This is a simplified "Mock" transform. |
| 39 | + // Real implementation would parse complex JSON or FilterBuilders. |
| 40 | + const ast: QueryAST = { |
| 41 | + // Pass through if it looks like AST, otherwise empty |
| 42 | + // In this demo, we assume the caller passes a simplified object or raw AST |
| 43 | + filters: filters.filters || undefined, |
| 44 | + top: filters.top || 100, |
| 45 | + sort: filters.sort || [] |
| 46 | + }; |
| 47 | + |
| 48 | + return this.driver.find(object, ast, options); |
| 49 | + } |
| 50 | + |
| 51 | + async insert(object: string, data: Record<string, any>, options?: DriverOptions) { |
| 52 | + console.log(`[ObjectQL] Creating ${object}...`); |
| 53 | + // 1. Validate Schema |
| 54 | + // 2. Run "Before Insert" Triggers |
| 55 | + |
| 56 | + const result = await this.driver.create(object, data, options); |
| 57 | + |
| 58 | + // 3. Run "After Insert" Triggers |
| 59 | + return result; |
| 60 | + } |
| 61 | + |
| 62 | + async update(object: string, id: string, data: Record<string, any>, options?: DriverOptions) { |
| 63 | + console.log(`[ObjectQL] Updating ${object} ${id}...`); |
| 64 | + return this.driver.update(object, id, data, options); |
| 65 | + } |
| 66 | + |
| 67 | + async delete(object: string, id: string, options?: DriverOptions) { |
| 68 | + console.log(`[ObjectQL] Deleting ${object} ${id}...`); |
| 69 | + return this.driver.delete(object, id, options); |
| 70 | + } |
| 71 | +} |
0 commit comments