【问题标题】:TypeORM: Create BaseRepository classTypeORM:创建 BaseRepository 类
【发布时间】:2020-03-20 11:13:24
【问题描述】:

如何创建扩展 TypeORM 的 Repository

baseRepository
    import { Repository } from 'typeorm';

    export abstract class BaseRepo extends Repository<T> {

        public getAll () { ... }

        public getOneById (id: number) { ... }

        public deleteById (id: number) { ... }

    }

然后继承这些方法,如

    @EntityRepository(User)
    export class UserRepo extends BaseRepo<User> {

        constructor (baseRepo: BaseRepo) {
            super();
            this.__baseRepo = baseRepo;
        }

        public getOne (id: number) {
            return __baseRepo.getOneById(id);
        }
    }

【问题讨论】:

    标签: typescript typeorm


    【解决方案1】:

    此解决方案在没有 constructor 的情况下有效

    import { Repository } from "typeorm";
    
    export class BaseRepo<T> extends Repository<T> {
    
       getOneById(id: string) { ... }
    
    }
    

    并像使用它

    import { EntityRepository } from "typeorm";
    import { BaseRepo } from "../shared/BaseRepo";
    import { User } from "../entities/User";
    
    @EntityRepository(User)
    export class MyEntityRepository extends BaseRepo<User> { }
    

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 1970-01-01
      • 2018-04-01
      • 2018-11-12
      • 1970-01-01
      • 2019-11-13
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多