Upload DBParser.js

Add parser for activities, user, and log
This commit is contained in:
buzz-lightsnack-2007 2025-04-06 15:14:26 +08:00
parent 8809c06912
commit 8b7349c9bc

100
scripts/DBParser.JS Normal file
View file

@ -0,0 +1,100 @@
/*
Modify data returned by the database connector scripts. s
*/
class DBParser {
mappings;
data;
constructor(DATA = {}, MAPPINGS = {}) {
this.data = DATA;
this.mappings = MAPPINGS;
}
/*
Modify the data for output.
@return {Object} cleaned data
*/
format () {
let ENTRIES;
/*
Map certain new keys to their orignal counterparts.
*/
const map = (ROW) => {
let RESULT = {};
Object.entries(this.mappings).forEach(([NEW, OLD]) => {
if (Object.keys(ROW).includes(OLD)) {
RESULT[NEW] = (typeof ROW[OLD]).includes(`str`) ? ROW[OLD].trim() : ROW[OLD];
}
});
return RESULT;
};
if (this.data instanceof Array) {
ENTRIES = [];
(this.data.length) ? this.data.forEach((SELECTED) => {
if (SELECTED) {
let CLEANED = map(SELECTED);
(Object.keys(CLEANED).length) ? ENTRIES.push(CLEANED) : false;
};
}) : false;
} else if (this.data && this.data instanceof Object) {
ENTRIES = map(this.data);
};
this.cleaned = ENTRIES;
return (this.cleaned);
}
}
DBParser.User = class User extends DBParser {
/*
Modify user information.
@param {Object} ENTRY the selected entry, or the entries
*/
constructor(DATA) {
super(DATA, {
"_id": "ID",
"username": "name"
});
}
}
DBParser.Activity = class Activity extends DBParser {
/*
Modify user information.
@param {Object} ENTRY the selected entry, or the entries
*/
constructor(ENTRY) {
super(DATA, {
"_id": "ID",
"username": "username",
"description": "description",
"duration": "duration",
"date": "date"
});
}
}
DBParser.Log = class Log extends DBParser {
/*
Clean the log information.
@param {Object} ENTRY the selected entry, or the entries
*/
constructor(ENTRY) {
super(ENTRY, {
"_id": "ID",
"username": "name",
"count": "count",
"log": "activities"
});
};
}
module.exports = DBParser;