'How to workraound this TypeORM error, "EntityRepository is deprecated , use Repository.extend function instead"?
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?
typeorm version: "^0.3.0"
I'm using nest js and trying to create a custom repository.
Solution 1:[1]
First of all:
npm install @nestjs/typeorm@next
NOTE
In my project @nestjs/typeorm version is 9.0.0-next.2
and typeorm version is 0.3.6
Create a folder named database in the src of your project then create two files in (typeorm-ex.decorator.ts and typeorm-ex.module.ts)
// typeorm-ex.decorator.ts
import { SetMetadata } from "@nestjs/common";
export const TYPEORM_EX_CUSTOM_REPOSITORY = "TYPEORM_EX_CUSTOM_REPOSITORY";
export function CustomRepository(entity: Function): ClassDecorator {
return SetMetadata(TYPEORM_EX_CUSTOM_REPOSITORY, entity);
}
And next file
// typeorm-ex.module.ts
import { DynamicModule, Provider } from "@nestjs/common";
import { getDataSourceToken } from "@nestjs/typeorm";
import { DataSource } from "typeorm";
import { TYPEORM_EX_CUSTOM_REPOSITORY } from "./typeorm-ex.decorator";
export class TypeOrmExModule {
public static forCustomRepository<T extends new (...args: any[]) => any>(repositories: T[]): DynamicModule {
const providers: Provider[] = [];
for (const repository of repositories) {
const entity = Reflect.getMetadata(TYPEORM_EX_CUSTOM_REPOSITORY, repository);
if (!entity) {
continue;
}
providers.push({
inject: [getDataSourceToken()],
provide: repository,
useFactory: (dataSource: DataSource): typeof repository => {
const baseRepository = dataSource.getRepository<any>(entity);
return new repository(baseRepository.target, baseRepository.manager, baseRepository.queryRunner);
},
});
}
return {
exports: providers,
module: TypeOrmExModule,
providers,
};
}
}
Open your AppModule and modify it like the following:
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mssql',
...
entities: [Photo],
}),
TypeOrmExModule.forCustomRepository([PhotoRepository]),
...
],
controllers: [AppController],
providers: [
AppService
],
})
export class AppModule { }
You can create your customer repository like the following :
@CustomRepository(Photo)
export class PhotoRepository extends Repository<Photo> {
public async getAllPhoto() {
const query = this.createQueryBuilder('photo')
.where('photo.isPublished = :isPublished', { isPublished: true })
const photos = await query.getMany()
return photos
}
}
Everything works perfectly.
Thanks to @anchan828
Solution 2:[2]
In order to extend UserRepository functionality you can use .extend method of Repository class
Example
// user.repository.ts
export const UserRepository = dataSource.getRepository(User).extend({...})
Solution 3:[3]
As refer to the related PR, the migration is work in progress. You can checkout the latest progress by using:
yarn add @nestjs/typeorm@next
You can use custom dynamic module as a workaround, like this gist for now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Kim Jeong Ho |
Solution 3 | Tura |