MariaDB Connector/Node.js First Beta Now Available

We are pleased to announce the immediate availability of MariaDB Connector/Node.js beta version 2.0.1. This is a non-blocking MariaDB client for Node.js, 100 percent JavaScript and compatible with Node.js 6+.

Connector/Node.js offers new functionalities like “Insert streaming”, “pipelining”, batching (see previous blog post).

Version 2.0.1 is adding the new features :

  • Batching
  • Failover / load balancing via multi-node configuration

Batching

Some use cases require a large amount of data to be inserted into a database table. By using batch processing, these queries can be sent to the database in one call, thus improving performance.

A new batch API is dedicated for those use case.

For instance, say you want to create a basket with five items :

connection.beginTransaction();
connection.query("INSERT INTO BASKET(customerId) values (?)", [1], (err, res) => {
  //must handle error if any
  const basketId = res.insertId;
  try {
    connection.batch("INSERT INTO basket_item(basketId, itemId) VALUES (?, ?)",[
        [basketId, 100],
        [basketId, 101],
        [basketId, 103],
        [basketId, 104],
        [basketId, 105]
    ]);
    //must handle error if any
    connection.commit();
  } catch (err) {
    connection.rollback();
    //handle error
  }
});

The implementation will differ according to the server version (batching will use a MariaDB dedicated protocol if server version is >= 10.2.7).

Comparison with popular Node.js clients using MariaDB 10.3 server:

Multi-node configuration

MariaDB Connector/Node.js now supports multi-node configuration, which is adding failover and load balancing capabilities to the connector and therefore the application.

The API is common to node.js connector mysql and mysql2.

The configuration is done through a Cluster Object that configures different pools and provides the API to choose pools according to a tag name with regex and according to the selector.

A set of selectors are predefined :

  • Round-robin
  • Random
  • Sequence (always use in predefined order)

Basic example:

const mariadb = require('mariadb');
const cluster = mariadb.createPoolCluster();
cluster.add("master", {
  host: 'mydb1.com',
  user: 'myUser',
  password: 'myPwd',
  connectionLimit: 5
});
cluster.add("slave1", {
  host: 'mydb2.com',
  user: 'myUser',
  password: 'myPwd',
  connectionLimit: 5
});
cluster.add("slave2", {
  host: 'mydb3.com',
  user: 'myUser',
  password: 'myPwd',
  connectionLimit: 5
});

//getting a connection from slave1 or slave2 using round-robin
cluster.getConnection(/^slave*$, "RR")
  .then(conn => {
    return conn.query("SELECT * FROM myTable")
      .then(row => {
        conn.release();
        return row.length;
      })
      .finally(() => {
        conn.release();
      });
  });

See the API for more information.

This is a beta release and it should not be used in production. We encourage you to test MariaDB Connector/Node.js and share your feedback with us.

Release Notes

Download Now NPM