What is mariadb?
The mariadb npm package is a Node.js connector for MariaDB, a popular open-source relational database. It allows you to interact with MariaDB databases using JavaScript, providing functionalities for connecting to the database, executing queries, managing transactions, and handling connection pools.
What are mariadb's main functionalities?
Connecting to the Database
This code demonstrates how to create a connection pool and connect to a MariaDB database using the mariadb package. It establishes a connection and logs a message upon successful connection.
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user: 'yourUsername',
password: 'yourPassword',
database: 'yourDatabase'
});
async function connect() {
let conn;
try {
conn = await pool.getConnection();
console.log('Connected to the database');
} catch (err) {
throw err;
} finally {
if (conn) conn.end();
}
}
connect();
Executing Queries
This code sample shows how to execute a simple SELECT query using the mariadb package. It retrieves all rows from a specified table and logs them to the console.
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user: 'yourUsername',
password: 'yourPassword',
database: 'yourDatabase'
});
async function executeQuery() {
let conn;
try {
conn = await pool.getConnection();
const rows = await conn.query('SELECT * FROM yourTable');
console.log(rows);
} catch (err) {
throw err;
} finally {
if (conn) conn.end();
}
}
executeQuery();
Managing Transactions
This code demonstrates how to manage transactions using the mariadb package. It begins a transaction, executes an INSERT query, and commits the transaction. If an error occurs, the transaction is rolled back.
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user: 'yourUsername',
password: 'yourPassword',
database: 'yourDatabase'
});
async function manageTransaction() {
let conn;
try {
conn = await pool.getConnection();
await conn.beginTransaction();
await conn.query('INSERT INTO yourTable (column1, column2) VALUES (?, ?)', [value1, value2]);
await conn.commit();
console.log('Transaction committed');
} catch (err) {
if (conn) await conn.rollback();
console.error('Transaction rolled back', err);
} finally {
if (conn) conn.end();
}
}
manageTransaction();
Handling Connection Pools
This code sample shows how to handle connection pools with the mariadb package. It sets a connection limit and demonstrates how to use the pool to execute a query.
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user: 'yourUsername',
password: 'yourPassword',
database: 'yourDatabase',
connectionLimit: 5
});
async function usePool() {
let conn;
try {
conn = await pool.getConnection();
const rows = await conn.query('SELECT * FROM yourTable');
console.log(rows);
} catch (err) {
throw err;
} finally {
if (conn) conn.end();
}
}
usePool();
Other packages similar to mariadb
mysql
The mysql package is a popular Node.js connector for MySQL databases. It offers similar functionalities to mariadb, such as connecting to the database, executing queries, and managing transactions. However, it is specifically designed for MySQL databases, whereas mariadb is tailored for MariaDB.
mysql2
The mysql2 package is another Node.js connector for MySQL databases. It is a more modern and faster alternative to the mysql package, with support for Promises and async/await. Like mariadb, it provides functionalities for connecting to the database, executing queries, and managing transactions.
pg
The pg package is a Node.js connector for PostgreSQL databases. While it serves a different database system, it offers similar functionalities to mariadb, such as connecting to the database, executing queries, and managing transactions. It is a good alternative if you are using PostgreSQL instead of MariaDB.
Security holding package
This package name is not currently in use, but was formerly occupied
by another package. To avoid malicious use, npm is hanging on to the
package name, but loosely, and we'll probably give it to you if you
want it.
You may adopt this package by contacting support@npmjs.com and
requesting the name.