Technology Used | Resource URL |
---|---|
JavaScript | https://developer.mozilla.org/en-US/docs/Web/JavaScript |
Node.js | https://developer.mozilla.org/en-US/docs/Glossary/Node.js |
MySQL | https://dev.mysql.com/doc/ |
This application allows users to manage their employee database from the command line. Add, update, and delete employee and organization data at will.
View the walkthrough video here
The below function queries the employee database to add new role based on user input. The inquirer department list dropdown is generated by querying all existing departments in the org and converting the returned object into an array. Using async/await allows us to correctly resolve the returned promise before the list is displayed to the user.
const addRole = async () => {
let availableDepartments = [];
const results = await db.promise().query("SELECT department_id, department_name FROM department;");
for (let i = 0; i < results[0].length; i++) {
availableDepartments.push(results[0][i].department_id + " - " + results[0][i].department_name);
};
const res = await inquirer
.prompt([
{
type: "input",
message: "What is the title of the role you would like to add?",
name: "roleTitle"
},
{
type: "input",
message: "What is the salary of the role you would like to add?",
name: "roleSalary"
},
{
type: "list",
message: "What is the department of the role you would like to add?",
name: "roleDepartmentId",
choices: availableDepartments
}
]);
await db.promise().query("INSERT INTO roles (title, salary, department_id) VALUES (?, ?, ?);", [res.roleTitle, res.roleSalary, res.roleDepartmentId.substr(0, res.roleDepartmentId.indexOf(" "))]);
return "Role added successfully!";
};