Search code examples
nestjs

NestJS - Export Custom Factory Provider (useFactory)


I have created a custom provider in one of my modules. I need to export that custom provider to make it available in other modules and I'm having trouble doing so. I do not know what to put in the exports array. Below is my custom provider.

    @Module({
      imports: [TypeOrmModule.forFeature([TypeOrmUser])],
      providers: [
        UserMapper,
        UserService,
        {
          provide: getRepositoryToken(TypeOrmUser),
          inject: [getDataSourceToken()],
          useFactory(dataSource: DataSource) {
            return dataSource.getRepository(TypeOrmUser).extend(userRepositoryImpl);
          },
        },
      ],
      exports: [UserService, 'what to put here to export custom user repository?'],
    })
    export class UsersModule {}

Thank you for your help.


Solution

  • To export a custom provider, all that is needed to be added to the exports array is the provider's injection token.

    In this case, that's getRepositoryToken(TypeOrmUser)