From 3af5b9a2135bbd5054bc7304b8e54563b8e3b24e Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Fri, 6 Nov 2015 18:25:07 -0700 Subject: [PATCH] * Much better DB init --- core/bbs.js | 3 +-- core/database.js | 60 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/core/bbs.js b/core/bbs.js index 414260ee..e2bf8c4a 100644 --- a/core/bbs.js +++ b/core/bbs.js @@ -117,8 +117,7 @@ function initialize(cb) { callback(null); }, function initDatabases(callback) { - database.initializeDatabases(); - callback(null); + database.initializeDatabases(callback); }, function initSystemProperties(callback) { require('./system_property.js').loadSystemProperties(callback); diff --git a/core/database.js b/core/database.js index efac0436..b5d11af0 100644 --- a/core/database.js +++ b/core/database.js @@ -2,8 +2,10 @@ 'use strict'; var conf = require('./config.js'); + var sqlite3 = require('sqlite3'); var paths = require('path'); +var async = require('async'); // database handles var dbs = {}; @@ -16,22 +18,50 @@ function getDatabasePath(name) { 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? - dbs.user = new sqlite3.Database(getDatabasePath('user')); - dbs.message = new sqlite3.Database(getDatabasePath('message')); - dbs.system = new sqlite3.Database(getDatabasePath('system')); - - dbs.user.serialize(function serialized() { - createSystemTables(); - createUserTables(); - createInitialUserValues(); - }); - - dbs.message.serialize(function serialized() { - createMessageBaseTables(); - createInitialMessageValues(); - }); + async.series( + [ + function systemDb(callback) { + dbs.system = new sqlite3.Database(getDatabasePath('system'), function dbCreated(err) { + if(err) { + callback(err); + } else { + dbs.system.serialize(function serialized() { + createSystemTables(); + }); + 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(); + }); + 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() { + createMessageBaseTables(); + createInitialMessageValues(); + }); + callback(null); + } + }); + } + ], + cb + ); } function createSystemTables() {