feat: enhance API and query handling with Redis caching; add QueryGuard for query validation; refactor services to utilize RedisClient for improved performance

This commit is contained in:
Boris D
2025-10-10 10:51:52 +03:00
parent 45db65cec8
commit ca134414b0
20 changed files with 228 additions and 64 deletions

View File

@ -1,22 +0,0 @@
import { Controller, Inject, Post, UseGuards } from "@nestjs/common";
import { ApiTokenGuard } from "src/api/guards/api-token.guard";
import { FunctionService } from "./function.service";
@Controller("functions")
@UseGuards(ApiTokenGuard)
export class FunctionController {
constructor(
@Inject(FunctionService)
private readonly functionService: FunctionService
) {}
@Post("create")
async createFunction(projectId: string, name: string, source: string) {
return this.functionService.create(projectId, name, source);
}
@Post("delete")
async deleteFunction(projectId: string, name: string) {
return this.functionService.deleteFunction(projectId, name);
}
}

View File

@ -1,65 +0,0 @@
import { Inject, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { FunctionEntity } from "src/query/entities/function.entity";
import { In, Repository } from "typeorm";
import { ProjectService } from "../project.service";
@Injectable()
export class FunctionService {
constructor(
@InjectRepository(FunctionEntity)
private readonly functionRepository: Repository<FunctionEntity>,
@Inject(ProjectService)
private readonly projectService: ProjectService
) {}
async create(
projectId: string,
name: string,
source: string
): Promise<FunctionEntity> {
const project = await this.projectService.findById(projectId);
if (!project) {
throw new Error("Project not found");
}
const existingFunction = await this.functionRepository.findOne({
where: { name, project: { id: projectId } },
});
if (existingFunction) {
existingFunction.source = source;
return this.functionRepository.save(existingFunction);
}
const functionEntity = this.functionRepository.create({
name,
source,
project,
});
return this.functionRepository.save(functionEntity);
}
async findByNames(
projectId: string,
names: string[]
): Promise<FunctionEntity[]> {
return this.functionRepository.find({
where: { project: { id: projectId }, name: In(names) },
});
}
async deleteFunction(projectId: string, name: string): Promise<void> {
const functionEntity = await this.functionRepository.findOne({
where: { name, project: { id: projectId } },
});
if (!functionEntity) {
throw new Error("Function not found");
}
await this.functionRepository.remove(functionEntity);
}
}

View File

@ -4,9 +4,14 @@ import { Project } from "./entities/project.entity";
import { ProjectService } from "./project.service";
import { ProjectController } from "./project.controller";
import { ApiModule } from "src/api/api.module";
import { RedisModule } from "src/redis/redis.module";
@Module({
imports: [forwardRef(() => ApiModule), TypeOrmModule.forFeature([Project])],
imports: [
forwardRef(() => ApiModule),
forwardRef(() => RedisModule),
TypeOrmModule.forFeature([Project]),
],
controllers: [ProjectController],
providers: [ProjectService],
exports: [ProjectService],

View File

@ -1,13 +1,16 @@
import { Injectable } from "@nestjs/common";
import { Inject, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Project } from "./entities/project.entity";
import { RedisClient } from "src/redis/redis.service";
@Injectable()
export class ProjectService {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>
private readonly projectRepository: Repository<Project>,
@Inject(RedisClient)
private readonly redisClient: RedisClient
) {}
create(name: string) {
@ -15,17 +18,33 @@ export class ProjectService {
return this.projectRepository.save(project);
}
findById(id: string) {
return this.projectRepository.findOne({ where: { id: id } });
async findById(id: string) {
const cached = await this.redisClient.get(`project_${id}`);
if (cached) {
return cached;
}
const project = await this.projectRepository.findOne({ where: { id: id } });
if (project) {
await this.redisClient.set(`project_${id}`, project, 300);
}
return project;
}
updateDatabase(projectId: string, databaseId: string) {
async updateDatabase(projectId: string, databaseId: string) {
await this.redisClient.del(`project_${projectId}`);
return this.projectRepository.update(projectId, {
database: { id: databaseId },
});
}
updateRedisNode(projectId: string, redisNodeId: { id: string }[]) {
async updateRedisNode(projectId: string, redisNodeId: { id: string }[]) {
await this.redisClient.del(`project_${projectId}`);
return this.projectRepository.update(projectId, {
redisNodes: redisNodeId,
});