Registry / database / node-mysql-queries

node-mysql-queries

JSON →
library1.0.9jsnpmunverified

A lightweight helper library for building common MySQL queries in Node.js. Version 1.0.9 is the latest stable release, with infrequent updates. It wraps the mysqljs/mysql package and provides simple functions like selectAllSQL, selectWhereSQL, insertSQL, updateSQL, and deleteSQL to reduce boilerplate. Unlike ORMs like Sequelize or TypeORM, it offers a minimal, direct query builder without schema mapping or migrations. Requires mysql and dotenv as peer dependencies. Ships with TypeScript definitions.

npm install node-mysql-queries
INSTALL
IMPORT
SIG · NODE-MYSQL-QUERIES
N
node-mysql-queries
databasejavascriptv1.0.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.

selectAllSQL
import { selectAllSQL } from 'node-mysql-queries'
const selectAllSQL = require('node-mysql-queries');
ESM default export is not provided; use named import. CJS require works but returns an object with all functions.
selectWhereSQL
import { selectWhereSQL } from 'node-mysql-queries'
const { selectWhereSQL } = require('node-mysql-queries').default;
No default export; use named import. In CJS, destructure directly from require().
insertSQL
import { insertSQL } from 'node-mysql-queries'
import * as mysqlQueries from 'node-mysql-queries'; const insertSQL = mysqlQueries.insertSQL;
Named import is preferred. Namespace import also works but is less common.
updateSQL
import { updateSQL } from 'node-mysql-queries'
Straightforward named import.
deleteSQL
import { deleteSQL } from 'node-mysql-queries'
Straightforward named import.

Initializes MySQL connection from environment variables, then demonstrates insert and select all operations.

import { selectAllSQL, insertSQL } from 'node-mysql-queries'; import mysql from 'mysql'; import dotenv from 'dotenv'; dotenv.config(); const connection = mysql.createConnection({ host: process.env.DB_HOST ?? '', user: process.env.DB_USER ?? '', password: process.env.DB_PASSWORD ?? '', database: process.env.DB_DATABASE ?? '' }); // Mock Express response object for demonstration const res = { status: (code) => ({ json: (data) => console.log(`Status ${code}:`, data) }), json: (data) => console.log('Response:', data) }; // Insert a new post insertSQL(res, 'posts', { id: 1, title: 'Hello World' }); // Select all posts selectAllSQL(res, 'posts');
Debug
Known issues
breakingThe library requires the 'mysql' package and 'dotenv' as peer dependencies - they are not included automatically.
fix
Run 'npm install mysql dotenv' alongside this package.
affects: >=1.0.0
gotchaThe functions accept a response object (res) as first argument and expect it to have .status().json() methods (Express-like). Using without Express will cause runtime errors.
fix
Pass an object with the required structure: { status: (code) => ({ json: (data) => {...} }) }.
affects: >=1.0.0
gotchaThe library does not handle connection pooling or error handling internally; connection failures will crash the app unless caught.
fix
Wrap calls in try-catch or use a custom wrapper to manage errors.
affects: >=1.0.0
deprecatedThe 'mysql' package is unmaintained and has known security vulnerabilities. The library does not support mysql2.
fix
Consider using a more modern driver like mysql2 or switch to an ORM that patches security issues.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot read properties of undefined (reading 'json')
The first argument 'res' does not have a .json() method, or the method doesn't return an object with .json().
fix
Ensure 'res' is an Express-like response object, or pass a mock object: { json: (data) => console.log(data) }.
ENOTFOUND: getaddrinfo ENOTFOUND your_db_host
Missing or malformed DB_HOST environment variable.
fix
Verify .env file has 'DB_HOST' set and is loaded with dotenv.config() before any DB calls.
ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host' (using password: YES)
Incorrect DB_USER or DB_PASSWORD in environment variables.
fix
Double-check credentials in .env and ensure the user has permissions on the specified database.
Upgrade
Version history
1.0.9latest on npm
Audit
Dependencies
mysqlrequiredCore MySQL driver for Node.js; this lib wraps its connection and query methods.
dotenvoptionalUsed to load database credentials from .env file by default.
Agent activity
2 hits · last 30 days
node
2
Resources
node-mysql-queries — npm install node-mysql-queries · libregistry