title: How to use ConfigService in Nestjs DatabaseModule
body: <p>I have created a DatabaseModule using the <a href="https://docs.nestjs.com/recipes/sql-typeorm" rel="noreferrer">nestjs typeorm</a></p>
  <pre><code>import { createConnection } from 'typeorm';
  import { ConfigService } from '@nestjs/config';
  
  
  export const databaseConnection = [
      {
          provide: 'DATABASE_CONNECTION',
          useFactory: async (configService: ConfigService) =&gt; await createConnection({
              type: configService.get('DBTYPE'),
              host: configService.get('DBHOST'),
              port: configService.get('DBPORT'),
              username: configService.get('DBUSERNAME'),
              password: configService.get('DBPASSWORD'),
              database: configService.get('DBNAME'),
              synchronize: true,
              entities: [
                  __dirname + '/../**/*.entity.ts'
              ]
          })
      }
  ];
  </code></pre>
  <p>While starting the rest service I am getting the following error</p>
  <pre><code>Cannot read property 'get' of undefined - {&quot;stack&quot;:[&quot;TypeError: Cannot read property 'get' of undefined
  
      at InstanceWrapper.useFactory [as metatype] (../database/database.provider.js:9:33)
      at Injector.instantiateClass (../node_modules/@nestjs/core/injector/injector.js:293:55)
      at callback (../node_modules/@nestjs/core/injector/injector.js:77:41)
      at process._tickCallback (internal/process/next_tick.js:68:7)
      at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
      at startup (internal/bootstrap/node.js:283:19)
      at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)&quot;]}
  </code></pre>
  <p>I have imported the <code>ConfigModule</code> inside <code>DatabaseModule</code>.</p>
  <p>Can someone help me out with this?</p>
  <p><strong>EDIT</strong></p>
  <p>My database config</p>
  <pre><code>import { registerAs } from '@nestjs/config';
  
  // Configuration factory class for database configuration.
  const DatabaseConfig = registerAs('DBConfig', () =&gt; ({
      DBTYPE: process.env.DATABASE_TYPE,
      DBHOST: process.env.DATABASE_HOST || 'localhost',
      DBPORT: process.env.DATABASE_PORT || 5432,
      DBUSERNAME: process.env.DATABASE_USERNAME,
      DBPASSWORD: process.env.DATABASE_PASSWORD,
      DBNAME: process.env.DATABASE_NAME
  }));
  </code></pre>
  <p>In app.module.ts imports</p>
  <pre><code>ConfigModule.forRoot({
          isGlobal: true,
          expandVariables: true,
          load: [AppConfig, DatabaseConfig]
  }),
  </code></pre>