Registry / database / mysql-model-orm

mysql-model-orm

JSON →
library2.1.4jsnpmunverified

mysql-model-orm is a lightweight Node.js ORM for MySQL that wraps common CRUD operations with a chainable API. Current stable version: 2.1.4. It uses connection pooling, returns Promises, and requires Node >= 7.6. Key differentiators: minimal configuration, simple chaining (table().where().select()), automatic SQL injection protection via the mysql driver's escape methods, and support for complex queries like JOINs, aggregates, transactions, and raw SQL.

npm install mysql-model-orm
INSTALL
IMPORT
SIG · MYSQL-MODEL-ORM
M
mysql-model-orm
databasejavascriptv2.1.4
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.

mysqlModelOrm
const mysqlModelOrm = require('mysql-model-orm'); const Model = new mysqlModelOrm({...});
import mysqlModelOrm from 'mysql-model-orm'; // ESM not supported in v2
Package is CommonJS only. Use require().
default export
const Model = require('mysql-model-orm'); const db = new Model({ host: 'localhost', ... });
const db = require('mysql-model-orm')(); // missing new keyword
Must call with new to instantiate.
chaining methods
const result = await db.table('users').where({id: 1}).findOne();
const result = await db.table('users').findOne().where({id: 1}); // where must come before query method
Chain order: table() first, then where/field/order/limit, then query method (select, findOne, etc).

Demonstrates instantiation, table creation, insert, select, update, delete, and transaction usage.

const mysqlModelOrm = require('mysql-model-orm'); const Model = new mysqlModelOrm({ host: 'localhost', user: 'root', password: process.env.DB_PASSWORD ?? '', database: 'test' }); async function main() { try { // Create table if not exists const createResult = await Model.execsql("CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)"); console.log('Table ready'); // Insert a user const insertResult = await Model.table('users').add({ name: 'Alice', age: 30 }); console.log('Inserted ID:', insertResult.insertId); // Query all users const users = await Model.table('users').select(); console.log('Users:', users); // Query with condition const user = await Model.table('users').where({ name: 'Alice' }).findOne(); console.log('Found:', user); // Update await Model.table('users').where({ name: 'Alice' }).update({ age: 31 }); // Delete await Model.table('users').where({ id: 1 }).delete(); // Transaction await Model.transaction([ Model.table('users').buildSql('add', { name: 'Bob', age: 25 }), Model.table('users').buildSql('update', { age: 26 }, { name: 'Bob' }) ]); } catch (err) { console.error(err); } } main();
Debug
Known issues
gotchaMethods are order-sensitive: table() must be called before where/field/order/limit, and query method (select, findOne, delete, etc.) must be called last.
fix
Ensure chain order: `Model.table('t').where(...).select()` not `Model.table('t').select().where(...)`.
affects: >=0.0.0
gotchadelete() must be called with a where() clause, otherwise it will throw an error or delete all rows.
fix
Always chain `.where(...)` before `.delete()`.
affects: >=0.0.0
gotchaThe library does not support ESM imports. Using `import` will fail.
fix
Use `require()` instead of `import`.
affects: >=0.0.0
gotchaThe table() method expects the full table name including prefix (if any). It does not automatically add prefixes.
fix
Pass the full table name like `Model.table('tk_tab')`.
affects: >=0.0.0
gotchaTransaction requires passing an array of SQL strings built with buildSql(), not chained method calls.
fix
Use `Model.transaction([Model.table('t').buildSql('add', data), ...])`.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Model.table is not a function
Missing `new` keyword when instantiating the model.
fix
Use `const Model = new mysqlModelOrm({...});` instead of `const Model = require('mysql-model-orm')({...});`.
Can't find variable: require
Trying to use require() in an ESM environment (e.g., "type": "module" in package.json).
fix
Use `import mysqlModelOrm from 'mysql-model-orm';` if the package supports ESM (it doesn't in v2), or switch to CommonJS by removing "type": "module".
Error: delete() must be used with where()
Calling delete() without a where() clause to prevent accidental mass deletion.
fix
Add `.where(...)` before `.delete()`.
TypeError: Cannot read properties of undefined (reading 'user')
Connection configuration missing or incorrect property names.
fix
Ensure config object has 'host', 'user', 'password', 'database', 'port' (optional).
Upgrade
Version history
2.1.4latest on npm
Audit
Dependencies
mysqlrequiredMySQL driver for database connectivity
Agent activity
7 hits · last 30 days
node
6
Resources
mysql-model-orm — npm install mysql-model-orm · libregistry