#Custom Env Handler

17 messages · Page 1 of 1 (latest)

glad crow
#

Hello! I am trying to implement some configuration files of the env variables in a custom way but it generates some conflicts. I would appreciate a hand.

What I did is the following:

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: environments[process.env.NODE_ENV],
      load: [EnvConfig],
    }),

    // Here some modules...
  ],
  providers: [],
})
export class AppModule {}

I have a configuration file like this:
import { registerAs } from '@nestjs/config';

export default registerAs('EnvConfig', () => {
  return {
    API: {
      PORT: +process.env.PORT,
      TIMEOUT_REQUEST: +process.env.TIMEOUT_REQUEST,
    },
    DATABASE: {
      POSTGRESQL_HOST: process.env.POSTGRESQL_HOST,
      POSTGRESQL_NAME: process.env.POSTGRESQL_NAME,
      POSTGRESQL_PASSWORD: process.env.POSTGRESQL_PASSWORD,
      POSTGRESQL_PORT: process.env.POSTGRESQL_PORT,
      POSTGRESQL_USERNAME: process.env.POSTGRESQL_USERNAME,
      POSTGRESQL_SSL: process.env.POSTGRESQL_SSL,
    },
  };
});

My idea is to be able to access environment variables in this way, as an object. However, how can I implement this in services or providers? I tried to do it for a custom typeorm provider but it generates an error (I will leave the error at the end, first I will show you the database provider).

database.module.ts

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [
        ConfigModule.forRoot({
          load: [EnvConfig],
        }),
      ],
      useFactory: createDatabaseOptions,
    }),
  ],
  exports: [TypeOrmModule],
})
export class DatabaseModule {}

database.provider.ts
The database provider is attached in the image
The error is in an attached picture too

#

Important fact, if I leave the DatabaseModule like this:

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      inject: [EnvConfig],
      useFactory: createDatabaseOptions,
    }),
  ],
  exports: [TypeOrmModule],
})
export class DatabaseModule {}

The error changes and looks like this:
(I attach a txt with the log)

#

I am really interested in making a good code to manage env files, I would appreciate the support of the experts!

glad crow
#

@spare mortar please help

spare mortar
spare mortar
#

If you inject: [EnvConfig] and you don't have the ConfigModule sett as global: true, then you need to add imports: [ConfigModule] to the TypeOrmModule.forRootAsync()

glad crow
#

First, sorry for tagging you. Second, thanks for the support. Finally, I do have the configModule with its isGlobal property set to true. What other aspect do you recommend me to check?

spare mortar
#

I beleive in the injects it should be EnvConfig.KEY.

#

Ah, no, I'm thinking of the namesapces

#

Oh, wait, you are using namespaces aren't you, with the registerAs

#

So you either would need to do configService.get('EnvConfig.DATABASE.<PROPERTY>') or inject: [EnvConfig.KEY] so you can do env.DATABASE.<PROPERTY>

#

That was about the JwtModule, not the TypeOrmModule. Have you updated everywhere you used the EnvConfig variable to beEnvConfig.KEY?

#

Yes, I saw it quickly enough before you deleted it to gett a sense of what the error said

glad crow
#

Haha, you're a crack, man! I had forgotten the ".KEY" it was my first time trying to make a custom in the env, thank you very much!

#

How do I terminate this issue, with which command? 😅

dry pagodaBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution if you are having the same issue, and then re-open the post if you are still having trouble, providing as much extra information as possible.