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:
lborv
2025-09-21 01:07:51 +03:00
parent d90c85d66f
commit 8eba1d1344
26 changed files with 620 additions and 25 deletions

View File

@ -1,4 +1,106 @@
import { Injectable } from "@nestjs/common";
import { Inject, Injectable } from "@nestjs/common";
import { Repository } from "typeorm";
import { Query } from "../entities/query.entity";
import { VMModule } from "../entities/module.entity";
import { VmPlugin } from "../entities/plugin.entity";
import { InjectRepository } from "@nestjs/typeorm";
import { ProjectService } from "src/project/project.service";
@Injectable()
export class QueryHandlerService {}
export class QueryHandlerService {
constructor(
@InjectRepository(Query)
private readonly queryRepository: Repository<Query>,
@InjectRepository(VMModule)
private readonly moduleRepository: Repository<VMModule>,
@InjectRepository(VmPlugin)
private readonly pluginRepository: Repository<VmPlugin>,
@Inject(ProjectService)
private readonly projectService: ProjectService
) {}
async createQuery(queryData: { projectToken: string; source: string }) {
const project = await this.projectService.findByToken(
queryData.projectToken
);
if (!project) {
throw new Error("Project not found");
}
queryData["project"] = project;
delete queryData.projectToken;
const query = this.queryRepository.create(queryData);
return this.queryRepository.save(query);
}
async updateQuery(id: string, updateData: Partial<Query>) {
const query = await this.queryRepository.findOne({ where: { id } });
if (!query) {
throw new Error("Query not found");
}
Object.assign(query, updateData);
return this.queryRepository.save(query);
}
async createModule(moduleData: { name: string; sourcePath: string }) {
const module = this.moduleRepository.create(moduleData);
return this.moduleRepository.save(module);
}
async createPlugin(pluginData: {
name: string;
class: string;
config: string;
}) {
const plugin = this.pluginRepository.create(pluginData);
return this.pluginRepository.save(plugin);
}
async addModuleToQuery(queryId: string, moduleId: string) {
const query = await this.queryRepository.findOne({
where: { id: queryId },
relations: ["modules"],
});
if (!query) {
throw new Error("Query not found");
}
const module = await this.moduleRepository.findOne({
where: { id: moduleId },
});
if (!module) {
throw new Error("Module not found");
}
query.modules.push(module);
return this.queryRepository.save(query);
}
async addPluginToQuery(queryId: string, pluginId: string) {
const query = await this.queryRepository.findOne({
where: { id: queryId },
relations: ["plugins"],
});
if (!query) {
throw new Error("Query not found");
}
const plugin = await this.pluginRepository.findOne({
where: { id: pluginId },
});
if (!plugin) {
throw new Error("Plugin not found");
}
query.plugins.push(plugin);
return this.queryRepository.save(query);
}
}