* Much better DB init

This commit is contained in:
Bryan Ashby 2015-11-06 18:25:07 -07:00
parent 2423a00d05
commit 3af5b9a213
2 changed files with 46 additions and 17 deletions

View File

@ -117,8 +117,7 @@ function initialize(cb) {
callback(null); callback(null);
}, },
function initDatabases(callback) { function initDatabases(callback) {
database.initializeDatabases(); database.initializeDatabases(callback);
callback(null);
}, },
function initSystemProperties(callback) { function initSystemProperties(callback) {
require('./system_property.js').loadSystemProperties(callback); require('./system_property.js').loadSystemProperties(callback);

View File

@ -2,8 +2,10 @@
'use strict'; 'use strict';
var conf = require('./config.js'); var conf = require('./config.js');
var sqlite3 = require('sqlite3'); var sqlite3 = require('sqlite3');
var paths = require('path'); var paths = require('path');
var async = require('async');
// database handles // database handles
var dbs = {}; var dbs = {};
@ -16,22 +18,50 @@ function getDatabasePath(name) {
return paths.join(conf.config.paths.db, name + '.sqlite3'); return paths.join(conf.config.paths.db, name + '.sqlite3');
} }
function initializeDatabases() { function initializeDatabases(cb) {
// :TODO: this will need to change if more DB's are added ... why? // :TODO: this will need to change if more DB's are added ... why?
dbs.user = new sqlite3.Database(getDatabasePath('user')); async.series(
dbs.message = new sqlite3.Database(getDatabasePath('message')); [
dbs.system = new sqlite3.Database(getDatabasePath('system')); function systemDb(callback) {
dbs.system = new sqlite3.Database(getDatabasePath('system'), function dbCreated(err) {
dbs.user.serialize(function serialized() { if(err) {
callback(err);
} else {
dbs.system.serialize(function serialized() {
createSystemTables(); createSystemTables();
createUserTables(); });
callback(null);
}
});
},
function userDb(callback) {
dbs.user = new sqlite3.Database(getDatabasePath('user'), function dbCreated(err) {
if(err) {
callback(err);
} else {
dbs.user.serialize(function serialized() {
createInitialUserValues(); createInitialUserValues();
}); });
callback(null);
}
});
},
function messageDb(callback) {
dbs.message = new sqlite3.Database(getDatabasePath('message'), function dbCreated(err) {
if(err) {
callback(err);
} else {
dbs.message.serialize(function serialized() { dbs.message.serialize(function serialized() {
createMessageBaseTables(); createMessageBaseTables();
createInitialMessageValues(); createInitialMessageValues();
}); });
callback(null);
}
});
}
],
cb
);
} }
function createSystemTables() { function createSystemTables() {