DBAccess updates

This commit is contained in:
Fred Boniface 2023-01-05 21:18:54 +00:00
parent 17ea61d35b
commit 9d55c7d4b9
3 changed files with 18 additions and 3 deletions

2
app.js
View File

@ -55,7 +55,7 @@ app.use('/api/v1/kube', kubeRtr);
// Start Express
app.listen(srvPort, srvListen, (error) =>{
if(!error) {
log.out(`Server: Started on http://${srvListen}:${srvPort}`);
log.out(`Server: Listening on http://${srvListen}:${srvPort}`);
log.out("State: alive")
} else {
log.out("Error occurred, server can't start", error);

View File

@ -12,9 +12,21 @@ const { MongoClient } = require('mongodb');
const client = new MongoClient(uri);
const db = client.db(dbName);
async function dropCollection(coll){
log.out(`DbAccess: Removing Existing ${coll} data`)
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)
}
}
async function putCorpus(data){
log.out("DbAccess: Uploading Corpus data to database")
log.out("DbAccess: Uploading CORPUS data to database")
await client.connect();
var coll = db.collection("corpus");
try {
@ -63,6 +75,7 @@ async function query(collection, query){
module.exports = {
putCorpus,
putStations,
dropCollection,
putMeta,
query
}

View File

@ -19,7 +19,7 @@ async function check(){
let meta = await dbAccess.query('meta',test);
log.out(`DbInit: Reading Database Collection: meta`);
} catch (error) {log.out(error)}
if (false){
if (true){
log.out("DbInit: Database structure not initialised")
build("all")
}
@ -29,11 +29,13 @@ async function build(db){ // `db` must be one of: `corpus`, `stations`, `all`.
log.out("DbInit: Building database structure")
var corpusAll = await corpus.get();
if (db === "corpus" || "all") {
await dbAccess.dropCollection("corpus");
dbAccess.putCorpus(corpusAll);
meta = [{collection:"any",updated:Math.floor(Date.now() / 1000)}];
dbAccess.putMeta(meta);
}
if (db === "stations" || "all") {
await dbAccess.dropCollection("stations");
var corpusSubset = await corpus.subset(corpusAll);
dbAccess.putStations(corpusSubset);
meta = [{collection:"any",updated:Math.floor(Date.now() / 1000)}];