Connect Using a Single Connection

A single connection is a direct, individual connection between the application and the NuoDB database. This connection is used for executing SQL queries and managing the interaction between the application and the database. It is suitable for simple applications or scenarios with low concurrency. NuoDB Node.js driver exposes driver.connect() to directly create a single connection against a NuoDB database.

To connect to the NuoDB database using a single connection:

  1. Define a configuration object

    To connect to a NuoDB database, provide a configuration object which includes the Connection Properties. Provide string values for each of the connection properties.

    With the configuration object defined, connect to the desired NuoDB database.

  2. Connect to a NuoDB database using configuration object using connect()

  3. Execute SQL queries

    Execute SQL queries using the execute() method of the connection object. Use getRows() to obtain the rows from the result set.

    const results = await connection.execute("SELECT * FROM SYSTEM.NODES;");
    const rows = await results.getRows();
    If the default schema was defined in the configuration map, do not specify the schema in the SQL query.
  4. Close result set

    Close a result set after using any fetch or cursor.

    await results.close();
  5. Close the connection

    Close the connection to release memory. This step ensures proper cleanup.

    await connection.close();

Examples

Example 1: Connect to NuoDB database using a single connection
import { Driver } from "nuodb";

const config = {              //define the configuration object
    database: `test`,
    password: "dba",
    user: "dba",
    port: "48004",
    schema: "USER"
};

async function () {
   const driver = new Driver();
   let connection = null;
   let results = null;

   try {
      connection = await driver.connect(config); //connect to the database
      results = await connection.execute("SELECT * FROM SYSTEM.NODES; "); //execute SQL commands
      const rows = await results.getRows();

      await results.close(); //Close result set

   } catch (err) {/* handle error */}

   finally {
      await results?.close().catch(err => { console.error(err) });
      await connection?.close().catch(err => { console.error(err) });
   }
}
In this example auto commit is on by default. Set auto commit through the connection configuration or SQL directive.
Example 2: Create a single connection against a NuoDB database using callback.
let rows;
const driver = new Driver();

driver.connect(config, (err, connection) => {
   if (err) {/* handle connection error */}

   connection.execute("SELECT * FROM SYSTEM.NODES;", (err, results) => {
      if (err) {/* handle execution error */}

      results.getRows((err, rows) => {
         if (err) {/* handle getting rows error */}

      });

      results.close(err => {
         if (err) {/* handle result set closure error */}
      })
   });

   connection.close((err) => {
      if (err) {/* handle closure error */}
   });
});
Example 3: Create a single connection against a NuoDB database using Promise semantics.
let rows;
const driver = new Driver();

driver.connect(config)
   .then(connection => {

      connection.execute("SELECT * FROM SYSTEM.NODES;")
         .then(results => {

            results.getRows()
               .then(rows => { /* use rows */ })
               .catch(err => { /* handle getting rows error */ });

            results.close()
               .catch(err => { /* handle results closure error */ })
         })
         .catch(err => { /* handle SQL execution error */ });

      connection.close()
         .catch(err => { /* handle connection closure error */ })

   })
   .catch(err => { /* handle connection error */ })