'use strict';
const Hoek = require('hoek');
const MongoModels = require('mongo-models');


const register = async function (server, options) {

    Hoek.assert(options.models, 'options.models is required');
    Hoek.assert(options.mongodb, 'options.mongodb is required');
    Hoek.assert(options.mongodb.connection, 'options.mongodb.connection is required');
    Hoek.assert(options.mongodb.connection.uri, 'options.mongodb.connection.uri is required');
    Hoek.assert(options.mongodb.connection.db, 'options.mongodb.connection.db is required');

    const modelModules = options.models.reduce((accumulator, path) => {

        accumulator[path] = require(path);

        return accumulator;
    }, {});

    server.expose('mongo-models', MongoModels);

    server.ext({
        type: 'onPreStart',
        method: async function (_server) {

            if (options.hasOwnProperty('autoIndex') && options.autoIndex === false) {
                return;
            }

            const indexJobs = options.models
                .map((path) => modelModules[path])
                .filter((model) => Boolean(model.indexes))
                .map((model) => model.createIndexes.bind(model, model.indexes));

            await Promise.all(indexJobs);

            server.log(`HapiMongoModels: finished processing auto indexes.`);
        }
    });

    await MongoModels.connect(options.mongodb.connection, options.mongodb.options);

    server.log('HapiMongoModels: successfully connected to the db.');
};


module.exports = {
    name: 'hapi-mongo-models',
    pkg: require('./package.json'),
    register
};

@@ -1265,17 +1265,38 @@
 ver.log(
-%60
+%5B'info', 'mongodb'%5D, '
 HapiMong
@@ -1341,9 +1341,9 @@
 xes.
-%60
+'
 );%0A 
@@ -1460,16 +1460,37 @@
 ver.log(
+%5B'info', 'mongodb'%5D, 
 'HapiMon

fix calls to server.log