diff --git a/scripts/DBParser.JS b/scripts/DBParser.JS new file mode 100644 index 0000000..6f884c2 --- /dev/null +++ b/scripts/DBParser.JS @@ -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; \ No newline at end of file