I am working on a application that is made completely in JavaScript (other than the database) and I've created a simple user management system for the application.
This user management system creates a query to the database containing the user info (the database contains the user info not the query) based on a few command line arguments, decrypts the data in the database, and then prints the result to the console.
I have been programming for 2 years but am self-taught and was looking for some feedback/criticism on my code.
Note: Some of the modules in the Main script are not included but, for the most part, their functions should be intuitive.
Edit: for more information about the DataBase
class see this question.
Here is my code:
Main script:
// this is a instance of a `DataBase` class:
const users = require('../../Code/MiddleWare/Database-comms/createUser').users
const lookup = require('./lookup')
const decryptAll = require('./decrypt')
void async function() {
if(process.argv[2] === '-e') {
console.dir(decryptAll(await lookup('Email', process.argv[3], users, process.argv[4] === '-ex')), {colors: true, depth: Infinity})
} else if(process.argv[2] ? process.argv[2].match(/^\d+$/) : false) {
console.dir(decryptAll(await lookup('ID', parseInt(process.argv[2]), users, process.argv[3] === '-ex', false)), {colors: true, depth: Infinity})
} else {
console.dir(decryptAll(await lookup('Username', process.argv[2] ? process.argv[2] : '', users, process.argv[3] === '-ex')), {colors: true, depth: Infinity})
}
}()
"lookup" module:
module.exports = async function(type, string, users, exact, encrypted = true) {
const aes = require('../../Code/MiddleWare/Security/index').encryption.AES
const retval = []
await users.query('Users').then((data) => {
for(let i of data) {
if(encrypted ? (exact? aes.decrypt(i[type]) === string : aes.decrypt(i[type]).match(string)) : i[type] === string) {
retval.push(i)
}
}
})
return retval.length === 0 ? false : retval
}
The "decrypt" module:
const decrypt = require('../../Code/MiddleWare/Security/encryption').AES.decrypt
module.exports = function(array) {
if(!array) {
return false
}
const retval = []
function decryptAll(obj) {
return {ID: obj.ID, Username: decrypt(obj.Username), Email: decrypt(obj.Email), Data: decrypt(obj.Data), Password: obj.Password}
}
for(const i of array) {
const item = decryptAll(i)
item.Data = JSON.parse(item.Data)
retval.push(item)
}
return retval
}