{"id":41559,"library":"mysql-model-orm","title":"mysql-model-orm","description":"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.","status":"active","version":"2.1.4","language":"javascript","source_language":"en","source_url":"https://github.com/1119879311/npm_module/tree/master/mysql-model-orm","tags":["javascript","mysql-model-orm","mysql","orm","model","node"],"install":[{"cmd":"npm install mysql-model-orm","lang":"bash","label":"npm"},{"cmd":"yarn add mysql-model-orm","lang":"bash","label":"yarn"},{"cmd":"pnpm add mysql-model-orm","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"MySQL driver for database connectivity","package":"mysql","optional":false}],"imports":[{"note":"Package is CommonJS only. Use require().","wrong":"import mysqlModelOrm from 'mysql-model-orm'; // ESM not supported in v2","symbol":"mysqlModelOrm","correct":"const mysqlModelOrm = require('mysql-model-orm');\nconst Model = new mysqlModelOrm({...});"},{"note":"Must call with new to instantiate.","wrong":"const db = require('mysql-model-orm')(); // missing new keyword","symbol":"default export","correct":"const Model = require('mysql-model-orm');\nconst db = new Model({ host: 'localhost', ... });"},{"note":"Chain order: table() first, then where/field/order/limit, then query method (select, findOne, etc).","wrong":"const result = await db.table('users').findOne().where({id: 1}); // where must come before query method","symbol":"chaining methods","correct":"const result = await db.table('users').where({id: 1}).findOne();"}],"quickstart":{"code":"const mysqlModelOrm = require('mysql-model-orm');\n\nconst Model = new mysqlModelOrm({\n  host: 'localhost',\n  user: 'root',\n  password: process.env.DB_PASSWORD ?? '',\n  database: 'test'\n});\n\nasync function main() {\n  try {\n    // Create table if not exists\n    const createResult = await Model.execsql(\"CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)\");\n    console.log('Table ready');\n\n    // Insert a user\n    const insertResult = await Model.table('users').add({ name: 'Alice', age: 30 });\n    console.log('Inserted ID:', insertResult.insertId);\n\n    // Query all users\n    const users = await Model.table('users').select();\n    console.log('Users:', users);\n\n    // Query with condition\n    const user = await Model.table('users').where({ name: 'Alice' }).findOne();\n    console.log('Found:', user);\n\n    // Update\n    await Model.table('users').where({ name: 'Alice' }).update({ age: 31 });\n\n    // Delete\n    await Model.table('users').where({ id: 1 }).delete();\n\n    // Transaction\n    await Model.transaction([\n      Model.table('users').buildSql('add', { name: 'Bob', age: 25 }),\n      Model.table('users').buildSql('update', { age: 26 }, { name: 'Bob' })\n    ]);\n  } catch (err) {\n    console.error(err);\n  }\n}\nmain();","lang":"javascript","description":"Demonstrates instantiation, table creation, insert, select, update, delete, and transaction usage."},"warnings":[{"fix":"Ensure chain order: `Model.table('t').where(...).select()` not `Model.table('t').select().where(...)`.","message":"Methods are order-sensitive: table() must be called before where/field/order/limit, and query method (select, findOne, delete, etc.) must be called last.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Always chain `.where(...)` before `.delete()`.","message":"delete() must be called with a where() clause, otherwise it will throw an error or delete all rows.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use `require()` instead of `import`.","message":"The library does not support ESM imports. Using `import` will fail.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Pass the full table name like `Model.table('tk_tab')`.","message":"The table() method expects the full table name including prefix (if any). It does not automatically add prefixes.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use `Model.transaction([Model.table('t').buildSql('add', data), ...])`.","message":"Transaction requires passing an array of SQL strings built with buildSql(), not chained method calls.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"search_vec":"'2.1.4':28 '7.6':38 'aggreg':66 'api':24 'automat':48 'chain':44 'chainabl':23 'common':18 'complex':62 'configur':42 'connect':31 'crud':19 'current':25 'differenti':40 'driver':55 'escap':57 'inject':50 'javascript':71 'join':65 'key':39 'lightweight':11 'like':64 'method':58 'minim':41 'model':3,7,74,78 'mysql':2,6,15,54,73,76 'mysql-model-orm':1,5,72 'node':37,79 'node.js':12 'oper':20 'orm':4,8,13,75,77 'pool':32 'promis':34 'protect':51 'queri':63 'raw':69 'requir':36 'return':33 'select':47 'simpl':43 'sql':49,70 'stabl':26 'support':60 'tabl':45 'transact':67 'use':30 'version':27 'via':52 'wrap':17","created_at":"2026-06-04T18:53:00.220194+00:00","updated_at":"2026-06-04T18:53:00.220194+00:00","problems":[{"fix":"Use `const Model = new mysqlModelOrm({...});` instead of `const Model = require('mysql-model-orm')({...});`.","cause":"Missing `new` keyword when instantiating the model.","error":"TypeError: Model.table is not a function"},{"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\".","cause":"Trying to use require() in an ESM environment (e.g., \"type\": \"module\" in package.json).","error":"Can't find variable: require"},{"fix":"Add `.where(...)` before `.delete()`.","cause":"Calling delete() without a where() clause to prevent accidental mass deletion.","error":"Error: delete() must be used with where()"},{"fix":"Ensure config object has 'host', 'user', 'password', 'database', 'port' (optional).","cause":"Connection configuration missing or incorrect property names.","error":"TypeError: Cannot read properties of undefined (reading 'user')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/1119879311/npm_module/tree/master/mysql-model-orm","github":"https://github.com/1119879311/npm_module/tree/master/mysql-model-orm","docs":null,"changelog":null,"pypi":null,"npm":"mysql-model-orm","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-04","next_check":"2026-09-02","install_tag":null}}