chore: separate DBresponse classes
This commit is contained in:
		
							parent
							
								
									048b42f6cd
								
							
						
					
					
						commit
						f6ec954c6f
					
				
					 4 changed files with 54 additions and 47 deletions
				
			
		
							
								
								
									
										20
									
								
								scripts/response/activity.JS
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								scripts/response/activity.JS
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | |||
| const DBParser = require(`./parser.JS`); | ||||
| 
 | ||||
| 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" | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| module.exports = Activity; | ||||
							
								
								
									
										17
									
								
								scripts/response/log.JS
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								scripts/response/log.JS
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | |||
| const DBParser = require(`./parser.JS`) | ||||
| 
 | ||||
| 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" | ||||
| 		}); | ||||
| 	}; | ||||
| } | ||||
							
								
								
									
										53
									
								
								scripts/response/parser.JS
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								scripts/response/parser.JS
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,53 @@ | |||
| /*  | ||||
| 	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); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| module.exports = DBParser; | ||||
							
								
								
									
										17
									
								
								scripts/response/user.JS
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								scripts/response/user.JS
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | |||
| const DBParser = require(`./parser.JS`); | ||||
| 
 | ||||
| class User extends DBParser { | ||||
| 	/* | ||||
| 		Modify user information.  | ||||
| 	 | ||||
| 		@param {Object} ENTRY the selected entry, or the entries | ||||
| 	*/ | ||||
| 	constructor(DATA) { | ||||
| 		super(DATA, { | ||||
| 			"_id": "ID", | ||||
| 			"username": "name" | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| module.exports = User; | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue