var r = require('rethinkdb');
var config = require('./config.js');
var util = require('util');

var connection;
r.connect({
    host: config.host,
    port: config.port,
    db: config.db
}, function(error, conn) {
    if (error) throw error;

    connection = conn;
    dropDb(config.db, function() {
        createDb(config.db, function() {
            createTable('Cat', function() { createIndex('Cat', 'name') })
            createTable('Dog')
            createTable('Human')
            createTable('Task', function() { createIndex('Task', 'catId') })
            createTable('Mother')
            createTable('CatTaskLink', function() { createIndex('CatTaskLink', 'catId'); createIndex('CatTaskLink', 'taskId') })
        })
    })
});

function dropDb(name, cb) {
    r.dbDrop(name).run(connection, function(error, result) {
        if (error) console.log(error);
        if ((result != null) && (result.created === 1)) {
            console.log('Db `'+name+'` created');
        }
        else {
            console.log('Error: Database `'+config.db+'` was not deleted.');
        }

        if (typeof cb === 'function') {
            cb()
        }

    });
}

function createDb(name, cb) {
    r.dbCreate(config.db).run(connection, function(error, result) {
        if (error) throw error;
        if ((result != null) && (result.created === 1)) {
            console.log('Db `'+name+'` created');
            if (typeof cb === 'function') {
                cb()
            }
        }
        else {
            console.log('Error: Database `'+name+'` not created');
        }
    });
}

function createTable(name, cb) {
    r.db(config.db).tableCreate(name).run(connection, function(error, result) {
        if (error) throw error;
        if ((result != null) && (result.created === 1)) {
            console.log('Table `'+name+'` created');
            if (typeof cb === 'function') {
                cb()
            }
        }
        else {
            console.log('Error: Table `'+name+'` not created');
        }
    });
}

function createIndex(table, name, cb) {
    r.db(config.db).table(table).indexCreate(name).run(connection, function(error, result) {
        if (error) throw error;
        if ((result != null) && (result.created === 1)) {
            console.log('Index `'+name+'` created');
            if (typeof cb === 'function') {
                cb()
            }
        }
        else {
            console.log('Error: Index '+name+' not created');
        }
    });
}

@@ -105,16 +105,61 @@
 ection;%0A
+var indexCreated = 0;%0Avar indexToCreate = 4;%0A
 r.connec
@@ -450,16 +450,22 @@
 , 'name'
+, done
 ) %7D)%0A   
@@ -597,16 +597,22 @@
  'catId'
+, done
 ) %7D)%0A   
@@ -728,16 +728,22 @@
  'catId'
+, done
 ); creat
@@ -772,16 +772,22 @@
 'taskId'
+, done
 ) %7D)%0A   
@@ -2551,28 +2551,214 @@
 ated');%0A        %7D%0A    %7D);%0A%7D%0A
+%0Afunction done() %7B%0A    indexCreated++;%0A    if (indexCreated === indexToCreate) %7B%0A        console.log('Init script complete, please check for errors.')%0A        connection.close()%0A    %7D%0A%7D%0A

Make init.js script terminate -- Fix #27