Registry / database / mysql-simple-wrap

mysql-simple-wrap

JSON →
library1.1.0jsnpmunverified

A Node.js library that wraps MySQL operations into an object-oriented interface to avoid writing raw SQL in application code. Current stable version is 1.1.0 (as of the last release). Provides batch insert, batch update, query chaining, and optional Redis caching. Key differentiators: simple API, no SQL required for common operations, and built-in Redis cache support. Compared to knex or sequelize, it is much lighter and more minimal, but lacks TypeScript types, transactions, migrations, and advanced query building. The project has low maintenance activity and a small community.

npm install mysql-simple-wrap
INSTALL
IMPORT
SIG · MYSQL-SIMPLE-WRAP
M
mysql-simple-wrap
databasejavascriptv1.1.0
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.

Conn
const { Conn } = require('mysql-simple-wrap')
const Conn = require('mysql-simple-wrap')
The library exports 'Conn' as a named export. Using a default import or requiring the whole module will not work.
Table
const table = new Conn({...}).table('tablename')
const { Table } = require('mysql-simple-wrap')
There is no standalone 'Table' export. You must create a Conn instance and call .table() on it.
query
const conn = new Conn({...}); conn.query('SELECT 1', callback)
const { query } = require('mysql-simple-wrap')
query is a method on Conn instance, not a standalone function.

Demonstrates connection setup, table selection, basic CRUD (select, batch insert, update), and chained query builder with conditions.

const { Conn } = require('mysql-simple-wrap'); const c = new Conn({ host: process.env.DB_HOST || 'localhost', user: process.env.DB_USER || 'root', password: process.env.DB_PASS || '', database: process.env.DB_NAME || 'test', port: 3306, charset: 'utf8mb4', timezone: 'Asia/Shanghai', time_colum_key: 'create_time', debug: false, }); const teacher = c.table('teacher'); // Simple select all teacher.getAllByWhere({ name: 'peter' }, (rows) => { console.log(rows); }); // Insert batch const data = [ { name: 'Alice', age: 20, class: 'A', school: 'School1' }, { name: 'Bob', age: 22, class: 'B', school: 'School2' }, ]; teacher.insert(data, (result) => { console.log('Inserted IDs:', result.insertId); }); // Update with where const updateData = { class: 'C' }; const where = { name: 'Alice' }; teacher.update(updateData, where, (result) => { console.log('Rows affected:', result.affectedRows); }); // Chain query const whereArr = []; whereArr.push(['age', '> 20']); whereArr.push(['school', 'is not null']); teacher.select().where(whereArr).orderby('create_time').limit(2).exec(rows => { console.log(rows); });
Debug
Known issues
gotchaThe 'insert' method expects an array for batch insert; if you pass a single object, it will wrap it in an array and treat it as a single-row insert. However, the callback returns insertId for the first row only.
fix
Always pass an array to insert for batch operations. For single insert, you can pass an object (it will work), but be aware that insertId may not correspond to the actual row if multiple rows are inserted inadvertently.
affects: >=1.0.0
gotchaThe 'update' method requires 'where' parameter as mandatory; omitting it will likely cause an error or unexpected update of all rows.
fix
Always provide a where object. To update all rows, pass an empty object {} (but be cautious).
affects: >=1.0.0
gotchaThe library uses callbacks, not Promises or async/await. There is no built-in promise support.
fix
Wrap callback-style calls in a Promise manually if you need async/await. For example: const queryAsync = (sql) => new Promise((resolve, reject) => c.query(sql, (err, rows) => err ? reject(err) : resolve(rows)));
affects: >=1.0.0
gotchaRedis caching is optional but if you provide a 'redis' config, the 'cache_on' flag must be true to enable caching. If Redis is provided but cache_on is false, caching will not work. If Redis is not provided but cache_on is true, it will throw an error.
fix
Ensure you either pass a valid redis client and set cache_on: true, or omit both settings entirely.
affects: >=1.0.0
gotchaThe 'mysql' driver (npm package) is a peer dependency and must be installed separately. The package does not bundle it, so you must have 'mysql' in your dependencies.
fix
Run: npm install mysql
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Conn is not a constructor
Using default import or destructuring incorrectly. The library exports 'Conn' as a named export, not a default export.
fix
Use `const { Conn } = require('mysql-simple-wrap');` instead of `const Conn = require('mysql-simple-wrap');`
Cannot find module 'mysql'
The package 'mysql' is a peer dependency required at runtime.
fix
Run `npm install mysql` in your project.
TypeError: teacher.select is not a function
Calling .select() on the result of a query or on wrong object instance.
fix
Ensure `teacher` is a Table instance obtained via `c.table('tablename')`. The .select() method is only available on Table instances.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
mysqlrequiredThe underlying MySQL driver used for database connections and queries.
redisoptionalRequired only if you want to use the built-in caching feature. If cache is not used, Redis can be omitted.
Agent activity
2 hits · last 30 days
node
2
Resources
mysql-simple-wrap — npm install mysql-simple-wrap · libregistry