gpt4 book ai didi

NestJS - appService instantiation problems when using dynamic providers with useFactory()(将动态提供程序与useFactory()一起使用时的NestJS-appService实例化问题)

转载 作者:bug小助手 更新时间:2023-10-27 19:46:51 34 4
gpt4 key购买 nike



I have a regular NestJS application with a module that is responsible for creating test data.

我有一个常规的NestJS应用程序,其中有一个负责创建测试数据的模块。


In the AppService class I'm calling this to create data on the startup of the application like so

在AppService类中,我调用它来创建应用程序启动时的数据,如下所示


@Injectable()
export class AppService {
constructor(dataGenerationService: DataGenerationService) {
dataGenerationService.initData()
}
}

And it works just fine.

而且它运行得很好。


However, ever since I added a dynamic provider for adding multi-tenancy into my application (see below) the constructor of the AppService is not called at all anymore.

然而,自从我在应用程序中添加了用于添加多租户的动态提供程序(见下文)以来,AppService的构造函数根本不再被调用。


const tenantContextFactory = {
provide: TENANT_CONTEXT,
scope: Scope.REQUEST,
useFactory: async (request, configService: ConfigService, tenantService: TenantsService) => {
// gets tenant ID out of the JWT token (left out of code snippet here)
const tenantId = ...

if (tenantId) {
return new TenantContext(tenantId)
}
return null
},
inject: [REQUEST, ConfigService, TenantsService],
}

@Module({
imports: [TypeOrmModule.forFeature([Tenant])],
controllers: [TenantsController],
providers: [TenantsService, tenantContextFactory],
exports: [TenantsService, tenantContextFactory],
})
export class TenantsModule {}


I've tried multiple ways and none of them worked

我试过多种方法,但都不管用



  • I tried the life cycle methods described here - but non of the onModuleInit() functions is called

  • I tried the execution context of the same article but it says that the dataGenerationService is undefined, so it has an issue instantiating it

  • If I remove the dependency and just have an empty constructor list with a log, then it's called, but not with the injected service.

  • If I remove the tenantContextFactory of the tenantsModule providers and export arrays then also, it's called normally.


Does anyone know why the instantiation doesn't properly work anymore?

有人知道为什么实例化不再正常工作吗?


更多回答

Is it not called on startup or not called ever (even on request)?

它是在启动时不被调用还是永远不被调用(即使是在请求时)?

neither on startup nor on requests (the goal is on startup)

既不在启动时,也不在请求时(目标是在启动时)

What is the DataGenerationService? Does it inject the TenantsService?

什么是DataGenerationService?它会注入TenantsService吗?

it's a service in the DataGenerationModule and the module itself injects the TenantsModule and the DataGenerationService injects the TenantsService yeah

它是DataGenerationModule中的一个服务,模块本身注入TenantsModule,DataGenerationService注入TenantsService是的

And what route are you hitting when you send a request? Does it use the AppService?

当您发送请求时,您选择的是哪条路线?它是否使用AppService?

优秀答案推荐

Nest is smart about what REQUEST scoped providers are created per request, and it ensures that REQUEST scoped providers are not created without a request happening. Because the AppService relies on DataGenerationService which ends up relying on TenantsService (which is REQUEST scoped), the AppService itself is REQUEST scoped and not created at the time of startup. If you were to call a route where AppService is required, you'd see the constructor of the AppService get called, but if the requested route doesn't require that service, it won't be created in the first place.

Nest对于每个请求创建哪些请求作用域提供程序很聪明,它确保在没有请求发生的情况下不会创建请求作用域提供程序。因为AppService依赖于DataGenerationService,而DataGenerationService最终依赖于TenantsService(它是请求范围的),所以AppService本身是请求范围的,而不是在启动时创建的。如果您要调用需要AppService的路由,您将看到AppService的构造函数被调用,但如果请求的路由不需要该服务,则不会首先创建该服务。


更多回答

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com