diff --git a/src/app/app.module.ts b/src/app/app.module.ts index bd06bc3..03d039f 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,11 +1,11 @@ import { Module } from "@nestjs/common"; -import { TestModule } from "../test/test.module"; import { ApiModule } from "../api/api.module"; import { QueryModule } from "src/query/query.module"; import { ProjectModule } from "src/project/project.module"; import { RedisModule } from "src/redis/redis.module"; import { DatabaseModule } from "src/database/database.module"; -import { DatabaseManagerModule } from "src/databaseManager/database/database.manager.module"; +import { DatabaseManagerModule } from "src/databaseManager/database.manager.module"; +import { RedisManagerModule } from "src/redisManager/redisManager.module"; @Module({ imports: [ @@ -15,7 +15,7 @@ import { DatabaseManagerModule } from "src/databaseManager/database/database.man ApiModule, ProjectModule, QueryModule, - TestModule, + RedisManagerModule, ], controllers: [], providers: [], diff --git a/src/databaseManager/database/database.manager.module.ts b/src/databaseManager/database.manager.module.ts similarity index 57% rename from src/databaseManager/database/database.manager.module.ts rename to src/databaseManager/database.manager.module.ts index 5414f6a..9546a19 100644 --- a/src/databaseManager/database/database.manager.module.ts +++ b/src/databaseManager/database.manager.module.ts @@ -1,13 +1,13 @@ import { forwardRef, Module } from "@nestjs/common"; import { TypeOrmModule } from "@nestjs/typeorm"; -import { Database } from "../entities/database.entity"; -import { MigrationModule } from "../migration/migration.module"; import { ProjectModule } from "src/project/project.module"; -import { DatabaseManagerController } from "./database.manager.controller"; -import { DatabaseManagerService } from "./database.manager.service"; -import { DatabaseNode } from "../entities/database.node.entity"; +import { MigrationModule } from "./migration/migration.module"; +import { Database } from "./entities/database.entity"; +import { DatabaseNode } from "./entities/database.node.entity"; import { Project } from "src/project/entities/project.entity"; -import { DatabaseNodeService } from "../databaseNode/database.node.service"; +import { DatabaseManagerController } from "./database/database.manager.controller"; +import { DatabaseManagerService } from "./database/database.manager.service"; +import { DatabaseNodeService } from "./databaseNode/database.node.service"; @Module({ imports: [ diff --git a/src/query/handler/query.handler.service.ts b/src/query/handler/query.handler.service.ts index 91dedeb..7f7654a 100644 --- a/src/query/handler/query.handler.service.ts +++ b/src/query/handler/query.handler.service.ts @@ -97,6 +97,25 @@ export class QueryHandlerService { 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); @@ -108,9 +127,13 @@ export class QueryHandlerService { delete queryData.projectToken; const query = this.queryRepository.create(queryData); - await this.queryRepository.save(query); - await this.createDefaults(query); + const imports = this.parseImports(query.source); + console.log("Parsed imports:", imports); + + query.source = this.clearImports(query.source); + + await this.queryRepository.save(query); return query; } diff --git a/src/redisManager/entities/redis.node.entity.ts b/src/redisManager/entities/redis.node.entity.ts new file mode 100644 index 0000000..f312e44 --- /dev/null +++ b/src/redisManager/entities/redis.node.entity.ts @@ -0,0 +1,19 @@ +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; + +@Entity("redisNode") +export class RedisNode { + @PrimaryGeneratedColumn("uuid") + id: string; + + @Column({ type: "varchar", length: 255 }) + host: string; + + @Column({ type: "int" }) + port: number; + + @Column({ type: "varchar", length: 255 }) + user: string | null; + + @Column({ type: "varchar", length: 255 }) + password: string | null; +} diff --git a/src/redisManager/redis.manager.controller.ts b/src/redisManager/redis.manager.controller.ts new file mode 100644 index 0000000..0e6d9c5 --- /dev/null +++ b/src/redisManager/redis.manager.controller.ts @@ -0,0 +1,19 @@ +import { Body, Controller, Post } from "@nestjs/common"; +import { RedisNodeService } from "./redisNode/redis.node.service"; + +@Controller("redis") +export class RedisManagerController { + constructor(private readonly redisNodeService: RedisNodeService) {} + + @Post("node/create") + addRedisNode( + @Body() body: { host: string; port: number; user: string; password: string } + ) { + return this.redisNodeService.create( + body.host, + body.port, + body.user, + body.password + ); + } +} diff --git a/src/redisManager/redisManager.module.ts b/src/redisManager/redisManager.module.ts new file mode 100644 index 0000000..5e09c24 --- /dev/null +++ b/src/redisManager/redisManager.module.ts @@ -0,0 +1,13 @@ +import { Module } from "@nestjs/common"; +import { TypeOrmModule } from "@nestjs/typeorm"; +import { RedisNode } from "./entities/redis.node.entity"; +import { RedisManagerController } from "./redis.manager.controller"; +import { RedisNodeService } from "./redisNode/redis.node.service"; + +@Module({ + imports: [TypeOrmModule.forFeature([RedisNode])], + controllers: [RedisManagerController], + providers: [RedisNodeService], + exports: [RedisNodeService], +}) +export class RedisManagerModule {} diff --git a/src/redisManager/redisNode/redis.node.service.ts b/src/redisManager/redisNode/redis.node.service.ts new file mode 100644 index 0000000..13b7fb7 --- /dev/null +++ b/src/redisManager/redisNode/redis.node.service.ts @@ -0,0 +1,57 @@ +import { Injectable } from "@nestjs/common"; +import { InjectRepository } from "@nestjs/typeorm"; +import { RedisNode } from "../entities/redis.node.entity"; +import { Repository } from "typeorm"; + +@Injectable() +export class RedisNodeService { + constructor( + @InjectRepository(RedisNode) + private readonly redisNodeRepository: Repository + ) {} + + async create( + host: string, + port: number, + user: string, + password: string + ): Promise { + const existingNode = await this.redisNodeRepository.findOne({ + where: { host, port }, + }); + + if (existingNode) { + existingNode.password = password; + existingNode.user = user; + return await this.redisNodeRepository.save(existingNode); + } + + const redisNode = this.redisNodeRepository.create({ + host, + port, + user, + password, + }); + + return this.redisNodeRepository.save(redisNode); + } + + async getConnectionOptions(id: string): Promise<{ + host: string; + port: number; + username: string; + password: string; + }> { + const node = await this.redisNodeRepository.findOne({ where: { id } }); + if (!node) { + throw new Error("Redis node not found"); + } + + return { + host: node.host, + port: node.port, + username: node.user, + password: node.password, + }; + } +} diff --git a/src/test/test.controller.ts b/src/test/test.controller.ts deleted file mode 100644 index 616324c..0000000 --- a/src/test/test.controller.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { Controller, Get } from "@nestjs/common"; -import * as ivm from "isolated-vm"; -import * as fs from "fs"; - -@Controller("test") -export class TestController { - @Get() - async test() { - const isolate = new ivm.Isolate({ memoryLimit: 128 }); - - const context = isolate.createContextSync(); - const jail = context.global; - - await jail.set("global", jail.derefInto()); - - jail.setSync("log", function (...args) { - console.log(...args); - }); - - const squelSource = fs.readFileSync( - "node_modules/squel/dist/squel.min.js", - "utf8" - ); - - await context.eval(squelSource); - - jail.setSync( - "testAsync", - new ivm.Reference(async (arg) => { - const result = await this.mockAsync(arg); - return result; - }) - ); - - jail.setSync("result", (query) => { - console.log("final", query); - }); - - const hostile = isolate.compileScriptSync(` - async function main() { - async function asyncCall(reference, arg) { - return await reference.apply(undefined, [arg], { result: { promise: true } }); - } - - const a = await asyncCall(testAsync, 'testArg'); - - const query = squel.select().from("users").where("id = ?", a); - - log(a); - log('testMe'); - return query.toString(); - }; - - (async () => { - const resultQuery = await main(); - result(resultQuery); - })(); - `); - - hostile - .run(context) - .catch((err) => console.error(err)) - .then((result) => { - console.log("Script executed successfully", result); - }); - - return "Test successful!"; - } - - mockAsync(arg: any) { - console.log(arg); - - const result = new Promise((resolve) => { - setTimeout(() => { - resolve("Mock async result"); - }, 1); - }); - - return result; - } -} diff --git a/src/test/test.module.ts b/src/test/test.module.ts deleted file mode 100644 index a26b7f9..0000000 --- a/src/test/test.module.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Module } from "@nestjs/common"; -import { TestController } from "./test.controller"; - -@Module({ - imports: [], - controllers: [TestController], - providers: [], -}) -export class TestModule {}