{"id":16332,"library":"database-js-mysql2","title":"database-js MySQL2 Adapter","description":"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`.","status":"abandoned","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/esteban-serfe/database-js-mysql2","tags":["javascript","database-js","mysql","mysql2"],"install":[{"cmd":"npm install database-js-mysql2","lang":"bash","label":"npm"},{"cmd":"yarn add database-js-mysql2","lang":"bash","label":"yarn"},{"cmd":"pnpm add database-js-mysql2","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as the core abstraction layer when using this package as a database-js driver.","package":"database-js","optional":true},{"reason":"The underlying MySQL client library that this package wraps.","package":"mysql2","optional":false}],"imports":[{"note":"The package primarily targets CommonJS environments. ESM imports are not directly supported.","wrong":"import mysql from 'database-js-mysql2';","symbol":"mysql","correct":"const mysql = require('database-js-mysql2');"},{"note":"When used with `database-js`, the Connection class is imported from the parent `database-js` package, which also targets CommonJS.","wrong":"import { Connection } from 'database-js';","symbol":"Connection","correct":"const { Connection } = require('database-js');"},{"note":"Common aliasing pattern for the `database-js` Connection class in CommonJS.","wrong":"import Database from 'database-js/Connection';","symbol":"Database","correct":"const Database = require('database-js').Connection;"}],"quickstart":{"code":"const Database = require('database-js').Connection;\nconst fs = require('fs');\n\n// Dummy 'fs' for example to make it runnable without actual files\n// In a real scenario, these files would exist on disk.\nconst caCertPath = './path/to/ca.pem';\nconst keyPath = './path/to/key.pem';\nconst certPath = './path/to/cert.pem';\n\n// Mock fs.existsSync and fs.readFileSync for demonstration purposes\nconst originalExistsSync = fs.existsSync;\nconst originalReadFileSync = fs.readFileSync;\nfs.existsSync = (path) => path === caCertPath || path === keyPath || path === certPath;\nfs.readFileSync = (path) => Buffer.from(`mock_certificate_for_${path}`);\n\n(async () => {\n    let connection, statement, rows;\n    const dbUser = process.env.DB_USER ?? 'my_secret_username';\n    const dbPass = process.env.DB_PASSWORD ?? 'my_secret_password';\n    const dbHost = process.env.DB_HOST ?? 'localhost';\n    const dbPort = process.env.DB_PORT ?? 3306;\n    const dbName = process.env.DB_NAME ?? 'my_top_secret_database';\n\n    // Example connection string with SSL parameters, ensuring paths are URL-encoded\n    // In a real application, ensure your SSL files are correctly placed and permissions set.\n    const connectionString = `mysql2://${dbUser}:${dbPass}@${dbHost}:${dbPort}/${dbName}?ssl[ca]=${encodeURIComponent(caCertPath)}&ssl[key]=${encodeURIComponent(keyPath)}&ssl[cert]=${encodeURIComponent(certPath)}`;\n\n    try {\n        connection = new Database(connectionString);\n        statement = await connection.prepareStatement(\"SELECT ? AS user_name_col, ? AS value_col\");\n        rows = await statement.query('not_so_secret_user', 123);\n        console.log('Query Result:', rows);\n    } catch (error) {\n        console.error('Database Error:', error);\n    } finally {\n        if (connection) {\n            await connection.close();\n        }\n    }\n})();\n\n// Restore original fs methods if necessary in a larger application context\nfs.existsSync = originalExistsSync;\nfs.readFileSync = originalReadFileSync;\n","lang":"javascript","description":"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."},"warnings":[{"fix":"Consider migrating to a actively maintained `database-js` driver if available, or a direct `mysql2` client for more current support.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure your project or file uses CommonJS `require()` statements for importing `database-js-mysql2` and `database-js`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be aware of the synchronous file access. For critical applications, pre-loading certificates or using an alternative driver that supports asynchronous certificate loading might be preferable if this becomes a performance bottleneck.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Install the `database-js` package: `npm install database-js`.","cause":"The `database-js` core package is not installed, but `database-js-mysql2` expects it when used as a driver.","error":"Error: Cannot find module 'database-js'"},{"fix":"Verify `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.","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.","error":"TypeError: Cannot read properties of undefined (reading 'Connection')"},{"fix":"Double-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.","cause":"Incorrect MySQL username, password, or host settings in the connection string. The database server rejected the authentication attempt.","error":"Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host' (using password: YES/NO)"}],"ecosystem":"npm"}