api-server/src/modules/knowledge-base/knowledge-base.repository.ts
WangDL 35de65e99b feat: 重构 api-server 为模块化单体架构,接入 MySQL + Redis
- 按 BACKEND-PLAN.md 将项目重构为 4 层架构:
  config/ -> common/ -> infrastructure/ -> modules/
- 15 个业务模块,遵循 Controller → Service → Repository 分层
- infrastructure: PrismaService / RedisService / QueueService / AiService / StorageService
- common: guards / interceptors / filters / pipes / decorators / dto / types / utils
- Prisma schema 含 27 张表,MySQL 8.0 服务器 db push 成功
- Redis 7 接入: 限流/任务状态/分布式锁/队列预留
- ai-analysis 模块: 每日 50 次限流 + 重复提交锁 + 异步任务状态追踪
- document-import 模块: 异步导入流程 + 进度追踪
- notifications 模块: BullMQ notification 队列预留
- /health 端点实时返回 database + redis 连接状态
- Swagger 注册 15 个 tag,67 个路由全部映射
2026-05-09 18:25:04 +08:00

69 lines
1.8 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { generateShortId } from '../../common/utils/id.util';
import { KnowledgeBaseStatus } from './types/knowledge-base.types';
export interface KnowledgeBase {
id: string;
userId: string;
title: string;
description: string;
status: KnowledgeBaseStatus;
itemCount: number;
lastStudiedAt: Date | null;
createdAt: Date;
updatedAt: Date;
}
@Injectable()
export class KnowledgeBaseRepository {
private items: Map<string, KnowledgeBase> = new Map();
async create(userId: string, dto: any): Promise<KnowledgeBase> {
const kb: KnowledgeBase = {
id: generateShortId(),
userId,
title: dto.title,
description: dto.description || '',
status: 'active',
itemCount: 0,
lastStudiedAt: null,
createdAt: new Date(),
updatedAt: new Date(),
};
this.items.set(kb.id, kb);
return kb;
}
async findById(id: string): Promise<KnowledgeBase | undefined> {
return this.items.get(id);
}
async findAllByUserId(userId: string): Promise<KnowledgeBase[]> {
return Array.from(this.items.values()).filter(
(kb) => kb.userId === userId && kb.status !== 'deleted',
);
}
async countByUserId(userId: string): Promise<number> {
return Array.from(this.items.values()).filter(
(kb) => kb.userId === userId && kb.status !== 'deleted',
).length;
}
async update(id: string, dto: any): Promise<KnowledgeBase | undefined> {
const kb = this.items.get(id);
if (!kb) return undefined;
Object.assign(kb, { ...dto, updatedAt: new Date() });
this.items.set(id, kb);
return kb;
}
async softDelete(id: string): Promise<boolean> {
const kb = this.items.get(id);
if (!kb) return false;
kb.status = 'deleted';
kb.updatedAt = new Date();
return true;
}
}