2023-02-09 20:34:53 +00:00
|
|
|
const log = require('../utils/log.utils'); // Log Helper
|
|
|
|
|
|
|
|
const dbUser = process.env.OWL_DB_USER || "owl"
|
|
|
|
const dbPass = process.env.OWL_DB_PASS || "twittwoo"
|
|
|
|
const dbName = process.env.OWL_DB_NAME || "owlboard"
|
|
|
|
const dbPort = process.env.OWL_DB_PORT || 27017
|
|
|
|
const dbHost = process.env.OWL_DB_HOST || "localhost"
|
|
|
|
const uri = `mongodb://${dbUser}:${dbPass}@${dbHost}:${dbPort}`;
|
|
|
|
|
|
|
|
const { MongoClient } = require('mongodb');
|
|
|
|
|
|
|
|
const client = new MongoClient(uri);
|
|
|
|
const db = client.db(dbName);
|
|
|
|
|
|
|
|
async function query(collection, query){
|
|
|
|
await client.connect();
|
2023-03-30 21:15:21 +01:00
|
|
|
log.out(`dbAccess.query: Connecting to collection: '${collection}'`, "info")
|
2023-02-09 20:34:53 +00:00
|
|
|
var qcoll = db.collection(collection);
|
|
|
|
var qcursor = qcoll.find(query)
|
|
|
|
qcursor.project({_id: 0})
|
2023-03-30 21:15:21 +01:00
|
|
|
log.out(`dbAccess.query: Running Query: ${JSON.stringify(query)}`, "info")
|
2023-02-09 20:34:53 +00:00
|
|
|
increment(collection)
|
|
|
|
return (await qcursor.toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
async function increment(target) {
|
2023-03-30 21:15:21 +01:00
|
|
|
log.out(`dbAccess.increment: Incrementing counter for: ${target}`, "info")
|
2023-02-09 20:34:53 +00:00
|
|
|
await client.connect();
|
|
|
|
let col = db.collection("meta");
|
|
|
|
let update = {}
|
|
|
|
update[target] = 1
|
|
|
|
col.updateOne({target: "counters"}, {$inc:update})
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
query,
|
2023-02-16 21:25:19 +00:00
|
|
|
increment
|
2023-02-09 20:34:53 +00:00
|
|
|
}
|