database-js MySQL2 Adapter
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
-
Error: Cannot find module 'database-js'
cause The `database-js` core package is not installed, but `database-js-mysql2` expects it when used as a driver.fixInstall the `database-js` package: `npm install database-js`. -
TypeError: Cannot read properties of undefined (reading 'Connection')
cause Attempting to destructure `Connection` from `require('database-js')` when `database-js` might not be correctly installed or resolved, or `Connection` is not a named export in an unexpected scenario.fixVerify `npm list database-js` shows the package, and ensure the import is `const Database = require('database-js').Connection;` if you are using the older `database-js` pattern. -
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host' (using password: YES/NO)
cause Incorrect MySQL username, password, or host settings in the connection string. The database server rejected the authentication attempt.fixDouble-check your `DB_USER`, `DB_PASSWORD`, `DB_HOST`, and `DB_PORT` environment variables or hardcoded values. Ensure the user has correct privileges for the specified database.
Warnings
- breaking This project appears to be abandoned, with the last commit in 2018. It is unlikely to receive updates for new Node.js versions, security patches, or compatibility with newer `mysql2` releases, posing potential stability and security risks.
- gotcha The package exclusively uses CommonJS `require()` syntax. It does not natively support ES Modules (`import/export`) and attempting to use them will result in syntax errors or module resolution failures.
- gotcha SSL certificate files (ca, key, cert) specified in the connection string are read synchronously using `fs.readFileSync`. In high-performance or serverless environments, this synchronous I/O can block the Node.js event loop, potentially impacting application responsiveness during connection setup.
Install
-
npm install database-js-mysql2 -
yarn add database-js-mysql2 -
pnpm add database-js-mysql2
Imports
- mysql
import mysql from 'database-js-mysql2';
const mysql = require('database-js-mysql2'); - Connection
import { Connection } from 'database-js';const { Connection } = require('database-js'); - Database
import Database from 'database-js/Connection';
const Database = require('database-js').Connection;
Quickstart
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;