After installing the driver, you will need an IP of the cluster and user credentials. These will be
used in the same way as with other databases.In the following example, you’ll find code that performs operations similar to those described in
the Regatta Platform section.To begin, import the relevant methods using the ‘node-regatta’require statement:
Copy
Ask AI
const {getConnection} = require("node-regatta");
Please ensure that you import all relevant classes and methods based on your application’s
functionality and requirements. Next, add the IP address and credentials obtained earlier (see
External Access to the Regatta Database):
Any code that performs SQL commands should be updated accordingly.To help you get acquainted with the Regatta API for Node.js, we’ve provided a simple code
example below. Additionally, a more comprehensive example is included in the driver package
under examples/node_regatta_examples.js. These documented examples demonstrate
common tasks such as creating connections, executing queries, and working with cursors. The
Regatta API supports both promise-based and callback-based semantics.Unlike the Regatta Platform, in the commands used by the driver we need to specify the device
mapping when creating a table. In the example below we will use a single device.Below is an example of some of the code.Note that a more robust example is also available as part of the downloaded driver.
Copy
Ask AI
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 tableconst 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();
Figure 51 Node.js example of application using driverFollowing is the output of the application:
Figure 52 Node.js example output
Note that all commands performed in the Regatta Platform section can be performed in the
same manner using a Node.js driver.