{"size": 622, "ext": "js", "max_stars_repo_path": "util/db_util.js", "max_stars_repo_name": "namit-piriya/labfileSubmission", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "util/db_util.js", "max_issues_repo_name": "namit-piriya/labfileSubmission", "max_issues_repo_issues_event_min_datetime": "2020-09-09T17:14:56.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T15:01:34.000Z", "max_forks_repo_path": "util/db_util.js", "max_forks_repo_name": "namit-piriya/labfileSubmission", "max_forks_repo_forks_event_min_datetime": "2020-09-09T10:46:59.000Z", "max_forks_repo_forks_event_max_datetime": "2020-09-09T10:46:59.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 23.037037037, "max_line_length": 51, "alphanum_fraction": 0.6575562701}
const keys = require("../config/keys");
const uri = keys.DB_STRING;
const dbName = keys.DB;
const MongoClient = require("mongodb").MongoClient;
const client = new MongoClient(uri, {
	useNewUrlParser: true,
	useUnifiedTopology: true
});

const mongoPromise = new Promise((res, rej) => {
	if (!dbName)
		throw new Error("db name not recieved!")
	client
		.connect()
		.then((mongo) => {
			res(mongo.db(dbName));
			console.log("connnected to " + dbName);
		})
		.catch((err) => {
			rej("connection problem " + err);
		});
});

let db;
module.exports = async function getDBName() {
	return !db ? await mongoPromise : db;
};