Compare commits

..

2 Commits

3 changed files with 37 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
// DB Init
initDb.check();
initDb.init();
// Express Error Handling:
app.use((err, req, res, next) => {

View File

@ -13,40 +13,42 @@ const client = new MongoClient(uri);
const db = client.db(dbName);
async function dropCollection(coll){
log.out(`DbAccess: Removing Existing ${coll} data`)
await client.connect();
log.out(`DbAccess.dropCollection: checking if collection exists: ${coll}`)
try {
//Some Code Here:
await client.connect();
var cols = await db.listCollections().toArray()
log.out(`dbAccess.dropCollection: Existing collections: ${JSON.stringify(cols)}`)
// If (any object in the Array contains name:{coll} then exsists. If not doesn't exist.)
// If Collection Exists:
log.out(`DbAccess.dropCollection: dropping collection: ${coll}`)
db.dropCollection(coll);
log.out(`DbAccess: Collection ${coll} dropped`)
} catch (error) {
log.out(`DbAccess: Error deleting collection ${coll}`)
log.out(error)
}
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)
}
};

View File

@ -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");