Working on Database Access

This commit is contained in:
Fred Boniface 2022-12-23 21:27:43 +00:00
parent 718b28c0ab
commit d826176851
6 changed files with 71 additions and 40 deletions

View File

@ -1,24 +1,24 @@
const list = require('../services/list.services'); const list = require('../services/list.services');
async function get(req, res, next){ async function getStations(req, res, next){
try { try {
res.json(await list.get(req.body)) res.json(await list.getStations(req.body))
} catch (err) { } catch (err) {
console.error(`Unknown Error`, err.message); console.error(`Controller Error`, err.message);
next(err); next(err);
} }
} }
async function getAll(req, res, next){ async function getCorpus(req, res, next){
try { try {
res.json(await list.getAll(req.body)) res.json(await list.getCorpus(req.body))
} catch (err) { } catch (err) {
console.erroe(`Unknown Error`, err.message); console.error(`Controller Error`, err.message);
next(err); next(err);
} }
} }
module.exports = { module.exports = {
get, getStations,
getAll getCorpus
} }

View File

@ -14,7 +14,7 @@ const listController = require('../controllers/list.controllers');
/* DELETE programming language */ /* DELETE programming language */
//router.delete('/:id', programmingLanguagesController.remove); //router.delete('/:id', programmingLanguagesController.remove);
router.get('/', listController.get); router.get('/stations', listController.getStations);
router.get('/all', listController.getAll); router.get('/corpus', listController.getCorpus);
module.exports = router; module.exports = router;

View File

@ -15,18 +15,18 @@ const gz = require('node-gzip')
const corpusUser = process.env.OWL_LDB_CORPUSUSER const corpusUser = process.env.OWL_LDB_CORPUSUSER
const corpusPass = process.env.OWL_LDB_CORPUSPASS const corpusPass = process.env.OWL_LDB_CORPUSPASS
async function initSubset(allCorpus) { async function subset(allCorpus) {
var cleanCorpus = await clean(allCorpus) var cleanCorpus = await clean(allCorpus)
return cleanCorpus return cleanCorpus
} }
async function initAll(){ async function get(){
var gzipData = await get() var gzipData = await fetch()
var allCorpus = await extract(gzipData) var allCorpus = await extract(gzipData)
return allCorpus return allCorpus
} }
async function get() { async function fetch() {
authHead = Buffer.from(`${corpusUser}:${corpusPass}`).toString('base64') authHead = Buffer.from(`${corpusUser}:${corpusPass}`).toString('base64')
const url = 'https://datafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS' const url = 'https://datafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS'
const options = { const options = {
@ -60,6 +60,6 @@ async function clean(input) {
} }
module.exports = { module.exports = {
initAll, get,
initSubset subset
} }

View File

@ -10,9 +10,9 @@ 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 putCorpusAll(data){ async function putCorpus(data){
await client.connect(); await client.connect();
var coll = db.collection("corpusAll"); var coll = db.collection("corpus");
var result = coll.insertMany(data); var result = coll.insertMany(data);
console.log(result); console.log(result);
}; };
@ -24,6 +24,13 @@ async function putStations(data){
console.log(result); console.log(result);
}; };
async function putMeta(data){
await client.connect();
var coll = db.collection("meta");
var result = coll.insertMany(data);
console.log(result);
}
//async function putData(data,collection) { //async function putData(data,collection) {
// await client.connect() // await client.connect()
// var coll = db.collection(collection); // var coll = db.collection(collection);
@ -31,18 +38,16 @@ async function putStations(data){
// console.log(result); // console.log(result);
//}; //};
async function getCrs(tiploc) { async function query(collection, query){
await client.connect();
var coll = db.collection(collection);
var result = coll.find(query) //???
}; }
async function checkCrs(crs) {
};
module.exports = { module.exports = {
putData, putCorpus,
putCorpusAll,
putStations, putStations,
getCrs, putMeta,
checkCrs query
} }

View File

@ -1,16 +1,16 @@
const corpus = require('../services/corpus.services'); const corpus = require('../services/corpus.services');
async function get(){ async function getStations(){
// Databse lookup, get list of all stations as per the Python script output // Databse lookup, get list of all stations as per the Python script output
return corpus.initSubset(); return {state:"No can do, not fully implemented."};
} }
async function getAll(){ async function getCorpus(){
// Raw corpus data // Raw corpus data, currently fetches from Network Rail each time its called
return corpus.initAll(); return corpus.get();
} }
module.exports = { module.exports = {
get, getStations,
getAll getCorpus
} }

View File

@ -1,12 +1,38 @@
// FUNCTIONS // FUNCTIONS
// init() : Exported: Uses the internal functions to initialise databases. // init() : Exported: Uses the internal functions to initialise databases.
// check() : Checks authentication, presence of tables and contents of tables. // check() :
// build() : Builds the lookup table using the corpus.utils to download data. // build() : Adds CORPUS Data to DB.
const corpus = require('../services/corpus.services'); const corpus = require('../services/corpus.services');
const db = require('../services/dbAccess.services'); const dbAccess = require('../services/dbAccess.services');
async function init(){
var status = check();
// if check() returns false run build, else break.
}
async function check(){
// Check meta collection for last update
// If last update more than 14 days () ago then return false
// Check corpus and stations collections exist. If not return false
// Return true if checks pass.
}
async function build(db){ // `db` must be one of: `corpus`, `stations`, `all`.
var corpusAll = await corpus.get();
if (db === "corpus" || "all") {
dbAccess.putCorpus(corpusAll);
meta = {collection:"any",updated:Math.floor(Date.now() / 1000)};
dbAccess.putMeta(meta);
}
if (db === "stations" || "all") {
var corpusSubset = await corpus.subset(corpusAll);
dbAccess.putStations(corpusSubset);
meta = {collection:"any",updated:Math.floor(Date.now() / 1000)};
dbAccess.putMeta(meta);
}
}
module.exports = { module.exports = {
init
} }