How to use ConfigService inside TypeOrmModule.forFeature?

Viewed 28

I want to use NestJS ConfigService inside my TypeOrmModule.forFeature to retrieve some vars inside the .env file. How can I do it? When creating a connection I use the forRootAsync but I can't find anything like it for configurations

My Module:

@Module({
  imports: [
    TypeOrmModule.forFeature([User, Faker]),
    TypeOrmModule.forFeature([User], ConnectionName.Secondary), <== use ConfigService here
    RedisModule,
  ],
  controllers: [UsersController],
  providers: [UsersService],
  exports: [UsersService],
})
export class UsersModule {}
1 Answers

There is no forFeatureAsync, and honestly you probably don't want there to be. When you use forFeature() with a connection name, that means you need to use @InjectRepository() with a connection name as well, and as there's no way to inject service into a decorator, you'd end up hitting another wall here. Along with that, the name that you pass to forFeature() should be defined away from the config service. You can still pass a name to TypeORM from the config service in the async options, but there should also be a name property on the same level as useFactory in the forRootAsync() options

Related