database-js-mysql
database-js-mysql is a wrapper for the `mysql` package, primarily designed to integrate MySQL functionality into applications using the `database-js` abstraction layer. It provides a promise-based API for handling MySQL connections and executing queries, offering both standalone usage and integration with the `database-js` Connection class. As of its latest stable version, 1.1.3, released over seven years ago, the package relies on CommonJS modules and older JavaScript syntax. Its key differentiator was providing a consistent `database-js` interface for MySQL, including support for parameterized queries and streamlined connection management, which was useful for abstracting database interactions in its ecosystem.
Common errors
-
ERR_REQUIRE_ESM: Must use import to load ES Module
cause Attempting to import `database-js-mysql` using `import` statement in an ES Module context.fixChange your import statement to `const mysql = require('database-js-mysql');` and ensure your project is configured for CommonJS, or use a tool like Webpack/Rollup to transpile. -
Error: connect ECONNREFUSED
cause The MySQL server is not running, is configured to listen on a different port/host, or firewall rules are blocking the connection.fixVerify that the MySQL server is running, check the `Hostname`, `Port`, `Username`, and `Password` in your connection options, and ensure no firewalls are preventing access. -
ER_ACCESS_DENIED_ERROR: Access denied for user '...'@'localhost' (using password: YES/NO)
cause Incorrect username or password provided in the connection options, or the user lacks necessary permissions for the database.fixDouble-check your `Username` and `Password` in the connection object. Ensure the MySQL user has privileges to connect from the specified host and access the target database. -
TypeError: connection.query is not a function
cause This error often occurs if `mysql.open()` was not `await`ed or if the `connection` object somehow became `undefined` or not the expected promise-wrapped connection.fixEnsure `let connection = await mysql.open({...});` is used to properly resolve the promise and get the connection object before calling methods on it.
Warnings
- breaking The project appears to be abandoned, with no updates or commits in over seven years. This means it is unlikely to support modern Node.js versions, fix security vulnerabilities, or adopt new JavaScript features (e.g., native ESM).
- gotcha This package is CommonJS-only and does not support native ES Modules (`import`/`export`). Attempting to `import` from it will result in errors.
- gotcha The underlying `mysql` package (version ~2.10.x at the time of this wrapper's last update) may have known vulnerabilities or compatibility issues with modern MySQL server versions or Node.js runtimes. These issues will not be patched by `database-js-mysql`.
- gotcha SSL configuration requires file paths (e.g., `ssl[ca]`) to be correctly specified and readable by the Node.js process. Incorrect paths or permissions will lead to connection failures that can be difficult to debug.
Install
-
npm install database-js-mysql -
yarn add database-js-mysql -
pnpm add database-js-mysql
Imports
- mysql
import mysql from 'database-js-mysql';
var mysql = require('database-js-mysql'); - Connection
import { Connection as Database } from 'database-js';var Database = require('database-js').Connection; - mysql.open
let connection = mysql.open({...});
Quickstart
const mysql = require('database-js-mysql');
(async () => {
let connection, rows;
try {
connection = mysql.open({
Hostname: process.env.DB_HOST ?? 'localhost',
Port: parseInt(process.env.DB_PORT ?? '3306', 10),
Username: process.env.DB_USERNAME ?? 'my_secret_username',
Password: process.env.DB_PASSWORD ?? 'my_secret_password',
Database: process.env.DB_DATABASE ?? 'my_top_secret_database'
});
// Ensure the connection is established before querying
rows = await connection.query("SELECT * FROM tablea WHERE user_name = 'not_so_secret_user'");
console.log('Query result:', rows);
} catch (error) {
console.error('Database operation failed:', error);
} finally {
if (connection) {
await connection.close();
console.log('Connection closed.');
}
}
})();