database-js MySQL2 Adapter

1.0.0 · abandoned · verified Wed Apr 22

database-js-mysql2 is an adapter that provides a MySQL2 driver for the database-js abstraction layer. It wraps the `mysql2` package, allowing `database-js` connections to interact with MySQL databases using the `mysql2://` connection string protocol. Additionally, it offers a standalone, Promise-based API for `mysql2` directly, which can be useful for those who want a simple Promise interface without the full `database-js` ecosystem. The package is currently at version 1.0.0. Given its last commit in 2018, its release cadence is effectively ceased, and it should be considered abandoned, potentially lacking updates for modern Node.js versions or security patches. Its primary differentiator is its integration with `database-js` and its out-of-the-box Promise support for `mysql2`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to MySQL using `database-js` with the `database-js-mysql2` driver, including parameterized queries and SSL configuration via connection string. Uses environment variables for credentials.

const Database = require('database-js').Connection;
const fs = require('fs');

// Dummy 'fs' for example to make it runnable without actual files
// In a real scenario, these files would exist on disk.
const caCertPath = './path/to/ca.pem';
const keyPath = './path/to/key.pem';
const certPath = './path/to/cert.pem';

// Mock fs.existsSync and fs.readFileSync for demonstration purposes
const originalExistsSync = fs.existsSync;
const originalReadFileSync = fs.readFileSync;
fs.existsSync = (path) => path === caCertPath || path === keyPath || path === certPath;
fs.readFileSync = (path) => Buffer.from(`mock_certificate_for_${path}`);

(async () => {
    let connection, statement, rows;
    const dbUser = process.env.DB_USER ?? 'my_secret_username';
    const dbPass = process.env.DB_PASSWORD ?? 'my_secret_password';
    const dbHost = process.env.DB_HOST ?? 'localhost';
    const dbPort = process.env.DB_PORT ?? 3306;
    const dbName = process.env.DB_NAME ?? 'my_top_secret_database';

    // Example connection string with SSL parameters, ensuring paths are URL-encoded
    // In a real application, ensure your SSL files are correctly placed and permissions set.
    const connectionString = `mysql2://${dbUser}:${dbPass}@${dbHost}:${dbPort}/${dbName}?ssl[ca]=${encodeURIComponent(caCertPath)}&ssl[key]=${encodeURIComponent(keyPath)}&ssl[cert]=${encodeURIComponent(certPath)}`;

    try {
        connection = new Database(connectionString);
        statement = await connection.prepareStatement("SELECT ? AS user_name_col, ? AS value_col");
        rows = await statement.query('not_so_secret_user', 123);
        console.log('Query Result:', rows);
    } catch (error) {
        console.error('Database Error:', error);
    } finally {
        if (connection) {
            await connection.close();
        }
    }
})();

// Restore original fs methods if necessary in a larger application context
fs.existsSync = originalExistsSync;
fs.readFileSync = originalReadFileSync;

view raw JSON →