如何将 TypeORM 存储库公开给 NestJS 中的其他模块

Han*_*tsy 1 typeorm nestjs

我将所有与数据库相关的操作移动到一个独立的模块中:

@Module({
  imports: [
    TypeOrmModule.forFeature([
      PostRepository,
      UserRepository,
      CommentRepository,
    ]),
  ],
  exports: [PostRepository, UserRepository, CommentRepository],
  providers: [PostsDataInitializer],
})
export class DatabaseModule {}
Run Code Online (Sandbox Code Playgroud)

但是在其他模块中,当我导入DatabaseModule并尝试注入PostRepository服务类时,出现以下错误。

Nest cannot export a provider/module that is not a part of the currently processed module (DatabaseModule). 
Please verify whether the exported PostRepository is available in this particular context.
Run Code Online (Sandbox Code Playgroud)

Han*_*tsy 8

在阅读了我的一些 Angular 代码后,我自己解决了这个问题,只需TypeOrmModuleDatabaseModule.

@Module({
  imports: [
    TypeOrmModule.forFeature([
      PostRepository,
      UserRepository,
      CommentRepository,
    ]),
  ],
  exports: [TypeOrmModule],
  providers: [PostsDataInitializer],
})
export class DatabaseModule {}
Run Code Online (Sandbox Code Playgroud)