I have this Module.
It allows the creation of a Databaseconnection. And takes a provider factory to create a options object (I think this is needed to use ConfigModule, at least I did not find any other examples on how to achieve this)
I need to close the database connection once the Module is unloaded.
Sidequestion, is it guaranteed that the factory to create a driver will only be called once ? Or is it possible that the IoC Container discards the cached instance of that provider at some point and creates a new one if a new one is requested ?
export class Neo4jConnectionModule implements OnModuleDestroy {
protected static driver: Driver
static async forRootAsync({
useFactory,
inject,
imports,
}: {
useFactory: (
...args: any[]
) => Neo4jConnectionModuleOptions | Promise<Neo4jConnectionModuleOptions>
inject: Array<InjectionToken | OptionalFactoryDependency>
imports: Array<any>
}): Promise<DynamicModule> {
return {
module: Neo4jConnectionModule,
imports,
providers: [
{
provide: NEO4J_SESSION_STATE,
useClass: Neo4jSessionState,
scope: Scope.TRANSIENT,
},
{
provide: NEO4J_OPTIONS,
useFactory,
inject,
},
{
provide: NEO4J_DRIVER, // Provides NEO4J_DRIVER
useFactory: async (options: Neo4jConnectionModuleOptions) => {
return await driverFactory(options)
},
inject: [NEO4J_OPTIONS],
},
],
exports: [NEO4J_OPTIONS, NEO4J_DRIVER],
}
}
constructor(@Inject(NEO4J_DRIVER) private readonly driver: Driver) {}. // Can't find NEO4J_DRIVER
async onModuleDestroy() {
await this.driver.close()
Neo4jConnectionLogger.log('Disconnected')
}
}
But Nest can't resolve the NEO4J_DRIVER in the constructor
I guess I am missing something obvious ^^