{"id":514,"hash":"662d7b0156ec5a91cbb745089fd8228c98f9573abfecd3ac43c8c20f161015ac","pattern":"How to workraound this TypeORM error, &quot;EntityRepository is deprecated , use Repository.extend function instead&quot;?","full_message":"However, I can't find any Repository.extend method in Repository class and there's nothing about it in the documentation. How to solve this?\n\ntypeorm version: \"^0.3.0\"\n\nI'm using nest js and trying to create a custom repository.","ecosystem":"npm","package_name":"nestjs","package_version":null,"solution":"First of all:\n\nnpm install @nestjs/typeorm@next\n\nNOTE\n\nIn my project @nestjs/typeorm version is 9.0.0-next.2 and typeorm version is 0.3.6\n\nCreate a folder named database in the src of your project then create two files in (typeorm-ex.decorator.ts and typeorm-ex.module.ts)\n\n// typeorm-ex.decorator.ts\n\nimport { SetMetadata } from \"@nestjs/common\";\n\nexport const TYPEORM_EX_CUSTOM_REPOSITORY = \"TYPEORM_EX_CUSTOM_REPOSITORY\";\n\nexport function CustomRepository(entity: Function): ClassDecorator {\n  return SetMetadata(TYPEORM_EX_CUSTOM_REPOSITORY, entity);\n}\n\nAnd next file\n\n// typeorm-ex.module.ts\n\nimport { DynamicModule, Provider } from \"@nestjs/common\";\nimport { getDataSourceToken } from \"@nestjs/typeorm\";\nimport { DataSource } from \"typeorm\";\nimport { TYPEORM_EX_CUSTOM_REPOSITORY } from \"./typeorm-ex.decorator\";\n\nexport class TypeOrmExModule {\n  public static forCustomRepository<T extends new (...args: any[]) => any>(repositories: T[]): DynamicModule {\n    const providers: Provider[] = [];\n\n    for (const repository of repositories) {\n      const entity = Reflect.getMetadata(TYPEORM_EX_CUSTOM_REPOSITORY, repository);\n\n      if (!entity) {\n        continue;\n      }\n\n      providers.push({\n        inject: [getDataSourceToken()],\n        provide: repository,\n        useFactory: (dataSource: DataSource): typeof repository => {\n          const baseRepository = dataSource.getRepository<any>(entity);\n          return new repository(baseRepository.target, baseRepository.manager, baseRepository.queryRunner);\n        },\n      });\n    }\n\n    return {\n      exports: providers,\n      module: TypeOrmExModule,\n      providers,\n    };\n  }\n}\n\nOpen your AppModule and modify it like the following:\n\n@Module({\n  imports: [\n    TypeOrmModule.forRoot({\n      type: 'mssql',\n      ...\n      entities: [Photo],\n    }),\n    TypeOrmExModule.forCustomRepository([PhotoRepository]),\n    ...\n  ],\n  controllers: [AppController],\n  providers: [\n    AppService\n  ],\n})\nexport class AppModule { }\n\nYou can create your customer repository like the following :\n\n@CustomRepository(Photo)\nexport class PhotoRepository extends Repository<Photo> {\n    public async getAllPhoto() {\n        const query = this.createQueryBuilder('photo')\n            .where('photo.isPublished = :isPublished', { isPublished: true })\n        const photos = await query.getMany()\n        return photos\n    }\n}\n\nEverything works perfectly.\n\nThanks to @anchan828","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/71557301/how-to-workraound-this-typeorm-error-entityrepository-is-deprecated-use-repo","votes":28,"created_at":"2026-04-19T04:51:16.280971+00:00","updated_at":"2026-04-19T04:51:16.280971+00:00"}