const {getConnection} = require("node-regatta");
async function main() {
const dbUser = "MyUserName";
const dbPassword = "SomeSophisticatedPassword";
const dbUrl = "aaa:bbb:ccc:ddd:pppp";
try {
// Get a database connection
let connection = await getConnection(dbUser, dbPassword, dbUrl);
console.log("Connected to the database");
// Create the employees table
const createQuery = `
CREATE TABLE employees (
key INT PRIMARY KEY INDEX WITH (devices = (m10d1)),
name VARCHAR(40) NOT NULL,
salary INT,
department VARCHAR(50) NOT NULL
) WITH (devices = (m10d1))
`;
await connection.execute(createQuery);
console.log("Employees table created");
// Insert values into table
const insertQuery = `
INSERT INTO employees (key, name, salary, department) VALUES
(1, 'John Doe', 10932, 'DevOps'),
(2, 'Richard Roe', 18324, 'Legal'),
(3, 'Jane Roe', 20411, 'Sale'),
(4, 'Rachel Roe', 19555, 'Support')
`;
await connection.execute(insertQuery);
console.log("Data inserted into employees table");
// Select all values
const selectQuery = "SELECT * FROM employees";
let cursor = await connection.execute(selectQuery);
// Fetch the results
let result = await cursor.fetchAll();
// Print the results
console.log(result.rows);
// Closing the connection will also close all open cursors.
await connection.close();
console.log("Connection closed");
} catch (error) {
console.error("Error: ", error.message);
}
}
main();