Skip to content

Commit

Permalink
fix(): cache only process.env values
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 20, 2020
1 parent 0646770 commit 7814b42
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/config.module.ts
Expand Up @@ -125,7 +125,7 @@ export class ConfigModule {
* Registers configuration object (partial registration).
* @param config
*/
static forFeature(config: ConfigFactory) {
static forFeature(config: ConfigFactory): DynamicModule {
const configProvider = createConfigProvider(
config as ConfigFactory & ConfigFactoryKeyHost,
);
Expand Down
35 changes: 18 additions & 17 deletions lib/config.service.ts
Expand Up @@ -19,7 +19,7 @@ export class ConfigService<K = Record<string, any>> {
this._isCacheEnabled = value;
}

private readonly cache: K = {} as any;
private readonly cache: Partial<K> = {} as any;
private _isCacheEnabled = false;

constructor(
Expand Down Expand Up @@ -52,20 +52,12 @@ export class ConfigService<K = Record<string, any>> {
* @param defaultValue
*/
get<T = any>(propertyPath: keyof K, defaultValue?: T): T | undefined {
if (
this.isCacheEnabled &&
has(this.cache as Record<any, any>, propertyPath)
) {
const cachedValue = this.getFromCache(propertyPath, defaultValue);
return !isUndefined(cachedValue) ? cachedValue : defaultValue;
}

const validatedEnvValue = this.getFromValidatedEnv(propertyPath);
if (!isUndefined(validatedEnvValue)) {
return validatedEnvValue;
}

const processEnvValue = this.getFromProcessEnv(propertyPath);
const processEnvValue = this.getFromProcessEnv(propertyPath, defaultValue);
if (!isUndefined(processEnvValue)) {
return processEnvValue;
}
Expand Down Expand Up @@ -93,26 +85,35 @@ export class ConfigService<K = Record<string, any>> {
this.internalConfig[VALIDATED_ENV_PROPNAME],
propertyPath,
);
this.setInCache(propertyPath, validatedEnvValue);

return (validatedEnvValue as unknown) as T;
}

private getFromProcessEnv<T = any>(propertyPath: keyof K): T | undefined {
private getFromProcessEnv<T = any>(
propertyPath: keyof K,
defaultValue: any,
): T | undefined {
if (
this.isCacheEnabled &&
has(this.cache as Record<any, any>, propertyPath)
) {
const cachedValue = this.getFromCache(propertyPath, defaultValue);
return !isUndefined(cachedValue) ? cachedValue : defaultValue;
}
const processValue = get(process.env, propertyPath);
this.setInCache(propertyPath, processValue);
this.setInCacheIfDefined(propertyPath, processValue);

return (processValue as unknown) as T;
}

private getFromInternalConfig<T = any>(propertyPath: keyof K): T | undefined {
const internalValue = get(this.internalConfig, propertyPath);
this.setInCache(propertyPath, internalValue);

return internalValue;
}

private setInCache(propertyPath: keyof K, value: any): void {
private setInCacheIfDefined(propertyPath: keyof K, value: any): void {
if (typeof value === 'undefined') {
return;
}
set(this.cache as Record<any, any>, propertyPath, value);
}
}
1 change: 1 addition & 0 deletions tests/src/app.module.ts
Expand Up @@ -23,6 +23,7 @@ export class AppModule {
ConfigModule.forRoot({
cache: true,
envFilePath: join(__dirname, '.env'),
load: [databaseConfig],
}),
],
};
Expand Down

0 comments on commit 7814b42

Please sign in to comment.