26 lines
716 B
JavaScript
26 lines
716 B
JavaScript
import client from "./db-client.js";
|
|
import prompt from "prompt-sync";
|
|
|
|
const promptSync = prompt();
|
|
|
|
try {
|
|
await client.connect();
|
|
|
|
const departmentName = promptSync("Enter the name of the department: ").trim();
|
|
|
|
if (!departmentName) {
|
|
throw new Error("Department name cannot be empty.");
|
|
}
|
|
|
|
// Parameterized queries keep user input separate from SQL.
|
|
const result = await client.query("INSERT INTO department (name) VALUES ($1) RETURNING id, name", [departmentName]);
|
|
const newDepartment = result.rows[0];
|
|
|
|
console.log(`New department created with ID ${newDepartment.id}: ${newDepartment.name}`);
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
process.exitCode = 1;
|
|
} finally {
|
|
await client.end();
|
|
}
|