Connect with MariaDB Connector/Node.js (Callback API)

Overview

Node.js developers can use MariaDB Connector/Node.js to establish client connections with MariaDB database products.

Require Callback API

MariaDB Connector/Node.js provides two different connection implementations: one built on the Promise API and the other built on the Callback API.

To use the Callback API, use the following module:

const mariadb = require('mariadb/callback');

Connect

createConnection(options) -> Connection is the base function used to create a Connection object.

The createConnection(options) function returns a Connection object.

The commonly used options in createConnection(options) are listed in this table:

Determine the connection information for your MariaDB SkySQL database service:

Option

Description

host

IP address or DNS of the database server. Default is localhost.

host

The fully Qualified Domain Name in the Service Details view

port

Port # to connect database at. Default is 3306.

port

The Read-Write Port or Read-Only Port in the Service Details view

user

User name to connect with database

user

The desired username, which might be the default username in the Service Credentials view

password

User password

password

The user's password, which might be the default password in the Service Credentials view if it was not yet customized

database

Database name to establish a connection to. No default is configured.

connectTimeout

Connection timeout in milliseconds. In Connector/Node.js 2.5.6, the default value changed to 1000. The default value for earlier versions is 10000.

rowsAsArray

A boolean value to indicate whether to return result sets as array instead of the default JSON. Arrays are comparatively faster.

Code Example: Connect

The following code example connects using the database and user account created in the example setup:

const mariadb = require('mariadb/callback');

// Declare async function
function main() {
   let conn;

   try {
      conn = mariadb.createConnection({
         host: "192.0.2.50",
         user: "db_user",
         password: "db_user_password",
         database: "test",
      });

      // Use Connection
      // ...
   } catch (err) {
      // Manage Errors
      console.log("SQL error in establishing a connection: ", err);
   } finally {
      // Close Connection
      if (conn) conn.end(err => {if(err){
         console.log("SQL error in closing a connection: ", err);}
      });
   }
}

main();
const mariadb = require('mariadb/callback');

// Certificate Authority (CA)",
var serverCert = [fs.readFileSync(process.env.SKYSQL_CA_PEM, "utf8")];

// Declare async function
function main() {
   let conn;

   try {
      conn = mariadb.createConnection({
         host: "example.skysql.net",
         port: 5009,
         ssl: { ca: serverCert },
         user: "db_user",
         password: "db_user_password",
         database: "test",
      });

      // Use Connection
      // ...
   } catch (err) {
      // Manage Errors
      console.log("SQL error in establishing a connection: ", err);
   } finally {
      // Close Connection
      if (conn) conn.end(err => {if(err){
         console.log("SQL error in closing a connection: ", err);}
      });
   }
}

main();
  • A try...catch...finally statement is used for exception handling.

  • New connections are created in auto-commit mode by default.

  • When you are done with a connection, close it to free resources. Close the connection using the connection.end([callback]) function.

  • The script calls the connection.end([callback]) function to close/end the connection in the finally block after the queries that are running have completed.

  • The end() function takes a callback function that defines one implicit argument for the Error object if thrown in closing the connection as argument. If no error is generated in closing a connection the Error object is null.