Registry / database / node-sql-util

node-sql-util

JSON →
library1.14.9jsnpmunverified

A lightweight Node.js database utility library built on MySQL2 and SSH2 that provides a simple ORM-like interface for MySQL queries, including SELECT, INSERT, UPDATE, DELETE, JOIN, and transaction support. Version 1.14.9 is the current stable release. It offers SSH tunneling for remote debugging, connection pooling, SQL injection protection, and the ability to return raw SQL strings. It is less flexible than full ORMs like Sequelize but simpler for basic CRUD operations, with a focus on ease of use and security. The library is actively maintained with regular updates.

npm install node-sql-util
INSTALL
IMPORT
SIG · NODE-SQL-UTIL
N
node-sql-util
databasejavascriptv1.14.9
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.

SqlUtil
const SqlUtil = require('node-sql-util');
import { SqlUtil } from 'node-sql-util';
The package exports a default class via CommonJS. ES module import is not supported natively. Use require() or dynamic import().
SqlUtil
const SqlUtil = require('node-sql-util');
Alternative import style using CommonJS destructuring. Works the same as the default require.
SqlUtil
import SqlUtil from 'node-sql-util';
import { SqlUtil } from 'node-sql-util';
When using ES module syntax with a bundler or Node.js ESM, use default import. Named import will fail.
SqlUtil
const { default: SqlUtil } = await import('node-sql-util');
Dynamic import in ESM environments. The module has no named exports; default is the class.

Initializes a SqlUtil instance with MySQL connection config, performs a simple SELECT query with conditions and limit, and handles the response.

const SqlUtil = require('node-sql-util'); const mySql = new SqlUtil({ dbConfig: { host: '1.2.3.4', port: 3306, database: 'testdb', user: 'user', password: 'pass', connectionLimit: 5, } }); async function quickStart() { let searchRes = await mySql.select({ table: 'users', fields: ['name', 'age'], where: { age: 18 }, limit: 10 }); if (searchRes.code === 0) { console.log('Success:', searchRes.data); } else { console.error('Error:', searchRes.message); } } quickStart();
Debug
Known issues
gotchaThe WHERE clause by default uses AND logic. To use OR, you must use the array syntax documented in the condition chapter.
fix
Use array notation for where: [{field: 'age', value: 18}], [{field: 'name', value: 'lili'}] to combine with OR.
affects: >=0.0.0
deprecatedThe `order` parameter in select/find is deprecated and may be removed in future versions. Use `orderCustom` instead.
fix
Replace `order: 'desc'` with `orderCustom: 'order by id desc'`.
affects: >=1.10.0
gotchaThe `find` method returns a single object (not an array) in `res.data`, even if no rows found (returns null). This differs from `select` which always returns an array.
fix
Always check if `res.data` is truthy before accessing properties. Use `if (res.data)` not `if (res.data.length)`.
affects: >=0.0.0
gotchaWhen using `asSql: true`, the method returns the SQL string instead of executing it. The return format is the SQL string directly, not wrapped in a response object.
fix
Check the return value: if `asSql` is true, it's a string; otherwise it's an object with `code`, `data`, etc.
affects: >=0.0.0
gotchaThe `returnOriginError` and `returnOriginSource` options affect the response format. When set to true, the error/success object is returned directly instead of the standard { code, subcode, message, data } wrapper.
fix
Set these to false (default) to get consistent response format, or parse the return accordingly.
affects: >=0.0.0
Errors
Common errors & fixes
Error: connect ECONNREFUSED 1.2.3.4:3306
MySQL host is not reachable; port might be closed, or SSH tunnel not configured correctly.
fix
Verify host/port are correct. For SSH, add sshConfig to SqlUtil constructor: `new SqlUtil({ dbConfig: {...}, sshConfig: { host: 'proxy', username: 'user', privateKey: require('fs').readFileSync('/path/to/key') } })`.
TypeError: Cannot destructure property 'code' of 'undefined' or null.
sqlUtil.select() returned undefined because `asSql: true` was set and the function returned a string, not an object.
fix
Check the `asSql` field: if true, the return value is a string (the SQL). Remove `asSql` or set to false to get the response object.
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host' (using password: YES)
Invalid username or password in dbConfig.
fix
Double-check credentials. Ensure the user has access from the specified host. For SSH, the database connection goes through the tunnel; the host in dbConfig might be 'localhost' if using SSH.
TypeError: sqlUtil.select is not a function
SqlUtil is not properly instantiated or the import failed (e.g., used named import instead of default).
fix
Ensure you use `new SqlUtil(config)` and that the import is correct: `const SqlUtil = require('node-sql-util')`.
Error: Cannot find module 'node-sql-util'
Package not installed or not in node_modules.
fix
Run `npm install node-sql-util --save` or `yarn add node-sql-util`.
Upgrade
Version history
1.14.9latest on npm
Audit
Dependencies
mysql2requiredCore MySQL database driver for query execution and connection pooling.
ssh2optionalRequired for SSH tunneling support when connecting to remote databases.
Agent activity
5 hits · last 30 days
node
4
Resources
node-sql-util — npm install node-sql-util · libregistry