feat: add Redis Manager module with controller, service, and entity; refactor app module imports
This commit is contained in:
@ -1,11 +1,11 @@
|
|||||||
import { Module } from "@nestjs/common";
|
import { Module } from "@nestjs/common";
|
||||||
import { TestModule } from "../test/test.module";
|
|
||||||
import { ApiModule } from "../api/api.module";
|
import { ApiModule } from "../api/api.module";
|
||||||
import { QueryModule } from "src/query/query.module";
|
import { QueryModule } from "src/query/query.module";
|
||||||
import { ProjectModule } from "src/project/project.module";
|
import { ProjectModule } from "src/project/project.module";
|
||||||
import { RedisModule } from "src/redis/redis.module";
|
import { RedisModule } from "src/redis/redis.module";
|
||||||
import { DatabaseModule } from "src/database/database.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({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -15,7 +15,7 @@ import { DatabaseManagerModule } from "src/databaseManager/database/database.man
|
|||||||
ApiModule,
|
ApiModule,
|
||||||
ProjectModule,
|
ProjectModule,
|
||||||
QueryModule,
|
QueryModule,
|
||||||
TestModule,
|
RedisManagerModule,
|
||||||
],
|
],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
providers: [],
|
providers: [],
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
import { forwardRef, Module } from "@nestjs/common";
|
import { forwardRef, Module } from "@nestjs/common";
|
||||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
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 { ProjectModule } from "src/project/project.module";
|
||||||
import { DatabaseManagerController } from "./database.manager.controller";
|
import { MigrationModule } from "./migration/migration.module";
|
||||||
import { DatabaseManagerService } from "./database.manager.service";
|
import { Database } from "./entities/database.entity";
|
||||||
import { DatabaseNode } from "../entities/database.node.entity";
|
import { DatabaseNode } from "./entities/database.node.entity";
|
||||||
import { Project } from "src/project/entities/project.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({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -97,6 +97,25 @@ export class QueryHandlerService {
|
|||||||
await this.queryRepository.save(query);
|
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 }) {
|
async createQuery(queryData: { projectToken: string; source: string }) {
|
||||||
const project = await this.projectService.findById(queryData.projectToken);
|
const project = await this.projectService.findById(queryData.projectToken);
|
||||||
|
|
||||||
@ -108,9 +127,13 @@ export class QueryHandlerService {
|
|||||||
delete queryData.projectToken;
|
delete queryData.projectToken;
|
||||||
|
|
||||||
const query = this.queryRepository.create(queryData);
|
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;
|
return query;
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/redisManager/entities/redis.node.entity.ts
Normal file
19
src/redisManager/entities/redis.node.entity.ts
Normal file
@ -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;
|
||||||
|
}
|
||||||
19
src/redisManager/redis.manager.controller.ts
Normal file
19
src/redisManager/redis.manager.controller.ts
Normal file
@ -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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/redisManager/redisManager.module.ts
Normal file
13
src/redisManager/redisManager.module.ts
Normal file
@ -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 {}
|
||||||
57
src/redisManager/redisNode/redis.node.service.ts
Normal file
57
src/redisManager/redisNode/redis.node.service.ts
Normal file
@ -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<RedisNode>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async create(
|
||||||
|
host: string,
|
||||||
|
port: number,
|
||||||
|
user: string,
|
||||||
|
password: string
|
||||||
|
): Promise<RedisNode> {
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
import { Module } from "@nestjs/common";
|
|
||||||
import { TestController } from "./test.controller";
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [],
|
|
||||||
controllers: [TestController],
|
|
||||||
providers: [],
|
|
||||||
})
|
|
||||||
export class TestModule {}
|
|
||||||
Reference in New Issue
Block a user