Registry / database / mysql-dbhelper

mysql-dbhelper

JSON →
library0.3.2jsnpmunverified

A thin wrapper around node-mysql (the mysql npm package) that provides a set of convenience methods for common database operations such as execute, executeScalar, executeFirstRow, executeNonQuery, insert, update, and exist. Version 0.3.2 is the latest release. The library automatically manages connection closing when methods prefixed with '$' are used, and auto-closes on errors. It is designed for use with CoffeeScript callbacks and does not support Promises or async/await. It was last updated in 2018 and is considered stable but in maintenance mode. Compared to modern libraries like knex or typeorm, it lacks query builders, ORM features, and TypeScript support.

npm install mysql-dbhelper
INSTALL
IMPORT
SIG · MYSQL-DBHELPER
M
mysql-dbhelper
databasejavascriptv0.3.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default factory
const dbHelper = require('mysql-dbhelper')(options);
import dbHelper from 'mysql-dbhelper'; (ESM import not supported)
Library is CommonJS only; it exports a factory function that takes options and returns an object with createConnection(). Call require('mysql-dbhelper')(options).
createConnection
const conn = dbHelper.createConnection();
const conn = new dbHelper.createConnection(); (not a constructor)
createConnection() returns a connection object that has methods like execute, insert, etc.
execute (auto-close variant)
conn.$execute(sql, params, callback);
conn.execute(sql, params, callback); (if auto-close is desired)
Methods prefixed with '$' auto-close the connection after the operation. Non-prefixed methods require manual conn.end().
Coercion of params
conn.execute(sql, callback); // params omitted if none
conn.execute(sql, undefined, callback); (passes undefined as second arg, may cause errors)
The 'params' argument is optional and can be omitted entirely. Do not pass null or undefined.

Connects to MySQL using 'mysql-dbhelper', executes a parameterized query to fetch the name of a user by ID, and logs the result. The connection is auto-closed due to the '$' prefix.

const dbHelper = require('mysql-dbhelper')({ dbConfig: { host: 'localhost', user: 'root', port: 3306, password: process.env.DB_PASSWORD ?? '', database: 'test' }, debug: false, timeout: 30 }); const conn = dbHelper.createConnection(); const sql = 'SELECT name FROM users WHERE id = ?'; const userId = 1; conn.$executeScalar(sql, [userId], function(err, name) { if (err) { console.error(err); return; } console.log('User name:', name); });
Debug
Known issues
gotchaAuto-close behavior: Methods without '$' prefix do NOT auto-close the connection. You must call conn.end() manually, otherwise connections will leak.
fix
Use '$' prefix methods to auto-close, or ensure conn.end() is called in the callback for non-prefixed methods.
affects: >=0.0.0
gotchaError auto-close: If a database error occurs, the connection is automatically closed even for non-prefixed methods. Subsequent operations on the same connection will fail.
fix
Do not reuse a connection after an error. Always create a new connection or handle errors appropriately.
affects: >=0.0.0
deprecatedThe underlying 'mysql' package (node-mysql) is in maintenance mode and does not support modern authentication (e.g., caching_sha2_password default in MySQL 8+).
fix
Use 'mysql2' package instead and switch to a library that supports it, or configure MySQL to use mysql_native_password.
affects: >=0.0.0
gotcha'params' argument must be an array or omitted. Passing a non-array (e.g., string or number) may cause unexpected behavior or errors.
fix
Always wrap parameters in an array: [value]. Omit the argument entirely if there are no parameters.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: dbHelper is not a function
Calling require('mysql-dbhelper') without invoking the returned factory function.
fix
Use const dbHelper = require('mysql-dbhelper')(options);
Cannot read property 'execute' of undefined
Calling methods on the factory result instead of the connection object. conn is undefined.
fix
First call dbHelper.createConnection() to get a connection, then call methods on that connection.
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server
MySQL 8+ uses caching_sha2_password by default, but the 'mysql' package only supports mysql_native_password.
fix
Alter MySQL user: ALTER USER 'user'@'host' IDENTIFIED WITH mysql_native_password BY 'password'; or use a different library.
Upgrade
Version history
0.3.2latest on npm
Audit
Dependencies
mysqlrequiredpeer dependency (node-mysql) – the underlying MySQL driver
Agent activity
4 hits · last 30 days
node
4
Resources
mysql-dbhelper — npm install mysql-dbhelper · libregistry