|
1 | | -import { DriverInterface, DriverOptions, QueryAST } from '@objectstack/spec'; |
| 1 | +// Export core engine |
| 2 | +export { ObjectQL } from './engine'; |
2 | 3 |
|
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 | | - private drivers = new Map<string, DriverInterface>(); |
15 | | - private defaultDriver: string | null = null; |
16 | | - |
17 | | - constructor() { |
18 | | - console.log(`[ObjectQL] Engine Instance Created`); |
19 | | - } |
20 | | - |
21 | | - /** |
22 | | - * Register a new storage driver |
23 | | - */ |
24 | | - registerDriver(driver: DriverInterface, isDefault: boolean = false) { |
25 | | - if (this.drivers.has(driver.name)) { |
26 | | - console.warn(`[ObjectQL] Driver ${driver.name} is already registered. Skipping.`); |
27 | | - return; |
28 | | - } |
29 | | - |
30 | | - this.drivers.set(driver.name, driver); |
31 | | - console.log(`[ObjectQL] Registered driver: ${driver.name} v${driver.version}`); |
32 | | - |
33 | | - if (isDefault || this.drivers.size === 1) { |
34 | | - this.defaultDriver = driver.name; |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - /** |
39 | | - * Helper to get the target driver |
40 | | - */ |
41 | | - private getDriver(object: string): DriverInterface { |
42 | | - // TODO: Look up Object definition to see if it specifies a specific datasource/driver |
43 | | - // For now, always return default |
44 | | - if (!this.defaultDriver) { |
45 | | - throw new Error('[ObjectQL] No drivers registered!'); |
46 | | - } |
47 | | - return this.drivers.get(this.defaultDriver)!; |
48 | | - } |
49 | | - |
50 | | - /** |
51 | | - * Initialize the engine and all registered drivers |
52 | | - */ |
53 | | - async init() { |
54 | | - console.log('[ObjectQL] Initializing drivers...'); |
55 | | - for (const [name, driver] of this.drivers) { |
56 | | - try { |
57 | | - await driver.connect(); |
58 | | - } catch (e) { |
59 | | - console.error(`[ObjectQL] Failed to connect driver ${name}`, e); |
60 | | - } |
61 | | - } |
62 | | - // In a real app, we would sync schemas here |
63 | | - } |
64 | | - |
65 | | - async destroy() { |
66 | | - for (const driver of this.drivers.values()) { |
67 | | - await driver.disconnect(); |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - // ============================================ |
72 | | - // Data Access Methods |
73 | | - // ============================================ |
74 | | - |
75 | | - async find(object: string, filters: any = {}, options?: DriverOptions) { |
76 | | - const driver = this.getDriver(object); |
77 | | - console.log(`[ObjectQL] Finding ${object} on ${driver.name}...`); |
78 | | - |
79 | | - // Transform simplified filters to QueryAST |
80 | | - // This is a simplified "Mock" transform. |
81 | | - // Real implementation would parse complex JSON or FilterBuilders. |
82 | | - const ast: QueryAST = { |
83 | | - // Pass through if it looks like AST, otherwise empty |
84 | | - // In this demo, we assume the caller passes a simplified object or raw AST |
85 | | - filters: filters.filters || undefined, |
86 | | - top: filters.top || 100, |
87 | | - sort: filters.sort || [] |
88 | | - }; |
89 | | - |
90 | | - return driver.find(object, ast, options); |
91 | | - } |
92 | | - |
93 | | - async insert(object: string, data: Record<string, any>, options?: DriverOptions) { |
94 | | - const driver = this.getDriver(object); |
95 | | - console.log(`[ObjectQL] Creating ${object} on ${driver.name}...`); |
96 | | - // 1. Validate Schema |
97 | | - // 2. Run "Before Insert" Triggers |
98 | | - |
99 | | - const result = await driver.create(object, data, options); |
100 | | - |
101 | | - // 3. Run "After Insert" Triggers |
102 | | - return result; |
103 | | - } |
104 | | - |
105 | | - async update(object: string, id: string, data: Record<string, any>, options?: DriverOptions) { |
106 | | - const driver = this.getDriver(object); |
107 | | - console.log(`[ObjectQL] Updating ${object} ${id}...`); |
108 | | - return driver.update(object, id, data, options); |
109 | | - } |
110 | | - |
111 | | - async delete(object: string, id: string, options?: DriverOptions) { |
112 | | - const driver = this.getDriver(object); |
113 | | - console.log(`[ObjectQL] Deleting ${object} ${id}...`); |
114 | | - return driver.delete(object, id, options); |
115 | | - } |
116 | | -} |
| 4 | +// Re-export common types from spec for convenience |
| 5 | +export type { DriverInterface, DriverOptions, QueryAST } from '@objectstack/spec'; |
0 commit comments