Complete migration of dbAccess.service to new logger

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-09-28 22:06:10 +01:00
parent f38c6c97fb
commit 10034ddf2a

View File

@ -1,6 +1,4 @@
/* global process */ import { logger } from "../utils/logger.utils";
const log = require("../utils/logs.utils"); // Log Helper
const logger = require("../utils/logger.utils"); // New Logger
const dbUser = process.env.OWL_DB_USER || "owl"; const dbUser = process.env.OWL_DB_USER || "owl";
const dbPass = process.env.OWL_DB_PASS || "twittwoo"; const dbPass = process.env.OWL_DB_PASS || "twittwoo";
@ -16,7 +14,7 @@ const db = client.db(dbName);
async function query(collection, query, returnId = false) { async function query(collection, query, returnId = false) {
await client.connect(); await client.connect();
logger.logger.trace( logger.trace(
`dbAccess.query: Connecting to collection: '${collection}'` `dbAccess.query: Connecting to collection: '${collection}'`
); );
var qcoll = db.collection(collection); var qcoll = db.collection(collection);
@ -24,41 +22,33 @@ async function query(collection, query, returnId = false) {
if (!returnId) { if (!returnId) {
qcursor.project({ _id: 0 }); qcursor.project({ _id: 0 });
} }
logger.logger.trace(query, "dbAccess.query: Runnung Query"); logger.trace(query, "dbAccess.query: Runnung Query");
increment(collection); increment(collection);
let result = await qcursor.toArray(); let result = await qcursor.toArray();
logger.logger.trace(result, "dbAccess.query: Response"); logger.trace(result, "dbAccess.query: Response");
return result; return result;
} }
async function queryProject(collection, query, projection) { async function queryProject(collection, query, projection) {
await client.connect(); await client.connect();
log.out(`dbAccess.queryProject: Connecting to col: '${collection}'`, "info"); logger.debug(`dbAccess.queryProject: Connecting to col: '${collection}'`);
const qcoll = db.collection(collection); const qcoll = db.collection(collection);
const qcursor = qcoll.find(query).project(projection); const qcursor = qcoll.find(query).project(projection);
log.out( logger.debug(projection, `dbAccess.query: Running Query: ${JSON.stringify(query)}`)
`dbAccess.query: Running Query: ${JSON.stringify(
query
)}, Projection: ${JSON.stringify(projection)}`,
"dbug"
);
increment(collection); increment(collection);
return await qcursor.toArray(); return await qcursor.toArray();
} }
async function queryAggregate(collection, pipeline) { async function queryAggregate(collection, pipeline) {
await client.connect(); await client.connect();
log.out(`dbAccess.queryProject: Connecting to col: '${collection}'`, "info"); logger.debug(`dbAccess.queryProject: Connecting to col: '${collection}'`);
log.out( logger.trace(pipeline, "dbAccess.query: Running Aggregation");
`dbAccess.query: Running Aggregation: ${JSON.stringify(pipeline)}`,
"dbug"
);
increment(collection); increment(collection);
return await db.collection(collection).aggregate(pipeline).toArray(); return await db.collection(collection).aggregate(pipeline).toArray();
} }
async function increment(target) { async function increment(target) {
logger.logger.debug( logger.debug(
`dbAccess.increment: Incrementing counter for: ${target}` `dbAccess.increment: Incrementing counter for: ${target}`
); );
await client.connect(); await client.connect();
@ -70,7 +60,7 @@ async function increment(target) {
async function addUser(uuid, domain) { async function addUser(uuid, domain) {
// Needs testing // Needs testing
log.out("dbAccess.addUser: Adding user to database"); logger.debug("dbAccess.addUser: Adding user to database");
let doc = { uuid: uuid, domain: domain, atime: new Date() }; let doc = { uuid: uuid, domain: domain, atime: new Date() };
await client.connect(); await client.connect();
let col = db.collection("users"); let col = db.collection("users");
@ -83,7 +73,7 @@ async function addUser(uuid, domain) {
async function addRegReq(uuid, domain) { async function addRegReq(uuid, domain) {
// Needs testing // Needs testing
log.out("dbAccess.addRegReq: Adding registration request"); logger.debug("dbAccess.addRegReq: Adding registration request");
let doc = { uuid: uuid, time: new Date(), domain: domain }; let doc = { uuid: uuid, time: new Date(), domain: domain };
await client.connect(); await client.connect();
let col = db.collection("registrations"); let col = db.collection("registrations");
@ -93,7 +83,7 @@ async function addRegReq(uuid, domain) {
async function userAtime(uuid) { async function userAtime(uuid) {
// Needs testing // Needs testing
log.out("dbAccess.userAtime: Updating access time for user"); logger.debug("dbAccess.userAtime: Updating access time for user");
let q = { uuid: uuid }; let q = { uuid: uuid };
let n = { $set: { uuid: uuid, atime: new Date() } }; let n = { $set: { uuid: uuid, atime: new Date() } };
await client.connect(); await client.connect();
@ -104,7 +94,7 @@ async function userAtime(uuid) {
// Deletes one single registration request entry from the DB // Deletes one single registration request entry from the DB
async function delRegReq(uuid) { async function delRegReq(uuid) {
log.out("dbAccess.delRegReq: Deleting a Registration Request"); logger.debug("dbAccess.delRegReq: Deleting a Registration Request");
let collection = "registrations"; let collection = "registrations";
await client.connect(); await client.connect();
let col = db.collection(collection); let col = db.collection(collection);
@ -112,11 +102,11 @@ async function delRegReq(uuid) {
} }
async function colCount(collection) { async function colCount(collection) {
log.out(`dbAccess.colCount: Counting entries in collection: ${collection}`); logger.debug(`dbAccess.colCount: Counting entries in collection: ${collection}`);
await client.connect(); await client.connect();
let col = db.collection(collection); let col = db.collection(collection);
let count = col.countDocuments(); let count = col.countDocuments();
log.out( logger.debug(
`dbAccess.colCount: Collection: ${collection} contains ${count}` + `dbAccess.colCount: Collection: ${collection} contains ${count}` +
" documents" " documents"
); );