Registry / database / mysql-magic

mysql-magic

JSON →
library0.3.0jsnpmunverified

Promised-based high-level extension for the mysqljs MySQL driver for Node.js >=7.6. Stable version 0.3.0 provides named connection pools, automatic connection release via async scopes, and higher-level query methods like insert, fetchRow, fetchAll. Wraps mysqljs pools with native Promises and async/await, simplifying error handling and connection lifecycle. Differentiators: global named pools, connection scopes with auto-release, high-level query API. Low maintenance cadence; last release 2019.

npm install mysql-magic
INSTALL
IMPORT
SIG · MYSQL-MAGIC
M
mysql-magic
databasejavascriptv0.3.0
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 export (require)
const _db = require('mysql-magic');
import _db from 'mysql-magic'; // ESM not supported
The package does not provide ESM exports; must use require().
getPool
const pool = require('mysql-magic').getPool('dbname');
const pool = getPool('dbname'); // not imported directly
Access via required module; cannot import named export standalone.
getConnection scope
const result = await require('mysql-magic').getConnection('dbname', async function() { ... });
await require('mysql-magic').getConnection('dbname', async () => { ... }); // arrow function breaks 'this' context
Scope callback must be regular function to use this.query(). Arrow function loses this binding.

Shows initializing a named pool and running a query within an auto-releasing connection scope.

const _db = require('mysql-magic'); _db.initPool('mydb', { host: 'localhost', user: 'root', password: process.env.DB_PASSWORD ?? '', database: 'test' }); const numUsers = await _db.getConnection('mydb', async function() { const [rows] = await this.query('SELECT COUNT(*) as count FROM users'); return rows[0].count; }); console.log(numUsers);
Debug
Known issues
breakingNode.js >=7.6 required; async/await used internally.
fix
Upgrade Node.js to >=7.6 or use older version 0.2.x.
affects: <0.3.0
deprecatedPackage has not been updated since 2019; mysqljs may have security issues.
fix
Consider migrating to mysql2 or sequelize for active maintenance.
affects: all
gotchaConnection scope callback must be a regular function, not arrow function, to access this.query.
fix
Use async function() {} instead of async () => {}.
affects: all
Errors
Common errors & fixes
Cannot find module 'mysql'
Missing peer dependency mysql
fix
npm install mysql
this.query is not a function
Using arrow function in connection scope; this is not bound to connection
fix
Use async function() { ... } instead of async () => { ... }
Upgrade
Version history
0.3.0latest on npm
Audit
Dependencies
mysqlrequiredPeer dependency: wraps mysqljs pools and connections
Agent activity
2 hits · last 30 days
node
2
Resources
mysql-magic — npm install mysql-magic · libregistry