- 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.
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import { InjectRepository } from "@nestjs/typeorm";
|
|
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 {
|
|
constructor(
|
|
@InjectRepository(Query)
|
|
private readonly queryRepository: Repository<Query>
|
|
) {}
|
|
|
|
async runQuery(token: string, queryData: any) {
|
|
const query = await this.queryRepository.findOne({
|
|
where: { id: token },
|
|
});
|
|
|
|
if (!query) {
|
|
throw new Error("Query not found");
|
|
}
|
|
|
|
const vm = await this.createVm(query);
|
|
const result = await vm.runScript(query.source, queryData);
|
|
|
|
return { message: "Query executed", result, query: queryData };
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|