const MongoClient = require("mongodb").MongoClient;
const {config} = require("../config.js");
const connectionString = config.db.connectionString;
const client = new MongoClient(connectionString);

let db;

const connectDb = async () => {
  if (db) return db;
  try {
    const conn = await client.connect();
    db = conn.db(config.db.dbName);
    return db;
  } catch(e) {
    console.error(e);
  }
};

module.exports = connectDb;