For the complete documentation index, see llms.txt. This page is also available as Markdown.

Getting Started With the Node.js Connector

Complete Node.js connector guide for MariaDB. Complete reference for installation, connection pooling, query execution, and error handling for production use.

The MariaDB Node.js Connector is available through the Node.js repositories. You can install it using npm:

npm install mariadb

Using ECMAScript, prior to 2017:

const mariadb = require('mariadb');
const pool = mariadb.createPool({
     host: 'mydb.com', 
     user:'myUser', 
     password: 'myPassword',
     connectionLimit: 5
});
pool.getConnection()
    .then(conn => {
    
      conn.query("SELECT 1 as val")
        .then((rows) => {
          console.log(rows); //[ {val: 1}, meta: ... ]
          //Table must have been created before 
          // " CREATE TABLE myTable (id int, val varchar(255)) "
          return conn.query("INSERT INTO myTable value (?, ?)", [1, "mariadb"]);
        })
        .then((res) => {
          console.log(res); // { affectedRows: 1, insertId: 1, warningStatus: 0 }
          conn.end();
          pool.end();
        })
        .catch(err => {
          //handle error
          console.log(err); 
          conn.end();
          pool.end();
        })
        
    }).catch(err => {
      //not connected
      pool.end();
    });

Using ECMAScript 2017:

The MariaDB Connector has a Promise (Default) and callback API.

Typescript specific docs are also available.

spinner

Last updated

Was this helpful?