/**
 *  Start MongoDB connection
 * @module init/mongo
 * @param {Object} config website configuration
 * @returns {Function} method to start MongoDB
 */
import mongoose from 'mongoose';
import Promise from 'bluebird';
import {partial} from 'ramda';

export default (config)=> {
    const mongoUrl = config.mongo;

    return new Promise((resolve)=> {
        var db;
        if (!mongoUrl) {
            throw new Error('missing_mongo_url');
        }
        mongoose.connect(mongoUrl);
        db = mongoose.connection;
        db.once('open', partial(resolve, [db]));
        db.on('open', onOpen);
        db.on('close', onClose);
    });

    function onOpen() {
        console.log('Mongo connected');
    }

    function onClose() {
        console.log('Mongo connection is closed');
    }
};

