mysql-database

raw JSON →
1.3.2 verified Sat May 09 auth: no javascript

Easily modify MySQL database data with easy functions. Current stable version is 1.3.2. Provides a simplified API with Promise support and events like connected, dataModification, tableCreate/Delete/Clear/Rename. Methods include set, get, push, pull, add, subtract, exists, clear, delete, and base_set/base_get for encrypted rows. Key differentiator is the event system mirroring library actions. Last updated in 2022, release cadence unknown. Not actively maintained since 2022.

error TypeError: db.connect is not a function
cause Calling connect() on the MySQL constructor instead of an instance.
fix
Create an instance first: const db = new MySQL(); then db.connect(...).
error Error: ER_BAD_DB_ERROR: Unknown database 'my_database'
cause Database does not exist. Library does not auto-create databases.
fix
Create the database manually: CREATE DATABASE my_database;
error Error: connect ECONNREFUSED
cause MySQL server not running or wrong host/port.
fix
Ensure MySQL server is running and host/port are correct.
breaking get method returns null if table does not exist (since v1.3.0).
fix Check for null or use exists() before get().
gotcha Events are only emitted if action is taken from the library. Manual SQL changes via other clients won't trigger events.
fix Use library methods to modify data if event tracking is needed.
deprecated The library has no releases since 2022. The underlying 'mysql' package is also deprecated in favor of 'mysql2'.
fix Consider migrating to 'mysql2' and using a more maintained wrapper like 'typeorm' or 'knex'.
gotcha Table names with uppercase letters may cause 'undefined tables' errors before v1.2.8.
fix Upgrade to >=1.2.8 or use lowercase table names.
npm install mysql-database
yarn add mysql-database
pnpm add mysql-database

Connect to MySQL, set and get a key-value pair, then close connection.

const MySQL = require('mysql-database');
const database = new MySQL();

async function main() {
  const db = await database.connect({
    host: 'localhost',
    user: 'root',
    password: process.env.DB_PASSWORD ?? '',
    database: 'my_database'
  });

  db.on('connected', () => console.log('Connected'));
  await db.set('users', '1', { name: 'Alice', age: 30 });
  const user = await db.get('users', '1');
  console.log(user); // { name: 'Alice', age: 30 }
  await db.end();
}
main().catch(console.error);