-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
405 lines (348 loc) · 12.4 KB
/
index.ts
File metadata and controls
405 lines (348 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { QueryAST, HookContext } from '@objectstack/spec/data';
import { ObjectStackManifest } from '@objectstack/spec/system';
import { DriverInterface, DriverOptions } from '@objectstack/spec/system';
import { IDataEngine, QueryOptions } from '@objectstack/spec/system';
import { SchemaRegistry } from './registry';
// Export Registry for consumers
export { SchemaRegistry } from './registry';
export type HookHandler = (context: HookContext) => Promise<void> | void;
/**
* Host Context provided to plugins
*/
export interface PluginContext {
ql: ObjectQL;
logger: Console;
// Extensible map for host-specific globals (like HTTP Router, etc.)
[key: string]: any;
}
/**
* ObjectQL Engine
*
* Implements the IDataEngine interface for data persistence.
*/
export class ObjectQL implements IDataEngine {
private drivers = new Map<string, DriverInterface>();
private defaultDriver: string | null = null;
// Hooks Registry
private hooks: Record<string, HookHandler[]> = {
'beforeFind': [], 'afterFind': [],
'beforeInsert': [], 'afterInsert': [],
'beforeUpdate': [], 'afterUpdate': [],
'beforeDelete': [], 'afterDelete': [],
};
// Host provided context additions (e.g. Server router)
private hostContext: Record<string, any> = {};
constructor(hostContext: Record<string, any> = {}) {
this.hostContext = hostContext;
console.log(`[ObjectQL] Engine Instance Created`);
}
/**
* Load and Register a Plugin
*/
async use(manifestPart: any, runtimePart?: any) {
// 1. Validate / Register Manifest
if (manifestPart) {
// 1. Handle Module Imports (commonjs/esm interop)
// If the passed object is a module namespace with a default export, use that.
const manifest = manifestPart.default || manifestPart;
// In a real scenario, we might strictly parse this using Zod
// For now, simple ID check
const id = manifest.id || manifest.name;
if (!id) {
console.warn(`[ObjectQL] Plugin manifest missing ID (keys: ${Object.keys(manifest)})`, manifest);
// Don't return, try to proceed if it looks like an App (Apps might use 'name' instead of 'id')
// return;
}
console.log(`[ObjectQL] Loading Plugin: ${id}`);
SchemaRegistry.registerPlugin(manifest as ObjectStackManifest);
// Register Objects from App/Plugin
if (manifest.objects) {
for (const obj of manifest.objects) {
// Ensure object name is registered globally
SchemaRegistry.registerObject(obj);
console.log(`[ObjectQL] Registered Object: ${obj.name}`);
}
}
// Register contributions
if (manifest.contributes?.kinds) {
for (const kind of manifest.contributes.kinds) {
SchemaRegistry.registerKind(kind);
}
}
// Register Data Seeding (Lazy execution or immediate?)
// We store it in a temporary registry or execute immediately if engine is ready.
// Since `use` is init time, we might need to store it and run later in `seed()`.
// For this MVP, let's attach it to the manifest object in registry so Kernel can find it.
}
// 2. Execute Runtime
if (runtimePart) {
const pluginDef = (runtimePart as any).default || runtimePart;
if (pluginDef.onEnable) {
const context: PluginContext = {
ql: this,
logger: console,
// Expose the driver registry helper explicitly if needed
drivers: {
register: (driver: DriverInterface) => this.registerDriver(driver)
},
...this.hostContext
};
await pluginDef.onEnable(context);
}
}
}
/**
* Register a hook
* @param event The event name (e.g. 'beforeFind', 'afterInsert')
* @param handler The handler function
*/
registerHook(event: string, handler: HookHandler) {
if (!this.hooks[event]) {
this.hooks[event] = [];
}
this.hooks[event].push(handler);
console.log(`[ObjectQL] Registered hook for ${event}`);
}
private async triggerHooks(event: string, context: HookContext) {
const handlers = this.hooks[event] || [];
for (const handler of handlers) {
// In a real system, we might want to catch errors here or allow them to bubble up
await handler(context);
}
}
/**
* Register a new storage driver
*/
registerDriver(driver: DriverInterface, isDefault: boolean = false) {
if (this.drivers.has(driver.name)) {
console.warn(`[ObjectQL] Driver ${driver.name} is already registered. Skipping.`);
return;
}
this.drivers.set(driver.name, driver);
console.log(`[ObjectQL] Registered driver: ${driver.name} v${driver.version}`);
if (isDefault || this.drivers.size === 1) {
this.defaultDriver = driver.name;
}
}
/**
* Helper to get object definition
*/
getSchema(objectName: string) {
return SchemaRegistry.getObject(objectName);
}
/**
* Helper to get the target driver
*/
private getDriver(objectName: string): DriverInterface {
const object = SchemaRegistry.getObject(objectName);
// 1. If object definition exists, check for explicit datasource
if (object) {
const datasourceName = object.datasource || 'default';
// If configured for 'default', try to find the default driver
if (datasourceName === 'default') {
if (this.defaultDriver && this.drivers.has(this.defaultDriver)) {
return this.drivers.get(this.defaultDriver)!;
}
// Fallback: If 'default' not explicitly set, use the first available driver?
// Better to be strict.
} else {
// Specific datasource requested
if (this.drivers.has(datasourceName)) {
return this.drivers.get(datasourceName)!;
}
// If not found, fall back to default? Or error?
// Standard behavior: Error if specific datasource is missing.
throw new Error(`[ObjectQL] Datasource '${datasourceName}' configured for object '${objectName}' is not registered.`);
}
}
// 2. Fallback for ad-hoc objects or missing definitions
// If we have a default driver, use it.
if (this.defaultDriver) {
if (!object) {
console.warn(`[ObjectQL] Object '${objectName}' not found in registry. Using default driver.`);
}
return this.drivers.get(this.defaultDriver)!;
}
throw new Error(`[ObjectQL] No driver available for object '${objectName}'`);
}
/**
* Initialize the engine and all registered drivers
*/
async init() {
console.log('[ObjectQL] Initializing drivers...');
for (const [name, driver] of this.drivers) {
try {
await driver.connect();
} catch (e) {
console.error(`[ObjectQL] Failed to connect driver ${name}`, e);
}
}
// In a real app, we would sync schemas here
}
async destroy() {
for (const driver of this.drivers.values()) {
await driver.disconnect();
}
}
// ============================================
// Data Access Methods (IDataEngine Interface)
// ============================================
/**
* Find records matching a query (IDataEngine interface)
*
* @param object - Object name
* @param query - Query options (IDataEngine format)
* @returns Promise resolving to array of records
*/
async find(object: string, query?: QueryOptions): Promise<any[]> {
const driver = this.getDriver(object);
// Convert QueryOptions to QueryAST
let ast: QueryAST = { object };
if (query) {
// Map QueryOptions to QueryAST
if (query.filter) {
ast.where = query.filter;
}
if (query.select) {
ast.fields = query.select;
}
if (query.sort) {
// Convert sort Record to orderBy array
// sort: { createdAt: -1, name: 'asc' } => orderBy: [{ field: 'createdAt', order: 'desc' }, { field: 'name', order: 'asc' }]
ast.orderBy = Object.entries(query.sort).map(([field, order]) => ({
field,
order: (order === -1 || order === 'desc') ? 'desc' : 'asc'
}));
}
// Handle both limit and top (top takes precedence)
if (query.top !== undefined) {
ast.limit = query.top;
} else if (query.limit !== undefined) {
ast.limit = query.limit;
}
if (query.skip !== undefined) {
ast.offset = query.skip;
}
}
// Set default limit if not specified
if (ast.limit === undefined) ast.limit = 100;
// Trigger Before Hook
const hookContext: HookContext = {
object,
event: 'beforeFind',
input: { ast, options: undefined },
ql: this
};
await this.triggerHooks('beforeFind', hookContext);
try {
const result = await driver.find(object, hookContext.input.ast, hookContext.input.options);
// Trigger After Hook
hookContext.event = 'afterFind';
hookContext.result = result;
await this.triggerHooks('afterFind', hookContext);
return hookContext.result;
} catch (e) {
// hookContext.error = e;
throw e;
}
}
async findOne(object: string, idOrQuery: string | any, options?: DriverOptions) {
const driver = this.getDriver(object);
let ast: QueryAST;
if (typeof idOrQuery === 'string') {
ast = {
object,
where: { _id: idOrQuery }
};
} else {
// Assume query object
// reuse logic from find() or just wrap it
if (idOrQuery.where || idOrQuery.fields) {
ast = { object, ...idOrQuery };
} else {
ast = { object, where: idOrQuery };
}
}
// Limit 1 for findOne
ast.limit = 1;
return driver.findOne(object, ast, options);
}
/**
* Insert a new record (IDataEngine interface)
*
* @param object - Object name
* @param data - Data to insert
* @returns Promise resolving to the created record
*/
async insert(object: string, data: any): Promise<any> {
const driver = this.getDriver(object);
// 1. Get Schema
const schema = SchemaRegistry.getObject(object);
if (schema) {
// TODO: Validation Logic
// validate(schema, data);
}
// 2. Trigger Before Hook
const hookContext: HookContext = {
object,
event: 'beforeInsert',
input: { data, options: undefined },
ql: this
};
await this.triggerHooks('beforeInsert', hookContext);
// 3. Execute Driver
const result = await driver.create(object, hookContext.input.data, hookContext.input.options);
// 4. Trigger After Hook
hookContext.event = 'afterInsert';
hookContext.result = result;
await this.triggerHooks('afterInsert', hookContext);
return hookContext.result;
}
/**
* Update a record by ID (IDataEngine interface)
*
* @param object - Object name
* @param id - Record ID
* @param data - Updated data
* @returns Promise resolving to the updated record
*/
async update(object: string, id: any, data: any): Promise<any> {
const driver = this.getDriver(object);
const hookContext: HookContext = {
object,
event: 'beforeUpdate',
input: { id, data, options: undefined },
ql: this
};
await this.triggerHooks('beforeUpdate', hookContext);
const result = await driver.update(object, hookContext.input.id, hookContext.input.data, hookContext.input.options);
hookContext.event = 'afterUpdate';
hookContext.result = result;
await this.triggerHooks('afterUpdate', hookContext);
return hookContext.result;
}
/**
* Delete a record by ID (IDataEngine interface)
*
* @param object - Object name
* @param id - Record ID
* @returns Promise resolving to true if deleted, false otherwise
*/
async delete(object: string, id: any): Promise<boolean> {
const driver = this.getDriver(object);
const hookContext: HookContext = {
object,
event: 'beforeDelete',
input: { id, options: undefined },
ql: this
};
await this.triggerHooks('beforeDelete', hookContext);
const result = await driver.delete(object, hookContext.input.id, hookContext.input.options);
hookContext.event = 'afterDelete';
hookContext.result = result;
await this.triggerHooks('afterDelete', hookContext);
// Return boolean - true if deletion was successful
// The driver.delete should return the deleted record or null
return hookContext.result !== null && hookContext.result !== undefined;
}
}