Registry / database / mm_mysql

mm_mysql

JSON →
library2.4.6jsnpmunverified

mm_mysql is a Node.js MySQL library that wraps the mysql2 driver with a clean async/await API, automatic type inference for table creation, built-in connection pooling, and transaction support. Current version is 2.4.6, released with a focus on ease of use. Key differentiators: auto-creates tables from JavaScript object definitions (infers INT, VARCHAR, TEXT, DATETIME, etc.), provides a high-level DB manager with CRUD methods (add, get, set, del), and supports both callback-style transactions and awaitable `beginTransaction`/`commit`/`rollback`. It uses mysql2 under the hood, supports connection pooling, health checks, and debugging mode. Requires Node >=12.0.0.

npm install mm_mysql
INSTALL
IMPORT
SIG · MM_MYSQL
M
mm_mysql
databasejavascriptv2.4.6
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.

Mysql
const { Mysql } = require('mm_mysql');
import { Mysql } from 'mm_mysql';
The package currently does not ship ESM; use CommonJS require().
Mysql (default)
const Mysql = require('mm_mysql').Mysql;
const Mysql = require('mm_mysql');
The default export is not the Mysql class; it's a module with named exports. Using require directly will not work.
Mysql (with destructuring)
const { Mysql } = require('mm_mysql');
const mysql = new Mysql(config);
If you forget destructuring, Mysql will be undefined.

Demonstrates creating a Mysql instance, opening a connection, using DB manager to auto-create a table, inserting a row, and querying all rows.

const { Mysql } = require('mm_mysql'); const mysql = new Mysql({ host: process.env.DB_HOST ?? 'localhost', port: parseInt(process.env.DB_PORT ?? '3306'), user: process.env.DB_USER ?? 'root', password: process.env.DB_PASSWORD ?? '', database: process.env.DB_NAME ?? 'test' }); async function main() { await mysql.open(); const db = mysql.db().new('users', 'id'); // Create table if not exists await db.createTable({ table: 'users', fields: { id: 0, name: '', age: 0, email: '', created_at: new Date() } }, { primary_key: 'id' }); // Insert a user const insertId = await db.add({ name: 'Alice', age: 30, email: 'alice@example.com' }); console.log('Inserted ID:', insertId); // Query users const users = await db.get(); console.log('Users:', users); await mysql.close(); } main().catch(console.error);
Debug
Known issues
gotchaopen() must be called before any database operations; forgetting this causes 'Connection not established' errors.
fix
Always call await mysql.open() before using mysql.db() or other methods.
affects: >=0.0.0
gotchadb().new('table') sets the table for subsequent CRUD operations; you must call new() on each DB manager instance before use.
fix
const db = mysql.db().new('users', 'id');
affects: >=0.0.0
gotchaThe package uses mysql2 internally; if mysql2 is not installed or version mismatched, you may get runtime errors.
fix
Ensure mysql2 is installed: npm install mm_mysql mysql2
affects: >=0.0.0
gotchainit() is a compatibility method that may be removed in future versions; use open() instead.
fix
Replace mysql.init() with await mysql.open();
affects: >=2.0.0
gotchaWhen using transaction(callback), the callback receives a transaction connection object with commit and rollback methods; these must be called inside the callback or the transaction will hang.
fix
Inside the callback, always call either await tx.commit() or await tx.rollback() before returning.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: mysql.open is not a function
Importing the package incorrectly, e.g., using require('mm_mysql') which returns an object, not the Mysql class.
fix
Use destructured import: const { Mysql } = require('mm_mysql');
Error: Connection not established. Call init() or open() first.
Calling database methods before opening the connection.
fix
Always call await mysql.open() before using mysql.db() or any SQL methods.
TypeError: Cannot read properties of undefined (reading 'new')
Calling mysql.db().new() before calling mysql.open(), or mysql.db() returns undefined because connection is not open.
fix
Ensure mysql.open() is called and awaited before calling mysql.db().
Error: Cannot enqueue Handshake after already enqueuing a Handshake
Calling mysql.open() multiple times without closing the connection.
fix
Check if connection is already open; avoid multiple open() calls. Use a singleton pattern or close before reopening.
Upgrade
Version history
2.4.6latest on npm
Audit
Dependencies
mysql2requiredUnderlying MySQL driver; mm_mysql wraps mysql2 for async/await and high-level operations.
Agent activity
4 hits · last 30 days
node
4
Resources
mm_mysql — npm install mm_mysql · libregistry