Q: Nestjs service class not being created due to factory provider I have the below module and service for my app tenants. When I try to inject the TENANT factory provider, the TenantService doesent get instantiated. I have tried using it without forwardRef but then the app doesent start and it hangs somehwere in the middle.
TenantModule
import { HttpModule } from '@nestjs/axios';
import { Global, Logger, Module, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TenantController } from './tenant.controller';
import { Tenant } from './tenant.entity';
import { TenantService } from './tenant.service';

const tenantFactoryFromRequest = {
  provide: 'TENANT',
  scope: Scope.REQUEST,
  durable: true,
  inject: [REQUEST, TenantService],
  useFactory: (req, tenantService: TenantService) => {
    const tenant: Promise<Tenant> = tenantService.getTenantByName(
      req.headers.host.split(".")[0]
    );
    Logger.log(req.headers.host.split(".")[0])
    return tenant;
  }
}

@Global()
@Module({
  controllers: [TenantController],
  providers: [TenantService, tenantFactoryFromRequest],
  imports: [HttpModule, TypeOrmModule.forFeature([Tenant])],
  exports: ["TENANT", TypeOrmModule]
})
export class TenantModule {}


TenantService
@Injectable()
export class TenantService {
    private tenant: Tenant;
    private tenants: Object = {};

    constructor(
        @InjectRepository(Tenant) private tenantRepository: Repository<Tenant>,
        @Inject(forwardRef(() => "TENANT")) tenant: Tenant
    ) {
        this.initTenants();
        Logger.log('constructor')

        if (this.tenants[tenant.companyName]) {
            this.tenant = this.tenants[tenant.companyName];
        } else {
            Logger.error('Unknown tenant.');
        } 
    }

    private async initTenants(): Promise<void> {
        try {
            const tenants: Tenant[] = await this.tenantRepository.find();

            for (const tenant of tenants) {
                this.tenants[tenant.companyName] = tenant;
            }
            console.log(this.tenants)
        } catch (error) {
            console.log(error)
        }
    }

    getAll(): Promise<Tenant[]> {
        return this.tenantRepository.find();
    }

    createOne(tenant: Tenant): Promise<Tenant> {
        return this.tenantRepository.save(tenant);
    }

    getTenantByName(companyName: string): Promise<Tenant> {
        // console.log(this.tenantRepository)
        if (this.tenants[companyName]) {
            return this.tenants[companyName]
        } else if (!this.tenants[companyName]) {
            const tenant: Promise<Tenant> = this.tenantRepository.findOneBy({
                companyName
            });
            this.tenants[companyName] = tenant;
            return tenant;
        } else {
            return null;
        }
    }

    findById(id: string): Promise<Tenant> {
        return this.tenantRepository.findOneBy({
            id
        });
    }

    async setCurrentTenantOnRepository<T>(repository: Repository<T>): Promise<void> {
        repository.query(
            `SET LOCAL hermestms.current_tenant=${this.tenant.id};`,
            []
        );
    }

    public async setCurrentTenantOnQueryRunner(queryRunner: QueryRunner): Promise<void> {
        queryRunner.query(`SET LOCAL hermestms.current_tenant=${this.tenant.id}`);
    } 
}

The below is the ouput with the forwardRef
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [NestFactory] Starting Nest application...
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +62ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [InstanceLoader] HttpModule dependencies initialized +1ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +135ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [InstanceLoader] TenantModule dependencies initialized +1ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [InstanceLoader] UserModule dependencies initialized +1ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [RoutesResolver] AppController {/hello}: +6ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [RouterExplorer] Mapped {/hello, GET} route +2ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [RoutesResolver] TenantController {/tenants}: +1ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [RouterExplorer] Mapped {/tenants, GET} route +1ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [RouterExplorer] Mapped {/tenants, POST} route +0ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [RoutesResolver] UserController {/users}: +1ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [RouterExplorer] Mapped {/users, GET} route +0ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [RouterExplorer] Mapped {/users, POST} route +1ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG [NestApplication] Nest application successfully started +2ms
[Nest] 26234  - 11/14/2022, 9:12:45 PM     LOG Running on port 3000

As you can see the log calls in the service class do not appear, if I try to send any request to the controller using this service any of the properties in the service will be undefined
And the below is the output without the forwardRef
[Nest] 26373  - 11/14/2022, 9:18:21 PM     LOG [NestFactory] Starting Nest application...
[Nest] 26373  - 11/14/2022, 9:18:21 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +51ms
[Nest] 26373  - 11/14/2022, 9:18:21 PM     LOG [InstanceLoader] HttpModule dependencies initialized +0ms
[Nest] 26373  - 11/14/2022, 9:18:21 PM     LOG [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 26373  - 11/14/2022, 9:18:21 PM     LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +118ms
[Nest] 26373  - 11/14/2022, 9:18:21 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 26373  - 11/14/2022, 9:18:21 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms

It just hangs like this forever.
What am i getting wrong here? Should I use a class provider and not a factory one?
