diff --git a/app.js b/app.js index 4436eb0..35b411d 100644 --- a/app.js +++ b/app.js @@ -33,7 +33,7 @@ log.out(`Starting OwlBoard - App Version: ${version.app} - API versions: ${versi // exit app // DB Init -initDb.check(); +initDb.init(); // Express Error Handling: app.use((err, req, res, next) => { diff --git a/src/services/dbAccess.services.js b/src/services/dbAccess.services.js index adb780c..784de4b 100644 --- a/src/services/dbAccess.services.js +++ b/src/services/dbAccess.services.js @@ -13,40 +13,36 @@ const client = new MongoClient(uri); const db = client.db(dbName); async function dropCollection(coll){ - log.out(`DbAccess: Removing Existing ${coll} data`) + log.out(`DbAccess.dropCollection: checking if collection exists: ${coll}`) + //Some Code Here + // If Collection Exists: + log.out(`DbAccess.dropCollection: dropping collection: ${coll}`) await client.connect(); - - try { - db.dropCollection(coll); - log.out(`DbAccess: Collection ${coll} dropped`) - } catch (error) { - log.out(`DbAccess: Error deleting collection ${coll}`) - log.out(error) - } + db.dropCollection(coll); + log.out(`DbAccess.dropCollection: dropped collection: ${coll}`) + // Else Do Nothing } async function putCorpus(data){ - log.out("DbAccess: Uploading CORPUS data to database") + log.out("DbAccess.putCorpus: Uploading CORPUS data to database") await client.connect(); - var coll = db.collection("corpus"); try { + var coll = db.collection("corpus"); await coll.insertMany(data); - log.out("DbAccess: Corpus upload complete") } catch (error) { - log.out("DbAccess: Error uploading Corpus data to database") + log.out("DbAccess.putCorpus: Error uploading Corpus data to database") log.out(error) } }; async function putStations(data){ - log.out("DbAccess: Uploading Stations data to database") + log.out("DbAccess.putStations: Uploading Stations data to database") await client.connect(); - var coll = db.collection("stations"); try { + var coll = db.collection("stations"); coll.insertMany(data); - log.out("DbAccess: Stations upload complete") } catch (error) { - log.out("DbAccess: Error uploading Stations data to database") + log.out("DbAccess.putStations: Error uploading Stations data to database") log.out(error) } }; diff --git a/src/utils/dbinit.utils.js b/src/utils/dbinit.utils.js index cc2a292..611798a 100644 --- a/src/utils/dbinit.utils.js +++ b/src/utils/dbinit.utils.js @@ -8,25 +8,32 @@ const corpus = require('../services/corpus.services'); const dbAccess = require('../services/dbAccess.services'); async function init(){ - var status = check(); - // if check() returns false run build, else break. + var status = await check(); + if (status == "not_ready") { + try { + build("all") + } catch (err) { + log.out("dbInit.init: Error building database") + log.out(err) + } + } } async function check(){ - log.out("DbInit: Checking database state") + log.out("DbInit.check: Checking database state") let test = {'collection': 'any'}; try { - let meta = await dbAccess.query('meta',test); - log.out(`DbInit: Reading Database Collection: meta`); - } catch (error) {log.out(error)} - if (true){ - log.out("DbInit: Database structure not initialised") - build("all") + await dbAccess.query('meta',test); + log.out(`DbInit.check: Reading Database Collection: meta`); + return "not_ready" + } catch (error) { + log.out(error) + return "not_ready" } } async function build(db){ // `db` must be one of: `corpus`, `stations`, `all`. - log.out("DbInit: Building database structure") + log.out("DbInit.build: Building database structure") var corpusAll = await corpus.get(); if (db === "corpus" || "all") { await dbAccess.dropCollection("corpus");