Compare commits
No commits in common. "1c269571b7b04f7ac4c242ace471df2bae18b19c" and "77c5ef2cf522d40f256a597834f4958fbaf7990f" have entirely different histories.
1c269571b7
...
77c5ef2cf5
2
app.js
2
app.js
|
@ -33,7 +33,7 @@ log.out(`Starting OwlBoard - App Version: ${version.app} - API versions: ${versi
|
|||
// exit app
|
||||
|
||||
// DB Init
|
||||
initDb.init();
|
||||
initDb.check();
|
||||
|
||||
// Express Error Handling:
|
||||
app.use((err, req, res, next) => {
|
||||
|
|
|
@ -13,42 +13,40 @@ const client = new MongoClient(uri);
|
|||
const db = client.db(dbName);
|
||||
|
||||
async function dropCollection(coll){
|
||||
log.out(`DbAccess.dropCollection: checking if collection exists: ${coll}`)
|
||||
|
||||
//Some Code Here:
|
||||
log.out(`DbAccess: Removing Existing ${coll} data`)
|
||||
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}`)
|
||||
try {
|
||||
db.dropCollection(coll);
|
||||
log.out(`DbAccess.dropCollection: dropped collection: ${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){
|
||||
log.out("DbAccess.putCorpus: Uploading CORPUS data to database")
|
||||
log.out("DbAccess: Uploading CORPUS data to database")
|
||||
await client.connect();
|
||||
try {
|
||||
var coll = db.collection("corpus");
|
||||
try {
|
||||
await coll.insertMany(data);
|
||||
log.out("DbAccess: Corpus upload complete")
|
||||
} catch (error) {
|
||||
log.out("DbAccess.putCorpus: Error uploading Corpus data to database")
|
||||
log.out("DbAccess: Error uploading Corpus data to database")
|
||||
log.out(error)
|
||||
}
|
||||
};
|
||||
|
||||
async function putStations(data){
|
||||
log.out("DbAccess.putStations: Uploading Stations data to database")
|
||||
log.out("DbAccess: Uploading Stations data to database")
|
||||
await client.connect();
|
||||
try {
|
||||
var coll = db.collection("stations");
|
||||
try {
|
||||
coll.insertMany(data);
|
||||
log.out("DbAccess: Stations upload complete")
|
||||
} catch (error) {
|
||||
log.out("DbAccess.putStations: Error uploading Stations data to database")
|
||||
log.out("DbAccess: Error uploading Stations data to database")
|
||||
log.out(error)
|
||||
}
|
||||
};
|
||||
|
|
|
@ -8,32 +8,25 @@ const corpus = require('../services/corpus.services');
|
|||
const dbAccess = require('../services/dbAccess.services');
|
||||
|
||||
async function init(){
|
||||
var status = await check();
|
||||
if (status == "not_ready") {
|
||||
try {
|
||||
build("all")
|
||||
} catch (err) {
|
||||
log.out("dbInit.init: Error building database")
|
||||
log.out(err)
|
||||
}
|
||||
}
|
||||
var status = check();
|
||||
// if check() returns false run build, else break.
|
||||
}
|
||||
|
||||
async function check(){
|
||||
log.out("DbInit.check: Checking database state")
|
||||
log.out("DbInit: Checking database state")
|
||||
let test = {'collection': 'any'};
|
||||
try {
|
||||
await dbAccess.query('meta',test);
|
||||
log.out(`DbInit.check: Reading Database Collection: meta`);
|
||||
return "not_ready"
|
||||
} catch (error) {
|
||||
log.out(error)
|
||||
return "not_ready"
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
async function build(db){ // `db` must be one of: `corpus`, `stations`, `all`.
|
||||
log.out("DbInit.build: Building database structure")
|
||||
log.out("DbInit: Building database structure")
|
||||
var corpusAll = await corpus.get();
|
||||
if (db === "corpus" || "all") {
|
||||
await dbAccess.dropCollection("corpus");
|
||||
|
|
Reference in New Issue