import { injectable , unmanaged } from "inversify" ; import { BaseEntity , EntityTarget , getConnection , getRepository , Repository } from "typeorm" ; import { IBaseRepository } from "./i.base.repository" ; @ injectable ( ) export class BaseRepository < T extends BaseEntity > implements IBaseRepository < T > { protected baseRepository : Repository < T > ; constructor ( @ unmanaged ( ) repository ) { this . baseRepository = repository ; } async create ( data ) { await this . baseRepository . save ( data ) ; } async update ( id , data ) { const existEntity = await this . getById ( id ) ; if ( ! existEntity ) { throw new Error ( "Cannot find entity" ) ; } this . baseRepository . update ( id , data ) ; } async getAll ( filter ) { return this . baseRepository . find ( { ... filter } ) ; } async getById ( id ) { return this . baseRepository . findOne ( id ) ; } async delete ( id ) { const existEntity = await this . getById ( id ) ; if ( ! existEntity ) { throw new Error ( "Cannot find entity" ) ; } this . baseRepository . delete ( id ) ; } }