This commit is contained in:
Boris D
2025-09-22 18:35:02 +03:00
parent 51f8b0d773
commit fbbbd61838
23 changed files with 282 additions and 67 deletions

View File

@ -12,9 +12,6 @@ export class VMModule {
@Column({ type: "varchar", length: 255 })
name: string;
@Column({ type: "tinyint", default: 1 })
isInjectable: number;
@ManyToMany(() => Query, (query) => query.modules)
queries: Query[];
}

View File

@ -1,4 +1,4 @@
import { Column, Entity, ManyToMany, PrimaryGeneratedColumn } from "typeorm";
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Query } from "./query.entity";
@Entity("plugin")
@ -12,8 +12,8 @@ export class VmPlugin {
@Column({ type: "varchar", length: 255, nullable: false })
name: string;
@ManyToMany(() => Query, (query) => query.plugins)
queries: Query[];
@ManyToOne(() => Query, (query) => query.plugins)
query: Query;
@Column({ type: "varchar", length: 255 })
config: string;

View File

@ -4,7 +4,9 @@ import {
Entity,
ManyToMany,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
JoinTable,
} from "typeorm";
import { VMModule } from "./module.entity";
import { VmPlugin } from "./plugin.entity";
@ -24,8 +26,13 @@ export class Query {
isActive: number;
@ManyToMany(() => VMModule, (module) => module.queries)
@JoinTable({
name: "query_modules_module",
joinColumn: { name: "queryId", referencedColumnName: "id" },
inverseJoinColumn: { name: "moduleId", referencedColumnName: "id" },
})
modules: VMModule[];
@ManyToMany(() => VmPlugin, (plugin) => plugin.queries)
@OneToMany(() => VmPlugin, (plugin) => plugin.query)
plugins: VmPlugin[];
}

View File

@ -16,6 +16,7 @@ export class QueryExecuterService {
async runQuery(token: string, queryData: any) {
const query = await this.queryRepository.findOne({
where: { id: token },
relations: ["modules", "plugins"],
});
if (!query) {
@ -42,16 +43,18 @@ export class QueryExecuterService {
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);
plugins: await Promise.all(
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}`);
}
default:
throw new Error(`Unknown plugin class: ${plugin.class}`);
}
}),
})
),
});
return await vm.init();

View File

@ -19,6 +19,84 @@ export class QueryHandlerService {
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);
}
async createQuery(queryData: { projectToken: string; source: string }) {
const project = await this.projectService.findByToken(
queryData.projectToken
@ -32,7 +110,11 @@ export class QueryHandlerService {
delete queryData.projectToken;
const query = this.queryRepository.create(queryData);
return this.queryRepository.save(query);
await this.queryRepository.save(query);
await this.createDefaults(query);
return query;
}
async updateQuery(id: string, updateData: Partial<Query>) {