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:
@ -1,4 +1,4 @@
|
||||
import { Controller, Inject } from "@nestjs/common";
|
||||
import { Body, Controller, Inject, Post } from "@nestjs/common";
|
||||
import { QueryHandlerService } from "./query.handler.service";
|
||||
|
||||
@Controller("query")
|
||||
@ -7,4 +7,47 @@ export class QueryHandlerController {
|
||||
@Inject(QueryHandlerService)
|
||||
private readonly queryHandlerService: QueryHandlerService
|
||||
) {}
|
||||
|
||||
@Post("create")
|
||||
async createQuery(
|
||||
@Body() queryData: { projectToken: string; source: string }
|
||||
) {
|
||||
return this.queryHandlerService.createQuery(queryData);
|
||||
}
|
||||
|
||||
@Post("update/:id")
|
||||
async updateQuery(
|
||||
@Body() updateData: Partial<{ source: string }>,
|
||||
@Inject("id") id: string
|
||||
) {
|
||||
return this.queryHandlerService.updateQuery(id, updateData);
|
||||
}
|
||||
|
||||
@Post("module/create")
|
||||
async createModule(@Body() moduleData: { name: string; sourcePath: string }) {
|
||||
return this.queryHandlerService.createModule(moduleData);
|
||||
}
|
||||
|
||||
@Post("plugin/create")
|
||||
async createPlugin(
|
||||
@Body() pluginData: { name: string; class: string; config: string }
|
||||
) {
|
||||
return this.queryHandlerService.createPlugin(pluginData);
|
||||
}
|
||||
|
||||
@Post("module/add")
|
||||
async addModuleToQuery(@Body() data: { queryId: string; moduleId: string }) {
|
||||
return this.queryHandlerService.addModuleToQuery(
|
||||
data.queryId,
|
||||
data.moduleId
|
||||
);
|
||||
}
|
||||
|
||||
@Post("plugin/add")
|
||||
async addPluginToQuery(@Body() data: { queryId: string; pluginId: string }) {
|
||||
return this.queryHandlerService.addPluginToQuery(
|
||||
data.queryId,
|
||||
data.pluginId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user