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
|
// exit app
|
||||||
|
|
||||||
// DB Init
|
// DB Init
|
||||||
initDb.init();
|
initDb.check();
|
||||||
|
|
||||||
// Express Error Handling:
|
// Express Error Handling:
|
||||||
app.use((err, req, res, next) => {
|
app.use((err, req, res, next) => {
|
||||||
|
|
|
@ -13,42 +13,40 @@ 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.dropCollection: checking if collection exists: ${coll}`)
|
log.out(`DbAccess: Removing Existing ${coll} data`)
|
||||||
|
|
||||||
//Some Code Here:
|
|
||||||
await client.connect();
|
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.)
|
|
||||||
|
|
||||||
|
try {
|
||||||
// If Collection Exists:
|
|
||||||
log.out(`DbAccess.dropCollection: dropping collection: ${coll}`)
|
|
||||||
db.dropCollection(coll);
|
db.dropCollection(coll);
|
||||||
log.out(`DbAccess.dropCollection: dropped collection: ${coll}`)
|
log.out(`DbAccess: Collection ${coll} dropped`)
|
||||||
// Else Do Nothing
|
} catch (error) {
|
||||||
|
log.out(`DbAccess: Error deleting collection ${coll}`)
|
||||||
|
log.out(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function putCorpus(data){
|
async function putCorpus(data){
|
||||||
log.out("DbAccess.putCorpus: Uploading CORPUS data to database")
|
log.out("DbAccess: Uploading CORPUS data to database")
|
||||||
await client.connect();
|
await client.connect();
|
||||||
try {
|
|
||||||
var coll = db.collection("corpus");
|
var coll = db.collection("corpus");
|
||||||
|
try {
|
||||||
await coll.insertMany(data);
|
await coll.insertMany(data);
|
||||||
|
log.out("DbAccess: Corpus upload complete")
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.out("DbAccess.putCorpus: Error uploading Corpus data to database")
|
log.out("DbAccess: Error uploading Corpus data to database")
|
||||||
log.out(error)
|
log.out(error)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function putStations(data){
|
async function putStations(data){
|
||||||
log.out("DbAccess.putStations: Uploading Stations data to database")
|
log.out("DbAccess: Uploading Stations data to database")
|
||||||
await client.connect();
|
await client.connect();
|
||||||
try {
|
|
||||||
var coll = db.collection("stations");
|
var coll = db.collection("stations");
|
||||||
|
try {
|
||||||
coll.insertMany(data);
|
coll.insertMany(data);
|
||||||
|
log.out("DbAccess: Stations upload complete")
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.out("DbAccess.putStations: Error uploading Stations data to database")
|
log.out("DbAccess: Error uploading Stations data to database")
|
||||||
log.out(error)
|
log.out(error)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,32 +8,25 @@ 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 = await check();
|
var status = check();
|
||||||
if (status == "not_ready") {
|
// if check() returns false run build, else break.
|
||||||
try {
|
|
||||||
build("all")
|
|
||||||
} catch (err) {
|
|
||||||
log.out("dbInit.init: Error building database")
|
|
||||||
log.out(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function check(){
|
async function check(){
|
||||||
log.out("DbInit.check: Checking database state")
|
log.out("DbInit: Checking database state")
|
||||||
let test = {'collection': 'any'};
|
let test = {'collection': 'any'};
|
||||||
try {
|
try {
|
||||||
await dbAccess.query('meta',test);
|
let meta = await dbAccess.query('meta',test);
|
||||||
log.out(`DbInit.check: Reading Database Collection: meta`);
|
log.out(`DbInit: Reading Database Collection: meta`);
|
||||||
return "not_ready"
|
} catch (error) {log.out(error)}
|
||||||
} catch (error) {
|
if (true){
|
||||||
log.out(error)
|
log.out("DbInit: Database structure not initialised")
|
||||||
return "not_ready"
|
build("all")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.build: Building database structure")
|
log.out("DbInit: 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");
|
||||||
|
|
Reference in New Issue