feat: enhance query handling with modules and plugins
- Added ManyToMany relationship for plugins in Query entity. - Updated QueryExecuterController to accept structured query data. - Enhanced QueryExecuterService to support plugin initialization and execution. - Implemented QueryHandlerService methods for creating and updating queries, modules, and plugins. - Introduced new endpoints for creating and adding modules and plugins to queries. - Created Plugin base class and DatabasePlugin implementation for database interactions. - Updated VM class to integrate plugin functionality during script execution. - Added test cases for project, query, module, and plugin operations.
This commit is contained in:
@ -9,7 +9,10 @@ export class QueryExecuterController {
|
||||
) {}
|
||||
|
||||
@Post("/run/:token")
|
||||
async runQuery(@Param("token") token: string, @Body() query: any) {
|
||||
async runQuery(
|
||||
@Param("token") token: string,
|
||||
@Body() query: Record<string, any>
|
||||
) {
|
||||
return this.queryExecuterService.runQuery(token, query);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import { Query } from "../entities/query.entity";
|
||||
import { Repository } from "typeorm";
|
||||
import { Vm } from "../../vm/vm.class";
|
||||
import { VModule } from "../../vm/module.class";
|
||||
import { DatabasePlugin } from "src/vm/plugins/database.plugin";
|
||||
|
||||
@Injectable()
|
||||
export class QueryExecuterService {
|
||||
@ -21,18 +22,38 @@ export class QueryExecuterService {
|
||||
throw new Error("Query not found");
|
||||
}
|
||||
|
||||
const vm = this.createVm(query);
|
||||
const result = await vm.runScript(query.source);
|
||||
const vm = await this.createVm(query);
|
||||
const result = await vm.runScript(query.source, queryData);
|
||||
|
||||
return { message: "Query executed", result, query: queryData };
|
||||
}
|
||||
|
||||
private createVm(query: Query) {
|
||||
return new Vm({
|
||||
memoryLimit: 5,
|
||||
private async createVm(query: Query) {
|
||||
if (query.modules === undefined) {
|
||||
query.modules = [];
|
||||
}
|
||||
|
||||
if (query.plugins === undefined) {
|
||||
query.plugins = [];
|
||||
}
|
||||
|
||||
const vm = new Vm({
|
||||
memoryLimit: 128,
|
||||
modules: query.modules.map((module) => {
|
||||
return new VModule(module.name, module.sourcePath);
|
||||
}),
|
||||
plugins: query.plugins.map((plugin) => {
|
||||
switch (plugin.class) {
|
||||
case "DATABASE": {
|
||||
const config = JSON.parse(plugin.config);
|
||||
return DatabasePlugin.init(plugin.name, config);
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unknown plugin class: ${plugin.class}`);
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
||||
return await vm.init();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user