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-utilNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Initializes a SqlUtil instance with MySQL connection config, performs a simple SELECT query with conditions and limit, and handles the response.
Use array notation for where: [{field: 'age', value: 18}], [{field: 'name', value: 'lili'}] to combine with OR.Replace `order: 'desc'` with `orderCustom: 'order by id desc'`.
Always check if `res.data` is truthy before accessing properties. Use `if (res.data)` not `if (res.data.length)`.
Check the return value: if `asSql` is true, it's a string; otherwise it's an object with `code`, `data`, etc.
Set these to false (default) to get consistent response format, or parse the return accordingly.
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') } })`.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.
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.
Ensure you use `new SqlUtil(config)` and that the import is correct: `const SqlUtil = require('node-sql-util')`.Run `npm install node-sql-util --save` or `yarn add node-sql-util`.