简体   繁体   English

在 nestjs 中导入并注入 typeorm 存储库

[英]import and inject a typeorm repository in nestjs

This one is driving me nuts!这个快把我逼疯了!

I have a nestjs project using typeorm , with the following simplified structure:我有一个使用typeormnestjs项目,具有以下简化结构:

+ src
   + dal
        + entities
             login.entity.ts
             password.entity.ts             
        + repositories
             login.repository.ts
             password.repository.ts
        dal.module.ts
   + modules
        + security
             + services
                  login.service.ts
                  security.service.ts
             security.module.ts
   app.module.ts
   main.ts

dal defines the following custom repositories: dal 定义了以下自定义存储库:

@EntityRepository(Login)
export class LoginRepository extends AbstractRepository<Login> implements ILoginRepository { }

@EntityRepository(Password)
export class PasswordRepository extends AbstractRepository<Password> implements IPasswordRepository { }

dal.module.ts: dal.module.ts:

@Module({
    imports: [TypeOrmModule.forFeature([Entity1, Entity2, Entity3])],
    controllers: [],
    providers: [PasswordRepository, LoginRepository],
})
export class DalModule { }

security.module.ts:安全模块.ts:

@Module({
    imports: [TypeOrmModule.forFeature([PasswordRepository, LoginRepository])],
    controllers: [SecurityController],
    providers: [LoginService, SecurityService],
})
export class SecurityModule { }

app.module.ts: app.module.ts:

@Module({
   imports: [
      DalModule,
      SecurityModule,
      TypeOrmModule.forRoot({
         type: 'mysql',
         port: Number(process.env.DB_PORT),
         host: process.env.DB_SERVER,
         username: process.env.DB_USERNAME,
         password: process.env.DB_PASSWORD,
         database: process.env.DB_NAME,
         entities: [__dirname + '/**/*.entity{.ts,.js}'],
         synchronize: false
      })
   ]
})
export class AppModule {}

login.service.ts:登录.service.ts:

export class LoginService {
   constructor(
      private readonly passwordRepository: PasswordRepository,
      @InjectRepository(Login) private readonly loginRepository: LoginRepository
   ) {
       console.log(this.passwordRepository.f1);
       console.log(this.loginRepository.f2);
   }

Here's the thing:事情是这样的:

The way it is now I will get [AsyncFunction: f1] and [AsyncFunction: f2] logged as expected in the LoginService constructor.现在的方式我将在 LoginService 构造函数中按预期记录[AsyncFunction: f1][AsyncFunction: f2]

However, if I remove @InjectRepository(Login) from the second argument, I get a Cannot read property 'f1' of undefined from the first console.log.但是,如果我从第二个参数中删除@InjectRepository(Login) ,我会从第一个 console.log 中得到一个Cannot read property 'f1' of undefined If I then comment out the first console.log, I get a Cannot read property 'f2' of undefined from the second console.log.如果我随后注释掉第一个console.log,我会从第二个console.log 中得到一个Cannot read property 'f2' of undefined

On the other hand, if I add @InjectRepository(Password) to the first argument, I get a Nest can't resolve dependencies of the LoginService (?, LoginRepository). Please make sure that the argument PasswordRepository at index [0] is available in the SecurityModule context另一方面,如果我将@InjectRepository(Password)添加到第一个参数,我会得到一个Nest can't resolve dependencies of the LoginService (?, LoginRepository). Please make sure that the argument PasswordRepository at index [0] is available in the SecurityModule context Nest can't resolve dependencies of the LoginService (?, LoginRepository). Please make sure that the argument PasswordRepository at index [0] is available in the SecurityModule context error. Nest can't resolve dependencies of the LoginService (?, LoginRepository). Please make sure that the argument PasswordRepository at index [0] is available in the SecurityModule context

why on earth is this happening?为什么会发生这种情况?

I encountered the same problem, but I resolved it finally.我遇到了同样的问题,但我终于解决了。

For your case, just declare Repository classes as part of TypeOrmModule , and exports TypeOrmModule .对于您的情况,只需将Repository类声明为TypeOrmModule的一部分,然后导出TypeOrmModule

@Module({
    imports: [TypeOrmModule.forFeature([PasswordRepository, LoginRepository])],
    exports: [TypeOrmModule],
    controllers: [SecurityController],
    providers: [LoginService, SecurityService],
})
export class SecurityModule { }

When importing SecuirtyModule in other modules, the LoginRepository will be available for injection.在其他模块中导入SecuirtyModule时, LoginRepository将可用于注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM