Refactor query execution logic to return script results and handle errors properly
This commit is contained in:
@ -22,16 +22,9 @@ export class QueryExecuterService {
|
||||
}
|
||||
|
||||
const vm = this.createVm(query);
|
||||
const result = await vm.runScript(query.source);
|
||||
|
||||
vm.setFunction("result", (result: string) => {
|
||||
console.log("Query Result:", result);
|
||||
});
|
||||
|
||||
await vm.runScript(query.source);
|
||||
|
||||
// Here you would add the logic to actually execute the query
|
||||
// against your database or data source. This is a placeholder.
|
||||
return { message: "Query executed", query: queryData };
|
||||
return { message: "Query executed", result, query: queryData };
|
||||
}
|
||||
|
||||
private createVm(query: Query) {
|
||||
|
||||
@ -29,7 +29,41 @@ export class Vm {
|
||||
}
|
||||
|
||||
async runScript(script: string) {
|
||||
const compiledScript = await this.context.isolate.compileScript(script);
|
||||
return compiledScript.run(this.context);
|
||||
let resolvePromise: (value: any) => void;
|
||||
let rejectPromise: (reason?: any) => void;
|
||||
|
||||
const resultPromise = new Promise<any>((resolve, reject) => {
|
||||
resolvePromise = resolve;
|
||||
rejectPromise = reject;
|
||||
});
|
||||
|
||||
this.setFunction("result", (...args) => {
|
||||
console.log("Script result:", args);
|
||||
resolvePromise(args);
|
||||
});
|
||||
|
||||
this.setFunction("error", (error: any) => {
|
||||
console.error("Script error:", error);
|
||||
rejectPromise(error);
|
||||
});
|
||||
|
||||
const scriptWithResult = `
|
||||
(async () => {
|
||||
${script}
|
||||
try {
|
||||
const result = await main()
|
||||
result(result)
|
||||
} catch (e) {
|
||||
error(e)
|
||||
}
|
||||
})()
|
||||
`;
|
||||
|
||||
const compiledScript = await this.context.isolate.compileScript(
|
||||
scriptWithResult
|
||||
);
|
||||
compiledScript.run(this.context);
|
||||
|
||||
return await resultPromise;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user