Skip to content

Instantly share code, notes, and snippets.

@mostafa8026
Forked from anchan828/README.md
Created May 24, 2023 18:02
Show Gist options
  • Save mostafa8026/dc98e38c9d1520fda62c2977b09e2fb4 to your computer and use it in GitHub Desktop.
Save mostafa8026/dc98e38c9d1520fda62c2977b09e2fb4 to your computer and use it in GitHub Desktop.
This is an improvement to allow @nestjs/[email protected] to handle CustomRepository. I won't explain it specifically, but it will help in some way. https://github.com/nestjs/typeorm/pull/1233

You need to provide some classes and decorators yourself to maintain the same style as [email protected].

1. EntityRepository -> CustomRepository

@EntityRepository(UserEntity)
export class UserRepository extends Repository<UserEntity> {}

@CustomRepository(UserEntity)
export class UserRepository extends Repository<UserEntity> {}

2. forFeature -> forCustomRepository

@Module({
  exports: [UserService],
  imports: [TypeOrmModule.forFeature([UserRepository])],
  providers: [UserService],
})
export class UserModule {}

@Module({
  exports: [UserService],
  imports: [TypeOrmExModule.forCustomRepository([UserRepository])],
  providers: [UserService],
})
export class UserModule {}
@Injectable()
export class DatabaseOptions implements TypeOrmOptionsFactory {
public createTypeOrmOptions(): TypeOrmModuleOptions {
return {
...
entities: [UserEntity],
};
}
}
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);
}
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,
};
}
}
import { Module } from "@nestjs/common";
import { TypeOrmExModule } from "../database/typeorm-ex.module";
import { UserRepository } from "./user.repository";
import { UserService } from "./user.service";
@Module({
exports: [UserService],
imports: [TypeOrmExModule.forCustomRepository([UserRepository])],
providers: [UserService],
})
export class UserModule {}
import { Repository } from "typeorm";
import { CustomRepository } from "../database/typeorm-ex.decorator";
import { UserEntity } from "./user.entity";
@CustomRepository(UserEntity)
export class UserRepository extends Repository<UserEntity> {}
import { Injectable } from "@nestjs/common";
import { UserRepository } from "./user.repository";
@Injectable()
export class UserService {
constructor(private readonly repository: UserRepository) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment