Refactor code structure for improved readability and maintainability
This commit is contained in:
20
src/query/entities/module.entity.ts
Normal file
20
src/query/entities/module.entity.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { Column, Entity, ManyToMany, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Query } from "./query.enitity";
|
||||
|
||||
@Entity("module")
|
||||
export class Module {
|
||||
@PrimaryGeneratedColumn("uuid")
|
||||
id: string;
|
||||
|
||||
@Column({ type: "varchar", length: 255, nullable: false })
|
||||
sourcePath: string;
|
||||
|
||||
@Column({ type: "varchar", length: 255 })
|
||||
name: string;
|
||||
|
||||
@Column({ type: "tinyint", default: 1 })
|
||||
isInjectable: number;
|
||||
|
||||
@ManyToMany(() => Query, (query) => query.modules)
|
||||
queries: Query[];
|
||||
}
|
||||
27
src/query/entities/query.enitity.ts
Normal file
27
src/query/entities/query.enitity.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { Project } from "src/project/entities/project.entity";
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
ManyToMany,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
} from "typeorm";
|
||||
import { Module } from "./module.entity";
|
||||
|
||||
@Entity("query")
|
||||
export class Query {
|
||||
@PrimaryGeneratedColumn("uuid")
|
||||
id: string;
|
||||
|
||||
@ManyToOne(() => Project, (project) => project.queries)
|
||||
project: Project;
|
||||
|
||||
@Column({ type: "longtext" })
|
||||
source: string;
|
||||
|
||||
@Column({ type: "tinyint", default: 1 })
|
||||
isActive: number;
|
||||
|
||||
@ManyToMany(() => Module, (module) => module.queries)
|
||||
modules: Module[];
|
||||
}
|
||||
15
src/query/executer/query.executer.controller.ts
Normal file
15
src/query/executer/query.executer.controller.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { Body, Controller, Inject, Param, Post } from "@nestjs/common";
|
||||
import { QueryExecuterService } from "./query.executer.service";
|
||||
|
||||
@Controller("query")
|
||||
export class QueryExecuterController {
|
||||
constructor(
|
||||
@Inject(QueryExecuterService)
|
||||
private readonly queryExecuterService: QueryExecuterService
|
||||
) {}
|
||||
|
||||
@Post("/run/:token")
|
||||
async runQuery(@Param("token") token: string, @Body() query: any) {
|
||||
return this.queryExecuterService.runQuery(token, query);
|
||||
}
|
||||
}
|
||||
41
src/query/executer/query.executer.service.ts
Normal file
41
src/query/executer/query.executer.service.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Query } from "../entities/query.enitity";
|
||||
import { Repository } from "typeorm";
|
||||
import { Vm } from "src/vm/vm.class";
|
||||
import { Module } from "src/vm/module.class";
|
||||
|
||||
@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 = this.createVm(query);
|
||||
|
||||
await vm.runScript(query.source);
|
||||
|
||||
// Here you would add the logic to actually execute the query
|
||||
// against your database or data source. This is a placeholder.
|
||||
return { message: "Query executed", query: queryData };
|
||||
}
|
||||
|
||||
private createVm(query: Query) {
|
||||
return new Vm({
|
||||
memoryLimit: 5,
|
||||
modules: query.modules.map((module) => {
|
||||
return new Module(module.name, module.sourcePath);
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
10
src/query/handler/query.handler.controller.ts
Normal file
10
src/query/handler/query.handler.controller.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { Controller, Inject } from "@nestjs/common";
|
||||
import { QueryHandlerService } from "./query.handler.service";
|
||||
|
||||
@Controller("query")
|
||||
export class QueryHandlerController {
|
||||
constructor(
|
||||
@Inject(QueryHandlerService)
|
||||
private readonly queryHandlerService: QueryHandlerService
|
||||
) {}
|
||||
}
|
||||
4
src/query/handler/query.handler.service.ts
Normal file
4
src/query/handler/query.handler.service.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class QueryHandlerService {}
|
||||
8
src/query/query.module.ts
Normal file
8
src/query/query.module.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [],
|
||||
providers: [],
|
||||
})
|
||||
export class QueryModule {}
|
||||
Reference in New Issue
Block a user