DBAccess updates
This commit is contained in:
parent
17ea61d35b
commit
9d55c7d4b9
2
app.js
2
app.js
@ -55,7 +55,7 @@ app.use('/api/v1/kube', kubeRtr);
|
|||||||
// Start Express
|
// Start Express
|
||||||
app.listen(srvPort, srvListen, (error) =>{
|
app.listen(srvPort, srvListen, (error) =>{
|
||||||
if(!error) {
|
if(!error) {
|
||||||
log.out(`Server: Started on http://${srvListen}:${srvPort}`);
|
log.out(`Server: Listening on http://${srvListen}:${srvPort}`);
|
||||||
log.out("State: alive")
|
log.out("State: alive")
|
||||||
} else {
|
} else {
|
||||||
log.out("Error occurred, server can't start", error);
|
log.out("Error occurred, server can't start", error);
|
||||||
|
@ -12,9 +12,21 @@ const { MongoClient } = require('mongodb');
|
|||||||
const client = new MongoClient(uri);
|
const client = new MongoClient(uri);
|
||||||
const db = client.db(dbName);
|
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){
|
async function putCorpus(data){
|
||||||
log.out("DbAccess: Uploading Corpus data to database")
|
log.out("DbAccess: Uploading CORPUS data to database")
|
||||||
await client.connect();
|
await client.connect();
|
||||||
var coll = db.collection("corpus");
|
var coll = db.collection("corpus");
|
||||||
try {
|
try {
|
||||||
@ -63,6 +75,7 @@ async function query(collection, query){
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
putCorpus,
|
putCorpus,
|
||||||
putStations,
|
putStations,
|
||||||
|
dropCollection,
|
||||||
putMeta,
|
putMeta,
|
||||||
query
|
query
|
||||||
}
|
}
|
@ -19,7 +19,7 @@ async function check(){
|
|||||||
let meta = await dbAccess.query('meta',test);
|
let meta = await dbAccess.query('meta',test);
|
||||||
log.out(`DbInit: Reading Database Collection: meta`);
|
log.out(`DbInit: Reading Database Collection: meta`);
|
||||||
} catch (error) {log.out(error)}
|
} catch (error) {log.out(error)}
|
||||||
if (false){
|
if (true){
|
||||||
log.out("DbInit: Database structure not initialised")
|
log.out("DbInit: Database structure not initialised")
|
||||||
build("all")
|
build("all")
|
||||||
}
|
}
|
||||||
@ -29,11 +29,13 @@ async function build(db){ // `db` must be one of: `corpus`, `stations`, `all`.
|
|||||||
log.out("DbInit: 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");
|
||||||
dbAccess.putCorpus(corpusAll);
|
dbAccess.putCorpus(corpusAll);
|
||||||
meta = [{collection:"any",updated:Math.floor(Date.now() / 1000)}];
|
meta = [{collection:"any",updated:Math.floor(Date.now() / 1000)}];
|
||||||
dbAccess.putMeta(meta);
|
dbAccess.putMeta(meta);
|
||||||
}
|
}
|
||||||
if (db === "stations" || "all") {
|
if (db === "stations" || "all") {
|
||||||
|
await dbAccess.dropCollection("stations");
|
||||||
var corpusSubset = await corpus.subset(corpusAll);
|
var corpusSubset = await corpus.subset(corpusAll);
|
||||||
dbAccess.putStations(corpusSubset);
|
dbAccess.putStations(corpusSubset);
|
||||||
meta = [{collection:"any",updated:Math.floor(Date.now() / 1000)}];
|
meta = [{collection:"any",updated:Math.floor(Date.now() / 1000)}];
|
||||||
|
Reference in New Issue
Block a user