title: How to close database connection in nestjs service?
body: <p>In my database module I'm creating a connection to my database. With this setup I do get the error</p>
  <p><strong>A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --runInBand --detectOpenHandles to find leaks.</strong></p>
  <p>in my e2e tests.</p>
  <p>I think I have to close the connection. But how do I run <code>client.close</code> in the <code>onModuleDestroy()</code> in my Databasemodule?</p>
  <p><strong>database.module.ts</strong></p>
  <pre><code>import { Module } from '@nestjs/common'
  import { MongoClient, Db, Logger } from 'mongodb'
  
  @Module({
      providers: [
          {
              provide: 'DATABASE_CONNECTION',
              useFactory: async (): Promise&lt;Db&gt; =&gt; {
                  const mongo = 'mongodb://localhost:27017'
                  const database = 'testing'
  
                  try {
                      Logger.setLevel('debug')
  
                      const client = await MongoClient.connect(mongo, {
                          useNewUrlParser: true,
                          useUnifiedTopology: true,
                      });
  
                      const db = client.db(database)
                      return db
                  } catch (error) {
                      throw error
                  }
              }
          },
          {
              provide: 'DATABASE_CLIENT',
              useFactory: () =&gt; true // how do I get client of the above provider?
          }
      ],
      exports: ['DATABASE_CONNECTION', 'DATABASE_CLIENT']
  })
  
  export class DatabaseModule {
      constructor(
          @Inject('DATABASE_CLIENT')
          private client: Db
      ) {}
  
      async onModuleDestroy() {
          console.log(this.client);
          // await this.client.close()
      }
  }
  </code></pre>