
var url = require('url');

var dbUrl = null;
if(process.env.DATABASE_URL) {
    dbUrl = url.parse(process.env.DATABASE_URL);
}


module.exports = {
  "development": {
    "username": null,
    "password": null,
    "database": "lightning-viz",
    "host": "127.0.0.1",
    "dialect": "sqlite",
    "port": 5432,
    "sync": {"force": true},
    "storage": 'database.sqlite',
    "logging": false
  },  
  "test": {
    "username": null,
    "password": null,
    "database": "lightning-viz",
    "host": "127.0.0.1",
    "dialect": "postgres",
    "port": 5432,
    "sync": {"force": true},
    "storage": 'database.sqlite',
    "logging": false
  },
  "test-sqlite": {
    "username": null,
    "password": null,
    "database": "lightning-viz",
    "host": "127.0.0.1",
    "dialect": "sqlite",
    "port": 5432,
    "sync": {"force": true},
    "storage": 'database.sqlite',
    "logging": false
  },
  "production": {
    database: (dbUrl) ? dbUrl.path.replace('/', '') : 'lightning-viz',
    username: (dbUrl) ? (dbUrl.auth.split(':') || [false])[0] : 'lightning',
    password: (dbUrl) ? (dbUrl.auth.split(':') || [false])[1] : null,
    host: (dbUrl) ? dbUrl.hostname : '127.0.0.1',
    "sync": {"force": true},
    "logging": false,
    native: true,
    ssl: true,
    "dialect": "postgres"
  }
}

@@ -161,34 +161,153 @@
 ent%22: %7B%0A    
-%22username%22
+database: (dbUrl) ? dbUrl.path.replace('/', '') : 'lightning-viz',%0A    username: (dbUrl) ? (dbUrl.auth.split(':') %7C%7C %5Bfalse%5D)%5B0%5D 
 : null,%0A    
@@ -302,33 +302,32 @@
 : null,%0A    
-%22
 password
 %22: null,%0A   
@@ -318,48 +318,64 @@
 word
-%22
 : 
-null,%0A    %22database%22: %22lightning-viz%22
+(dbUrl) ? (dbUrl.auth.split(':') %7C%7C %5Bfalse%5D)%5B1%5D : null
 ,%0A  
@@ -543,34 +543,153 @@
 est%22: %7B%0A    
-%22username%22
+database: (dbUrl) ? dbUrl.path.replace('/', '') : 'lightning-viz',%0A    username: (dbUrl) ? (dbUrl.auth.split(':') %7C%7C %5Bfalse%5D)%5B0%5D 
 : null,%0A    
@@ -684,33 +684,32 @@
 : null,%0A    
-%22
 password
 %22: null,%0A   
@@ -700,48 +700,64 @@
 word
-%22
 : 
-null,%0A    %22database%22: %22lightning-viz%22
+(dbUrl) ? (dbUrl.auth.split(':') %7C%7C %5Bfalse%5D)%5B1%5D : null
 ,%0A  
@@ -940,18 +940,137 @@
     
-%22username%22
+database: (dbUrl) ? dbUrl.path.replace('/', '') : 'lightning-viz',%0A    username: (dbUrl) ? (dbUrl.auth.split(':') %7C%7C %5Bfalse%5D)%5B0%5D 
 : nu
@@ -1081,17 +1081,16 @@
     
-%22
 password
 %22: n
@@ -1089,48 +1089,64 @@
 word
-%22
 : 
-null,%0A    %22database%22: %22lightning-viz%22
+(dbUrl) ? (dbUrl.auth.split(':') %7C%7C %5Bfalse%5D)%5B1%5D : null
 ,%0A  

add DATABASE_URL config to all node envs