mysql-all-in-one

4.10.4 · active · verified Wed Apr 22

mysql-all-in-one is a comprehensive wrapper library for interacting with MySQL databases in Node.js, built on top of the robust `mysql2` package. Currently at version 4.10.4, it provides a high-level Data Access Object (DAO) for executing common database operations like SELECT, INSERT, UPDATE, DELETE, and UPSERT. A core feature is its inherent prevention of SQL injection vulnerabilities through the consistent use of prepared statements. The package also includes a standalone Query Builder for constructing raw SQL queries programmatically, offering advanced capabilities like complex WHERE clause nesting and custom SQL expressions. Key functionalities extend to creating database dumps and simplifying multi-database interactions. While the README doesn't specify a precise release cadence, its active development and recent version history suggest ongoing maintenance. Its primary differentiator lies in offering a simplified, secure, and opinionated interface over `mysql2`, ideal for applications requiring robust database interactions without direct exposure to raw SQL string manipulation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a database connection using the DataAccessObject, create a table, insert data, and perform basic SELECT queries. It uses environment variables for secure credential handling.

import { DataAccessObject } from 'mysql-all-in-one';

const dao = new DataAccessObject({
  host: process.env.DB_HOST ?? 'localhost',
  user: process.env.DB_USER ?? 'root',
  password: process.env.DB_PASSWORD ?? '1234',
  port: Number(process.env.DB_PORT ?? 3306),
  database: process.env.DB_DATABASE ?? 'test_database'
});

const main = async () => {
  try {
    // Example: Create a table if it doesn't exist
    await dao.query('CREATE TABLE IF NOT EXISTS my_table (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)');
    console.log('Table my_table ensured.');

    // Example: Insert data
    await dao.insert({ into: 'my_table', values: { name: 'Alice', age: 30 } });
    await dao.insert({ into: 'my_table', values: { name: 'Bob', age: 24 } });
    console.log('Inserted sample data.');

    // Example: Select all rows
    const result = await dao.select({ from: 'my_table' });
    console.log('All rows:', result);

    // Example: Select with a WHERE clause
    const specificResult = await dao.select({ from: 'my_table', where: { name: 'Alice' } });
    console.log('Alice data:', specificResult);

  } catch (error) {
    console.error('Database operation failed:', error);
  } finally {
    // Disconnect if the DAO doesn't handle pooling internally (or if explicit close is needed)
    // The current DAO design maintains connection, explicit close might depend on `mysql2`'s pooling behavior.
    // For simple scripts, allowing process to exit is common.
  }
};

main();

view raw JSON →