Adjust DB Features logging methods

This commit is contained in:
Fred Boniface 2023-01-10 20:54:19 +00:00
parent 77c5ef2cf5
commit 538325cada
3 changed files with 31 additions and 28 deletions

2
app.js
View File

@ -33,7 +33,7 @@ log.out(`Starting OwlBoard - App Version: ${version.app} - API versions: ${versi
// exit app // exit app
// DB Init // DB Init
initDb.check(); initDb.init();
// Express Error Handling: // Express Error Handling:
app.use((err, req, res, next) => { app.use((err, req, res, next) => {

View File

@ -13,40 +13,36 @@ const client = new MongoClient(uri);
const db = client.db(dbName); const db = client.db(dbName);
async function dropCollection(coll){ 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(); await client.connect();
db.dropCollection(coll);
try { log.out(`DbAccess.dropCollection: dropped collection: ${coll}`)
db.dropCollection(coll); // Else Do Nothing
log.out(`DbAccess: Collection ${coll} dropped`)
} catch (error) {
log.out(`DbAccess: Error deleting collection ${coll}`)
log.out(error)
}
} }
async function putCorpus(data){ async function putCorpus(data){
log.out("DbAccess: Uploading CORPUS data to database") log.out("DbAccess.putCorpus: Uploading CORPUS data to database")
await client.connect(); await client.connect();
var coll = db.collection("corpus");
try { try {
var coll = db.collection("corpus");
await coll.insertMany(data); await coll.insertMany(data);
log.out("DbAccess: Corpus upload complete")
} catch (error) { } catch (error) {
log.out("DbAccess: Error uploading Corpus data to database") log.out("DbAccess.putCorpus: Error uploading Corpus data to database")
log.out(error) log.out(error)
} }
}; };
async function putStations(data){ async function putStations(data){
log.out("DbAccess: Uploading Stations data to database") log.out("DbAccess.putStations: Uploading Stations data to database")
await client.connect(); await client.connect();
var coll = db.collection("stations");
try { try {
var coll = db.collection("stations");
coll.insertMany(data); coll.insertMany(data);
log.out("DbAccess: Stations upload complete")
} catch (error) { } catch (error) {
log.out("DbAccess: Error uploading Stations data to database") log.out("DbAccess.putStations: Error uploading Stations data to database")
log.out(error) log.out(error)
} }
}; };

View File

@ -8,25 +8,32 @@ const corpus = require('../services/corpus.services');
const dbAccess = require('../services/dbAccess.services'); const dbAccess = require('../services/dbAccess.services');
async function init(){ async function init(){
var status = check(); var status = await check();
// if check() returns false run build, else break. if (status == "not_ready") {
try {
build("all")
} catch (err) {
log.out("dbInit.init: Error building database")
log.out(err)
}
}
} }
async function check(){ async function check(){
log.out("DbInit: Checking database state") log.out("DbInit.check: Checking database state")
let test = {'collection': 'any'}; let test = {'collection': 'any'};
try { try {
let meta = await dbAccess.query('meta',test); await dbAccess.query('meta',test);
log.out(`DbInit: Reading Database Collection: meta`); log.out(`DbInit.check: Reading Database Collection: meta`);
} catch (error) {log.out(error)} return "not_ready"
if (true){ } catch (error) {
log.out("DbInit: Database structure not initialised") log.out(error)
build("all") return "not_ready"
} }
} }
async function build(db){ // `db` must be one of: `corpus`, `stations`, `all`. 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(); var corpusAll = await corpus.get();
if (db === "corpus" || "all") { if (db === "corpus" || "all") {
await dbAccess.dropCollection("corpus"); await dbAccess.dropCollection("corpus");