title: Async Await and Shareable MongoDB connection
body: <p>I am currently working on building a backend using Express and MongoDB Native Node. I have been researching attempting to find the best "best practice" for managing connections to a Mongo database and using that connection across the app.
  My current solution is working and I get desired results via tests in Postman. I have come up with this after being unable to find concrete answers on handling the connections with MongoDB 3.x (without Mongoose) and still modular. </p>
  
  <p><strong>Would someone be willing to give some feedback on my current solution?</strong></p>
  
  <p>My main concern would be that this setup would not be performant. I suspect it may not be, potentially due to opening/closing connections frequently, but I am not sure if the way I am doing it is good or bad practice. </p>
  
  <p>I created a <code>db.js</code> file to serve my connection:</p>
  
  <pre><code>const assert = require("assert");
  const MongoClient = require("mongodb").MongoClient;
  const base = process.env.PWD;
  const config = require(base + "/config");
  let db;
  let client;
  
  
  const connect = async () =&gt; {
    const url = config.url
    const dbName = config.dbName
    client = await MongoClient.connect(url)
    db = await client.db(dbName)
    return db
  }
  
  const disconnect = () =&gt; {
    client.close()
  }
  
  module.exports = {
    connect: connect,
    disconnect: disconnect
    }
  </code></pre>
  
  <p>I then set the routes for my 'todos' in <code>index.js</code> inside of my todos folder. Following a best practice <a href="https://github.com/i0natan/nodebestpractices/blob/master/sections/projectstructre/breakintcomponents.md" rel="nofollow noreferrer">suggestion</a> to have all component files in their own folders(open to feedback on folder structure):</p>
  
  <pre><code>const express = require('express'),
  base = process.env.PWD,
  router = express.Router(),
  todos = require(base + '/todos/todosController')
  
  /* GET All Todos */
  router.get('/all', todos.getTodos)
  /* GET One Todo */
  router.get('/todo/:id', todos.getTodo)
  /* POST One Todo */
  router.post('/todo/:id', todos.addTodo)
  /* DELETE One Todo */
  router.delete('/todo/:id', todos.deleteTodo)
  
  module.exports = router;
  </code></pre>
  
  <p>Lastly the actual <code>todosController.js</code> which requires <code>db.js</code>
  This is where I suspect some improvement could happen but I am just not sure. I connect within the routes via async function and await the connection and assign it to <code>db</code> I do my CRUD queries (all currently working properly) and then disconnect at the end.
  If this is considered performant and a good practice I am happy with that answer but if there is a way to do this better with current driver and syntax I would be happy for any feedback.</p>
  
  <pre><code>'use strict';
  
  const base = process.env.PWD,
        client = require(base + '/db.js'),
        assert = require('assert')
  
  let db
  
  const getTodos = async (req, res) =&gt; {
    db = await client.connect()
    const collection = await db.collection('documents')
    // Find all todos
    collection.find({}).toArray((err, todos) =&gt; {
      assert.equal(err, null)
      res.status(200).json(todos)
    })
    client.disconnect()
  }
  </code></pre>