/*eslint no-console: ["error", { allow: ["log"] }] */

// dependencies --------------------------------------------------

import {MongoClient} from 'mongodb';
import config        from '../config';
import async         from 'async';

export default {

    /**
     * DB
     *
     * A storage location for a the database instance.
     * Used to access the native mongo api.
     */
    dbs: {
        crn:     null,
        scitran: null
    },

    /**
     * Collections
     *
     * A list of all mongo collections and a simplified
     * interface for accessing them. Collections start
     * out null an are initialized after mongo connects.
     */
    collections: {
        crn: {
            blacklist: null,
            validationQueue: null,
            jobs: null,
            jobDefinitions: null,
            tickets: null,
            userPreferences: null,
            notifications: null,
            counters: null,
            logs: null
        },
        scitran: {
            projects: null,
            project_snapshots: null
        }
    },

    /**
     * Connect
     *
     * Makes a connection to mongodbs and creates an accessible
     * reference to the dbs and collections
     * takes optional callback (using callback to kick of server side job polling)
     */
    connect(callback) {
        async.each(Object.keys(this.dbs), (dbName, cb) => {
            MongoClient.connect(config.mongo.url + dbName, (err, db) => {
                if (err) {
                    console.log(err);
                    process.exit();
                } else {
                    this.dbs[dbName] = db;
                    for (let collectionName in this.collections[dbName]) {
                        if (this.collections[dbName][collectionName] === null) {
                            this.collections[dbName][collectionName] = this.dbs[dbName].collection(collectionName);
                        }
                    }
                    console.log(dbName, ' - db connected');
                }
                cb();
            });
        }, () => {
            if(callback && typeof callback === 'function') {
                callback();
            }
        });
    }
};
@@ -1058,32 +1058,185 @@
       %7D%0A    %7D,%0A%0A
+    /** Index definitions **/%0A    indexes: %7B%0A        crn: %7B%0A            jobs: %7B'analysis.created': -1%7D,%0A            logs: %7B'date': -1%7D%0A        %7D%0A    %7D,%0A%0A
     /**%0A     * C
@@ -2047,16 +2047,266 @@
 nName);%0A
+                            if (this.indexes%5BdbName%5D && this.indexes%5BdbName%5D%5BcollectionName%5D) %7B%0A                                this.collections%5BdbName%5D%5BcollectionName%5D.createIndex(this.indexes%5BdbName%5D%5BcollectionName%5D);%0A                            %7D%0A
         
@@ -2603,8 +2603,9 @@
     %7D%0A%7D;
+%0A

Add some default indexes for the crn mongo database.