import { Repository, EntityTarget, ObjectLiteral } from 'typeorm'; import { getAppDataSource } from '../connection.js'; /** * Base repository class with common CRUD operations */ export class BaseRepository { protected readonly repository: Repository; constructor(entityClass: EntityTarget) { this.repository = getAppDataSource().getRepository(entityClass); } /** * Get repository access */ getRepository(): Repository { return this.repository; } /** * Find all entities */ async findAll(): Promise { return this.repository.find(); } /** * Find entity by ID * @param id Entity ID */ async findById(id: string | number): Promise { return this.repository.findOneBy({ id } as any); } /** * Save or update an entity * @param entity Entity to save */ async save(entity: Partial): Promise { return this.repository.save(entity as any); } /** * Save multiple entities * @param entities Array of entities to save */ async saveMany(entities: Partial[]): Promise { return this.repository.save(entities as any[]); } /** * Delete an entity by ID * @param id Entity ID */ async delete(id: string | number): Promise { const result = await this.repository.delete(id); return result.affected !== null && result.affected !== undefined && result.affected > 0; } /** * Count total entities */ async count(): Promise { return this.repository.count(); } } export default BaseRepository;