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