Registry / database / mysql-json

mysql-json

JSON →
library0.1.3jsnpmunverified

A simple Node.js wrapper around the mysql package that provides JSON-based CRUD operations: insert, update, delete, and findByPrimaryKey. Current stable version is 0.1.3 (last release). No release cadence defined; the package appears to be in maintenance mode. Differentiators include a minimal API for generating SQL from JSON objects without raw queries.

npm install mysql-json
INSTALL
IMPORT
SIG · MYSQL-JSON
M
mysql-json
databasejavascriptv0.1.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.

default
const MysqlJson = require('mysql-json'); const db = new MysqlJson(options);
import MysqlJson from 'mysql-json'; // ESM not supported
The package exports a CommonJS class. Use require().
findByPrimaryKey
db.findByPrimaryKey('table', 1, callback);
db.findByPrimaryKey('table', '1', callback); // id parameter should be number, not string
Assumes primary key is numeric. String ids may not work correctly.
insert
db.insert('table', {col: 'val'}, callback);
db.insert('table', {col:7}, callback); // numeric values accepted but all values become strings due to JSON.stringify
May cause type coercion; numeric values are converted to strings.

Demonstrates full CRUD: insert, findByPrimaryKey, update, and delete using JSON objects.

const MysqlJson = require('mysql-json'); const db = new MysqlJson({ host:'127.0.0.1', user:'root', password:'root', database:'test' }); db.insert('users', { name: 'Alice', email: 'alice@example.com' }, (err, res) => { if (err) throw err; console.log('Inserted:', res); db.findByPrimaryKey('users', 1, (err, row) => { if (err) throw err; console.log('Found:', row); db.update('users', { name: 'Alice Updated' }, { id: { operator: '=', value: 1 } }, (err) => { if (err) throw err; db.delete('users', { id: { operator: '=', value: 1 } }, (err) => { if (err) throw err; console.log('Deleted'); }); }); }); });
Debug
Known issues
gotchaAll values in insert/update are converted to strings via JSON.stringify. Numeric values become strings in the database.
fix
Use the underlying mysql package directly for type-safe queries.
affects: >=0.1.0
gotchaConditions object keys are column names, and values must be objects with operator and value properties. Missing operator defaults to '='?
fix
Always specify {operator: '=', value: ...} in conditions.
affects: >=0.1.0
gotchafindByPrimaryKey expects a numeric id. Passing a string may cause unexpected SQL or no results.
fix
Ensure id is a number: findByPrimaryKey('table', Number(id), callback).
affects: >=0.1.0
breakingNo connection pooling. Each new MysqlJson instance creates a new connection. Improper use may exhaust MySQL connections.
fix
Reuse a single instance or implement connection pooling manually with the mysql package.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: MysqlJson is not a constructor
Using import instead of require in a CommonJS environment.
fix
Use require('mysql-json') instead of import.
ER_BAD_FIELD_ERROR: Unknown column 'value' in 'where clause'
Conditions object formatted incorrectly; missing operator or nested incorrectly.
fix
Ensure conditions are like {column: {operator: '=', value: 'val'}}.
Cannot read property 'connect' of undefined
Database options missing or connection fails.
fix
Check host, user, password, database in options.
Upgrade
Version history
0.1.3latest on npm
Audit
Dependencies
mysqlrequiredRequired to connect and query MySQL databases.
Agent activity
4 hits · last 30 days
node
4
Resources
mysql-json — npm install mysql-json · libregistry