import { DeepPartial, FindManyOptions, FindOneOptions, FindOptionsWhere, ObjectLiteral, Repository, } from "typeorm"; export abstract class BaseRepository { protected constructor(protected readonly repository: Repository) {} /** Find a single entity by its primary key. */ async findById( id: string, options?: Omit, "where">, ): Promise { return this.repository.findOne({ ...options, where: { id } as unknown as FindOptionsWhere, }); } /** Find many entities matching the given options. */ async findAll(options?: FindManyOptions): Promise { return this.repository.find(options); } /** Find many entities + return the total count for pagination. */ async findAndCount(options?: FindManyOptions): Promise<[T[], number]> { return this.repository.findAndCount(options); } /** Create and persist a new entity. */ async create(data: DeepPartial): Promise { const entity = this.repository.create(data); return this.repository.save(entity); } /** Patch an entity in place and return the reloaded row. */ async update(id: string, data: DeepPartial): Promise { await this.repository.update(id, data as never); return this.findById(id); } /** Soft-delete an entity by primary key (sets deleted_at). */ async softDelete(id: string): Promise { await this.repository.softDelete(id); } /** Permanently delete an entity. Avoid in domain code; prefer softDelete. */ async hardDelete(id: string): Promise { await this.repository.delete(id); } }