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');
async function get(req, res, next){
async function getStations(req, res, next){
try {
res.json(await list.get(req.body))
res.json(await list.getStations(req.body))
} catch (err) {
console.error(`Unknown Error`, err.message);
console.error(`Controller Error`, err.message);
next(err);
}
}
async function getAll(req, res, next){
async function getCorpus(req, res, next){
try {
res.json(await list.getAll(req.body))
res.json(await list.getCorpus(req.body))
} catch (err) {
console.erroe(`Unknown Error`, err.message);
console.error(`Controller Error`, err.message);
next(err);
}
}
module.exports = {
get,
getAll
getStations,
getCorpus
}

View File

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

View File

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

View File

@ -10,9 +10,9 @@ const { MongoClient } = require('mongodb');
const client = new MongoClient(uri);
const db = client.db(dbName);
async function putCorpusAll(data){
async function putCorpus(data){
await client.connect();
var coll = db.collection("corpusAll");
var coll = db.collection("corpus");
var result = coll.insertMany(data);
console.log(result);
};
@ -24,6 +24,13 @@ async function putStations(data){
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) {
// await client.connect()
// var coll = db.collection(collection);
@ -31,18 +38,16 @@ async function putStations(data){
// 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 = {
putData,
putCorpusAll,
putCorpus,
putStations,
getCrs,
checkCrs
putMeta,
query
}

View File

@ -1,16 +1,16 @@
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
return corpus.initSubset();
return {state:"No can do, not fully implemented."};
}
async function getAll(){
// Raw corpus data
return corpus.initAll();
async function getCorpus(){
// Raw corpus data, currently fetches from Network Rail each time its called
return corpus.get();
}
module.exports = {
get,
getAll
getStations,
getCorpus
}

View File

@ -1,12 +1,38 @@
// FUNCTIONS
// init() : Exported: Uses the internal functions to initialise databases.
// check() : Checks authentication, presence of tables and contents of tables.
// build() : Builds the lookup table using the corpus.utils to download data.
// check() :
// build() : Adds CORPUS Data to DB.
const corpus = require('../services/corpus.services');
const db = require('../services/dbAccess.services');
const corpus = require('../services/corpus.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 = {
init
}