Refactor VM and Plugin Management
- Removed the `plugins.constants.ts` file as it was no longer needed. - Updated the `vm.class.ts` to improve result handling and logging. - Introduced `vm.constants.ts` to manage registered plugins and modules. - Simplified the test payload in `case1-payload.js` by removing unnecessary insert logic. - Enhanced `case1.ts` to include database and migration setup, improving test clarity. - Deleted unused functions for adding modules and plugins, streamlining the codebase.
This commit is contained in:
@ -1,8 +1,6 @@
|
||||
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";
|
||||
|
||||
@ -11,111 +9,10 @@ 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
|
||||
) {}
|
||||
|
||||
private async createOrGetVmModule(
|
||||
name: string,
|
||||
sourcePath: string
|
||||
): Promise<VMModule> {
|
||||
const vmModule = await this.moduleRepository.findOne({
|
||||
where: {
|
||||
name,
|
||||
},
|
||||
});
|
||||
|
||||
if (vmModule) {
|
||||
return vmModule;
|
||||
}
|
||||
|
||||
const newModule = this.moduleRepository.create({
|
||||
sourcePath,
|
||||
name,
|
||||
});
|
||||
|
||||
return this.moduleRepository.save(newModule);
|
||||
}
|
||||
|
||||
private async createOrGetPlugin(
|
||||
name: string,
|
||||
className: string,
|
||||
query: Query,
|
||||
config: Record<string, any>
|
||||
): Promise<VmPlugin> {
|
||||
const plugin = await this.pluginRepository.findOne({
|
||||
where: {
|
||||
name,
|
||||
query: {
|
||||
id: query.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (plugin) {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
const newPlugin = this.pluginRepository.create({
|
||||
name,
|
||||
class: className,
|
||||
config: JSON.stringify(config),
|
||||
query,
|
||||
});
|
||||
|
||||
return this.pluginRepository.save(newPlugin);
|
||||
}
|
||||
|
||||
// TODO: make it nice
|
||||
async createDefaults(query: Query): Promise<void> {
|
||||
const asyncCallModule = await this.createOrGetVmModule(
|
||||
"asyncCall",
|
||||
"dist/vm/modules/async.js"
|
||||
);
|
||||
const squelModule = await this.createOrGetVmModule(
|
||||
"query",
|
||||
"node_modules/squel/dist/squel.min.js"
|
||||
);
|
||||
|
||||
query.modules = [asyncCallModule, squelModule];
|
||||
|
||||
const dbPlugin = await this.createOrGetPlugin("db", "DATABASE", query, {
|
||||
// TODO: take it from database handler
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USERNAME,
|
||||
port: process.env.DB_PORT,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: "test",
|
||||
});
|
||||
|
||||
query.plugins = [dbPlugin];
|
||||
|
||||
await this.queryRepository.save(query);
|
||||
}
|
||||
|
||||
parseImports(source: string): string[] {
|
||||
const importRegex =
|
||||
/import\s+(?:[\w*\s{},]*\s+from\s+)?["']([^"']+)["'];?/g;
|
||||
const imports: string[] = [];
|
||||
let match: RegExpExecArray | null;
|
||||
|
||||
while ((match = importRegex.exec(source)) !== null) {
|
||||
imports.push(match[1]);
|
||||
}
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
clearImports(source: string): string {
|
||||
return source
|
||||
.replace(/import\s+(?:[\w*\s{},]*\s+from\s+)?["']([^"']+)["'];?/g, "")
|
||||
.trim();
|
||||
}
|
||||
|
||||
async createQuery(queryData: { projectToken: string; source: string }) {
|
||||
const project = await this.projectService.findById(queryData.projectToken);
|
||||
|
||||
@ -128,11 +25,6 @@ export class QueryHandlerService {
|
||||
|
||||
const query = this.queryRepository.create(queryData);
|
||||
|
||||
const imports = this.parseImports(query.source);
|
||||
console.log("Parsed imports:", imports);
|
||||
|
||||
query.source = this.clearImports(query.source);
|
||||
|
||||
await this.queryRepository.save(query);
|
||||
|
||||
return query;
|
||||
@ -148,62 +40,4 @@ export class QueryHandlerService {
|
||||
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