Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/foundation/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ export class ObjectQL extends UpstreamObjectQL {
// Store drivers for registration during init()
if (config.datasources) {
for (const [name, driver] of Object.entries(config.datasources)) {
if (!(driver as any).name) {
(driver as any).name = name;
}
// Always set driver.name to the config key so datasource(name) lookups work
(driver as any).name = name;
// Cast: local Driver interface is structurally compatible with upstream DriverInterface
this.pendingDrivers.push({
name,
Expand Down
125 changes: 114 additions & 11 deletions packages/foundation/core/test/__mocks__/@objectstack/objectql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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 });
}
Expand Down Expand Up @@ -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 */ }
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

driver.findOne is being called with { _id: id } as the id argument. All in-repo drivers use the signature findOne(objectName, id, query?, options?), so this call will look up a record with id "[object Object]" and previousData will always be undefined, breaking hooks that rely on it (e.g., multitenancy/validator). Call driver.findOne(name, id, undefined, options) (or driver.findOne(name, id) if options aren’t needed) to fetch the prior record.

Suggested change
try { previousData = await driver.findOne(name, { _id: id }); } catch (_e) { /* ignore */ }
try { previousData = await driver.findOne(name, id, undefined, options); } catch (_e) { /* ignore */ }

Copilot uses AI. Check for mistakes.
}
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
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as update(): driver.findOne(name, { _id: id }) passes an object into the id parameter, so drivers won’t return the existing record and previousData will be missing in before/afterDelete hooks. Use driver.findOne(name, id, undefined, options) (or driver.findOne(name, id)) instead.

Copilot uses AI. Check for mistakes.
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;
}
})
};
Expand Down