title: Should I open/close database connection after every query/insert?
body: <p>Hey everyone i'm developer  a simple app in last days using nodejs and create this function to return client instance from mongodb</p>
  <pre><code>const mongodb = require(&quot;mongodb&quot;);
  const { db } = require(&quot;../config/env&quot;);
  
  const conection = async () =&gt; {
      try {
          const client = await mongodb.MongoClient.connect(db.uri, {
              useNewUrlParser: true,
              useUnifiedTopology: true,
          });
          return client;
      } catch (error) {
          throw error;
      }
  };
  
  module.exports = conection;
  </code></pre>
  <p>and i make this simple function for acess data layer and return records instered</p>
  <pre><code>const index = async ({ limit = 10, offset = 0, filter = {} }) =&gt; {
      const client = await conection();
      if (filter._id) {
          filter._id = mongodb.ObjectID(filter._id);
      }
      try {
          const collection = client.db(&quot;api&quot;).collection(&quot;user&quot;);
          const data = await collection
              .find({ ...filter })
              .skip(offset)
              .limit(limit)
              .toArray();
          return data;
      } catch (error) {
          throw new Error(error);
      } finally {
          await client.close();
      }
  };
  </code></pre>
  <p>I would like to know if I really need to make the connection and close it with each query or should I keep the connection open</p>
  <p>NOTE: in this case I am using a simple Atlas cluster (free) but I would like to know if I should do this also when working with sql banks like postgres</p>