/**
 * The main database module.
 * @module db/index
 * @author Sören Gade
 *
 * @requires sequelize
 * */

var config = require('../config');
var crypt = require('../crypto/');
var fs        = require('fs'),
    path      = require('path'),
    Sequelize = require('sequelize'),
    lodash    = require('lodash'),
    db        = {};

var sequelize = new Sequelize(config.db.name, config.db.username, config.db.password, {
  dialect: 'sqlite',
  storage: config.db.name,
  
  logging: ( lodash.contains(['production', 'test'], process.env.NODE_ENV) ) ? false : console.log
});

var dirname = __dirname + "/models";

fs.readdirSync(dirname).filter(function(file) {
  return (file.indexOf('.') !== 0) && (file !== 'index.js');
}).forEach(function(file) {    
  var model = sequelize.import(path.join(dirname, file));
  db[model.name] = model;
});
 
Object.keys(db).forEach(function(modelName) {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db);
  }
});

// TODO remove force: true
sequelize.sync().complete(function(err) {
  if ( !err ) {
    if(!lodash.contains(['production', 'test'], process.env.NODE_ENV)) {
      console.log("Connected to database.");
    }
    
    if(process.env.NODE_ENV !== 'test') {
      // Dummy data
      db.User.findOrCreate({
        username: 'test',
        password: crypt.encrypt('test'),
        type: 'admin'
      }).success(function(userTest) {
      
        db.Card.findOrCreate({
          uid: '6040082934'
        }).success(function(cardTest) {
          userTest.addCard(cardTest);
        });
      
      });
    }
  }
});

module.exports = lodash.extend({
  sequelize: sequelize,
  Sequelize: Sequelize
}, db);

@@ -967,16 +967,52 @@
  %7D%0A%7D);%0A%0A
+var initDB = new function(done) %7B%0A  
 // TODO 
@@ -1030,16 +1030,18 @@
 e: true%0A
+  
 sequeliz
@@ -1047,16 +1047,29 @@
 ze.sync(
+%7Bforce: true%7D
 ).comple
@@ -1083,24 +1083,26 @@
 tion(err) %7B%0A
+  
   if ( !err 
@@ -1101,24 +1101,26 @@
  ( !err ) %7B%0A
+  
     if(!loda
@@ -1176,24 +1176,26 @@
 ODE_ENV)) %7B%0A
+  
       consol
@@ -1235,15 +1235,19 @@
     
+  
 %7D%0A    %0A
+  
     
@@ -1290,16 +1290,18 @@
 %7B%0A      
+  
 // Dummy
@@ -1312,16 +1312,18 @@
 a%0A      
+ 
+ 
 db.User.
@@ -1341,24 +1341,26 @@
 e(%7B%0A        
+  
 username: 't
@@ -1373,16 +1373,18 @@
         
+ 
+ 
 password
@@ -1404,24 +1404,26 @@
 pt('test'),%0A
+  
         type
@@ -1434,24 +1434,26 @@
 dmin'%0A      
+  
 %7D).success(f
@@ -1475,24 +1475,26 @@
 t) %7B%0A      %0A
+  
         db.C
@@ -1522,16 +1522,18 @@
         
+  
 uid: '60
@@ -1542,32 +1542,34 @@
 082934'%0A        
+ 
+ 
 %7D).success(funct
@@ -1590,24 +1590,26 @@
 %7B%0A          
+  
 userTest.add
@@ -1624,16 +1624,38 @@
 dTest);%0A
+            done();%0A  
         
@@ -1671,24 +1671,26 @@
  %0A      
+  
 %7D);%0A    
 %7D%0A  %7D%0A%7D)
@@ -1685,17 +1685,25 @@
     
+ 
+ 
 %7D%0A  
+ 
+ 
 %7D%0A
+  
 %7D);
+%0A%7D
 %0A%0Amo
@@ -1779,16 +1779,32 @@
 equelize
+,%0A  init: initDB
 %0A%7D, db);

Put DB Init it its own method.