-
Notifications
You must be signed in to change notification settings - Fork 2
fix: complete @objectstack/objectql mock and fix bridge driver naming #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,18 @@ export class ObjectQL { | |
|
|
||
| async connect() {} | ||
| async disconnect() {} | ||
| async init() {} | ||
| async init() { | ||
| // Initialize drivers (connect + sync schema) | ||
| for (const [_name, driver] of this.drivers) { | ||
| if (driver.connect) { | ||
| await driver.connect(); | ||
| } | ||
| if (driver.init) { | ||
| const objects = SchemaRegistry.getAllObjects(); | ||
| await driver.init(objects); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| registerDriver(driver: any, isDefault: boolean = false) { | ||
| if (!driver.name) { | ||
|
|
@@ -31,6 +42,18 @@ export class ObjectQL { | |
| this.defaultDriver = driver.name; | ||
| } | ||
| } | ||
|
|
||
| datasource(name: string): any { | ||
| const driver = this.drivers.get(name); | ||
| if (!driver) { | ||
| throw new Error(`[ObjectQL] Datasource '${name}' not found`); | ||
| } | ||
| return driver; | ||
| } | ||
|
|
||
| getDriverByName(name: string): any { | ||
| return this.drivers.get(name); | ||
| } | ||
|
|
||
| registerObject(schema: any, packageId: string = '__runtime__', namespace?: string): string { | ||
| // Auto-assign field names from keys | ||
|
|
@@ -68,6 +91,10 @@ export class ObjectQL { | |
| this.hooks.get(event)!.push({ handler, options }); | ||
| } | ||
|
|
||
| on(event: string, objectName: string, handler: any, packageId?: string) { | ||
| this.registerHook(event, handler, { object: objectName, packageId }); | ||
| } | ||
|
|
||
| registerMiddleware(fn: any, options?: { object?: string }) { | ||
| this.middlewares.push({ fn, object: options?.object }); | ||
| } | ||
|
|
@@ -128,25 +155,101 @@ export class ObjectQL { | |
| }); | ||
| return opCtx.result; | ||
| }, | ||
| create: async (data: any) => { | ||
| const driver = this.drivers.get(this.defaultDriver || this.drivers.keys().next().value); | ||
| const opCtx = { object: name, operation: 'insert', data, context: options, result: undefined as any }; | ||
| await this.executeWithMiddleware(opCtx, async () => { | ||
| const hookContext: any = { | ||
| object: name, event: 'beforeCreate', | ||
| input: { data: opCtx.data }, session: options, | ||
| data: opCtx.data, user: options.userId ? { id: options.userId } : options.user | ||
| }; | ||
| await this.triggerHooks('beforeCreate', hookContext); | ||
| const finalData = hookContext.data || hookContext.input.data; | ||
| const result = driver?.create ? await driver.create(name, finalData, options) : finalData; | ||
| hookContext.event = 'afterCreate'; | ||
| hookContext.result = result; | ||
| await this.triggerHooks('afterCreate', hookContext); | ||
| return hookContext.result; | ||
| }); | ||
| return opCtx.result; | ||
| }, | ||
| insert: async (data: any) => { | ||
| const driver = this.drivers.get(this.defaultDriver || this.drivers.keys().next().value); | ||
| if (driver && driver.insert) { | ||
| return driver.insert(name, data); | ||
| } | ||
| return data; | ||
| const opCtx = { object: name, operation: 'insert', data, context: options, result: undefined as any }; | ||
| await this.executeWithMiddleware(opCtx, async () => { | ||
| const hookContext: any = { | ||
| object: name, event: 'beforeCreate', | ||
| input: { data: opCtx.data }, session: options, | ||
| data: opCtx.data, user: options.userId ? { id: options.userId } : options.user | ||
| }; | ||
| await this.triggerHooks('beforeCreate', hookContext); | ||
| const finalData = hookContext.data || hookContext.input.data; | ||
| const result = driver?.create ? await driver.create(name, finalData, options) | ||
| : driver?.insert ? await driver.insert(name, finalData) | ||
| : finalData; | ||
| hookContext.event = 'afterCreate'; | ||
| hookContext.result = result; | ||
| await this.triggerHooks('afterCreate', hookContext); | ||
| return hookContext.result; | ||
| }); | ||
| return opCtx.result; | ||
| }, | ||
| update: async (id: string, data: any) => { | ||
| const driver = this.drivers.get(this.defaultDriver || this.drivers.keys().next().value); | ||
| if (driver && driver.update) { | ||
| return driver.update(name, id, data); | ||
| } | ||
| return data; | ||
| const opCtx = { object: name, operation: 'update', data, context: options, result: undefined as any }; | ||
| await this.executeWithMiddleware(opCtx, async () => { | ||
| // Fetch previous data for hooks that need it | ||
| let previousData: any; | ||
| if (driver?.findOne) { | ||
| try { previousData = await driver.findOne(name, { _id: id }); } catch (_e) { /* ignore */ } | ||
| } | ||
| const hookContext: any = { | ||
| object: name, event: 'beforeUpdate', | ||
| input: { id, data: opCtx.data }, session: options, | ||
| data: opCtx.data, previousData, user: options.userId ? { id: options.userId } : options.user | ||
| }; | ||
| await this.triggerHooks('beforeUpdate', hookContext); | ||
| const finalData = hookContext.data || hookContext.input.data; | ||
| const result = driver?.update ? await driver.update(name, id, finalData, options) : finalData; | ||
| hookContext.event = 'afterUpdate'; | ||
| hookContext.result = result; | ||
| await this.triggerHooks('afterUpdate', hookContext); | ||
| return hookContext.result; | ||
| }); | ||
| return opCtx.result; | ||
| }, | ||
| delete: async (id: string) => { | ||
| const driver = this.drivers.get(this.defaultDriver || this.drivers.keys().next().value); | ||
| if (driver && driver.delete) { | ||
| return driver.delete(name, id); | ||
| const opCtx = { object: name, operation: 'delete', context: options, result: undefined as any }; | ||
| await this.executeWithMiddleware(opCtx, async () => { | ||
| // Fetch current data for hooks that need it | ||
| let previousData: any; | ||
| if (driver?.findOne) { | ||
| try { previousData = await driver.findOne(name, { _id: id }); } catch (_e) { /* ignore */ } | ||
| } | ||
|
Comment on lines
+227
to
+230
|
||
| const hookContext: any = { | ||
| object: name, event: 'beforeDelete', | ||
| input: { id }, session: options, | ||
| data: previousData, previousData, user: options.userId ? { id: options.userId } : options.user | ||
| }; | ||
| await this.triggerHooks('beforeDelete', hookContext); | ||
| const result = driver?.delete ? await driver.delete(name, id) : true; | ||
| hookContext.event = 'afterDelete'; | ||
| hookContext.result = result; | ||
| await this.triggerHooks('afterDelete', hookContext); | ||
| return hookContext.result; | ||
| }); | ||
| return opCtx.result; | ||
| }, | ||
| count: async (filter?: any) => { | ||
| const driver = this.drivers.get(this.defaultDriver || this.drivers.keys().next().value); | ||
| if (driver?.count) { | ||
| return driver.count(name, filter); | ||
| } | ||
| // Fallback: use find and count | ||
| const results = driver?.find ? await driver.find(name, filter) : []; | ||
| return Array.isArray(results) ? results.length : 0; | ||
| } | ||
| }) | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
driver.findOneis being called with{ _id: id }as the id argument. All in-repo drivers use the signaturefindOne(objectName, id, query?, options?), so this call will look up a record with id "[object Object]" andpreviousDatawill always be undefined, breaking hooks that rely on it (e.g., multitenancy/validator). Calldriver.findOne(name, id, undefined, options)(ordriver.findOne(name, id)if options aren’t needed) to fetch the prior record.