hmnayem
8/24/2019 - 11:11 AM

MongoDB Database Connection Using Mongoose

const connectDB = async () => {
	try {
		await mongoose.connect(config.get('db'), {
			useNewUrlParser: true,
			useCreateIndex: true
		});
		console.log('Database Connected');
	} catch (e) {
		console.log(e);
	}

	const connection = mongoose.connection;
	connection.on('connected', () => {
		console.log('Database Connected Successfully');
	});

	connection.on('disconnected', () => {
		console.log('Database Disconnected');
	});

	connection.on('error', err => {
		console.log('Error Occurred', err);
	});

	connection.on('close', () => {
		console.log('Database Closed Successfully');
	});
};