const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;

let _db;

const mongoConnect = (callback) => {
	MongoClient.connect('mongodb+srv://emdbadmin:emdbauto@empool-3klmr.mongodb.net/subscribers?retryWrites=true&w=majority')
		.then(client => {
			console.log('Connected to db successfully...');
			_db = client.db(); // method connecting and storing the connection to the db (keeps running)
			callback(client);
		})
		.catch(err => {
			console.log(err);
			throw err;
		})
}

const getDb = () => { // method returning db access if exists
	if (_db) {
		return _db;
	}
	throw 'no db found';
}

/*module.export = mongoConnect;*/
exports.mongoConnect = mongoConnect;
exports.getDb = getDb;