Registry / database / mysql-async-simple

mysql-async-simple

JSON →
library1.0.3jsnpmunverified

A minimal async/await wrapper around the standard `mysql` package for Node.js (v1.0.3). It provides three simple functions: `makeDb`, `db.connect`, `db.query`, and `db.close`. Unlike alternatives like `mysql2` or `mysql2/promise`, this library keeps the familiar `mysql` API but returns promises. It has no additional dependencies beyond `mysql`, but does not support TypeScript types out-of-the-box. Low release cadence; last update was in 2020.

npm install mysql-async-simple
INSTALL
IMPORT
SIG · MYSQL-ASYNC-SIMPLE
M
mysql-async-simple
databasejavascriptv1.0.3
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.

makeDb
✓ const { makeDb } = require('mysql-async-simple');
✗ import { makeDb } from 'mysql-async-simple';
This package is CommonJS-only. ESM import syntax (e.g. in TypeScript with esModuleInterop) may not work. Use require or set up a bundler accordingly.
db
✓ const db = makeDb();
✗ const db = new makeDb();
makeDb() is a factory function, not a constructor. Calling it with 'new' will throw a TypeError.
query
✓ const rows = await db.query(connection, sql);
✗ const rows = await db.query(sql);
Unlike the original mysql connection, the query method requires the connection object as the first argument. Forgetting it will pass the SQL string as the connection and cause a runtime error.

Shows async/await usage with mysql-async-simple: create connection, wrap with makeDb(), connect, query, close.

const { makeDb } = require('mysql-async-simple'); const mysql = require('mysql'); const connection = mysql.createConnection({ host: process.env.HOST ?? 'localhost', user: process.env.USER ?? 'root', password: process.env.PASSWORD ?? '', database: process.env.DB ?? 'test' }); const db = makeDb(); async function run() { try { await db.connect(connection); const users = await db.query(connection, 'SELECT * FROM users'); console.log(users); } catch (err) { console.error(err); } finally { await db.close(connection); } } run();
Debug
Known issues
gotchaTypeError: connection.query is not a function when calling db.query() with missing connection argument.
fix
Always pass the connection object as first argument: await db.query(connection, sql).
affects: >=1.0.0
gotchaTypeError: db.connect is not a function if you forget to call makeDb() first.
fix
Call const db = makeDb() before using db.connect().
affects: >=1.0.0
gotchaError: Cannot find module 'mysql' if mysql package is not installed.
fix
Install the peer dependency: npm install mysql mysql-async-simple
affects: >=1.0.0
deprecatedThe MySQL npm package (mysql) is no longer maintained. Consider using mysql2 for better performance and ongoing support.
fix
Switch to mysql2 (which has built-in promise support) or use mysql-async-simple with mysql2 if compatible, but prefer native promise API.
affects: >=1.0.0
gotchaCannot use import syntax in pure Node.js without transpilation.
fix
Use require() or transpile with Babel/TypeScript.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: db.query is not a function
Attempting to call query directly on the connection object created by mysql, not on the wrapped db object.
fix
First call const db = makeDb(), then use db.query(connection, sql).
Error: connect ECONNREFUSED 127.0.0.1:3306
MySQL server not running or connection config incorrect.
fix
Start MySQL service or check host/port in connection options.
TypeError: Cannot read property 'query' of undefined
The db variable is undefined because makeDb() returned undefined (e.g., if you forgot to assign it).
fix
Ensure const db = makeDb(); is executed successfully.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
mysqlrequiredPeer dependency: the library wraps the mysql client and requires it to be installed separately.
Agent activity
6 hits · last 30 days
node
6
Resources
mysql-async-simple — npm install mysql-async-simple · libregistry