Registry / database / mysql-activerecord

mysql-activerecord

JSON →
library0.8.6jsnpmunverified

A lightweight MySQL query builder for Node.js built on top of node-mysql. Version 0.8.6 is the latest stable release, with low release cadence and minimal maintenance. It provides a fluent API for building SELECT, INSERT, UPDATE, DELETE queries with automatic escaping and method chaining. Unlike full ORMs, it gives direct control over SQL while simplifying common patterns. It bundles node-mysql internally, so no extra dependencies are needed. Supports reconnection, raw queries, joins, grouping, and having clauses. However, it is limited to MySQL and ties closely to the callback-based node-mysql driver, lacking promise support.

npm install mysql-activerecord
INSTALL
IMPORT
SIG · MYSQL-ACTIVERECORD
M
mysql-activerecord
databasejavascriptv0.8.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.

Db
const Db = require('mysql-activerecord');
import Db from 'mysql-activerecord';
The package uses CommonJS and does not ship ESM modules. ESM import may fail in strict environments.
Adapter
const db = new Db.Adapter({ server: 'localhost', username: 'root', password: '12345', database: 'test' });
const db = new Db({ server: 'localhost', username: 'root', password: '12345', database: 'test' });
The exported object has an Adapter property; direct instantiation of the root export is not supported.
default
const Db = require('mysql-activerecord');
import mysql-activerecord from 'mysql-activerecord';
This package has no default export in the ESM sense; it relies on module.exports.

Demonstrates connecting to MySQL, selecting fields, applying a WHERE condition, ordering, and executing a callback-based query with the query builder.

const Db = require('mysql-activerecord'); const db = new Db.Adapter({ server: 'localhost', username: 'root', password: process.env.DB_PASS ?? '', database: 'test', reconnectTimeout: 2000 }); db.select('id, name') .from('users') .where('active', 1) .order_by('name ASC') .get((err, rows) => { if (err) throw err; console.log(rows); }); db.resetQuery();
Debug
Known issues
deprecatedUses callback-based node-mysql which is in maintenance mode; does not support promises or async/await.
fix
Consider migrating to mysql2 or a promise-based library like knex or Sequelize.
affects: >=0.0.1
gotchaThe package bundles a specific version of node-mysql internally, which may have unpatched security issues.
fix
Pin version and audit dependency tree; use Snyk or Dependabot.
affects: >=0.0.1
gotchaIf you call db.resetQuery() after each query, you must restart the query chain; forgetting this can carry over conditions between queries.
fix
Always call resetQuery() or use a new Adapter instance per query.
affects: >=0.0.1
gotchaThe .where() method treats arrays as IN clauses, but passing an object with array values may behave unexpectedly.
fix
Use .where() with separate arguments: .where('field', [1,2,3]) instead of .where({ field: [1,2,3] }).
affects: >=0.0.1
breakingVersion 0.5.0 changed default reconnectTimeout from 500 to 2000 ms; older code relying on 500ms may experience timing differences.
fix
Explicitly set reconnectTimeout in options if you need a specific value.
affects: <0.5.0
Errors
Common errors & fixes
Error: Cannot find module 'mysql-activerecord'
Package not installed or missing from node_modules.
fix
Run: npm install mysql-activerecord
TypeError: Db.Adapter is not a constructor
Using import statement where require is expected, or misspelling 'Adapter'.
fix
Use: const Db = require('mysql-activerecord'); const db = new Db.Adapter(...);
Error: Cannot enqueue Query after invoking quit.
Trying to execute a query after connection has been closed.
fix
Create a new Adapter instance or ensure the connection is still open before calling query methods.
Error: ECONNREFUSED when connecting to MySQL
MySQL server is not running or the port/host is incorrect.
fix
Check MySQL service: systemctl start mysql (Linux) or brew services start mysql (macOS). Verify host and port.
Upgrade
Version history
0.8.6latest on npm
Audit
Dependencies
mysqlrequiredWraps the node-mysql module for database connectivity; bundled in the package but still a peer dependency at runtime.
Agent activity
4 hits · last 30 days
node
4
Resources
mysql-activerecord — npm install mysql-activerecord · libregistry