Hantsy
Hantsy

Reputation: 9261

How to expose TypeORM Repository to other modules in NestJS

I moved all database related operations into a standalone module:

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

But in other modules, when I imported the DatabaseModule and tried to inject PostRepository in service class, I got the following error.

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.

Upvotes: 1

Views: 4438

Answers (1)

Hantsy
Hantsy

Reputation: 9261

Resolved it myself after reading some of my Angular codes, just need to export the TypeOrmModule in the DatabaseModule.

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

Upvotes: 11

Related Questions