Currently, I am in the process of integrating a global module into my nest.js project
I have written a service as shown below:
export interface ConfigData {
DB_NAME: string;
}
@Injectable()
export class ConfigManager {
private static _instance?: ConfigManager;
public static configData:ConfigData | null;
private constructor() {
console.log(ConfigManager)
if (ConfigManager._instance)
ConfigManager._instance = this;
else{
ConfigManager._instance = new ConfigManager()
ConfigManager.configData = <ConfigData|null><unknown>ConfigManager.fetchSecretData()
}
}
private static async fetchSecretData():Promise<ConfigData|null>{
// some data from db
}
// static get instance() {
// return ConfigManager._instance ?? (ConfigManager._instance = new ConfigManager());
// //return ConfigManager._instance ?? (ConfigManager._instance = ConfigManager.fetchSecretData()) //new ConfigManager());
// }
}
configuration.module.ts
@Global()
@Module({
providers: [ConfigManager],
exports: [ConfigManager],
})
export class ConfigurationModule {}
Within app.module.ts, I included ConfigurationModule
in the imports section.
However, upon adding a private constructor to the service, it prevented me from including it in the module.ts file.
My expectation was that I would be able to access configData
from anywhere without having to import ConfigManager. Unfortunately, this is not functioning as intended...
The issue lies in the fact that ConfigManager
is inaccessible without importing it first.