Registry / database / mysql-named-params-escape

mysql-named-params-escape

JSON →
library1.0.2jsnpmunverified

Custom query format for named parameters in node-mysql (mysql2 compatible). Version 1.0.2. Allows use of named placeholders prefixed with : for values and :: for identifiers (e.g., column names, table names) in MySQL queries. Integrates with mysql's connection.config.queryFormat. Lightweight, no dependencies. Alternatives: knex, squel, but this is a minimal escape-only solution.

npm install mysql-named-params-escape
INSTALL
IMPORT
SIG · MYSQL-NAMED-PARAMS
M
mysql-named-params-escape
databasejavascriptv1.0.2
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.

default
import npEscape from 'mysql-named-params-escape'
const { npEscape } = require('mysql-named-params-escape')
Module exports a single function as default. Use default import for ES modules.
require (CommonJS)
const npEscape = require('mysql-named-params-escape')
const { escape } = require('mysql-named-params-escape')
CommonJS require returns the function directly; no named exports.
TypeScript types
import npEscape from 'mysql-named-params-escape'
Package does not ship TypeScript definitions; use @types/mysql-named-params-escape or declare module.

Shows how to set queryFormat on a mysql connection and use named placeholders for identifiers (::) and values (:).

const mysql = require('mysql'); const npEscape = require('mysql-named-params-escape'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: process.env.DB_PASSWORD ?? '', database: 'test' }); // Set query format connection.config.queryFormat = npEscape; const query = "SELECT ::name, ::age FROM ::users WHERE ::users.id = :id"; const params = { name: 'first_name', age: 'age', users: 'users', id: 1 }; connection.query(query, params, (error, results) => { if (error) throw error; console.log(results); }); connection.end();
Debug
Known issues
gotchaIdentifiers must use double colon (::) prefix; single colon (:) is for values. Mixing them causes unexpected escaping.
fix
Ensure :: is used for identifiers (table, column names) and : for values.
affects: >=1.0.0
gotchaThe module only escapes identifiers and values; it does not validate that keys in params correspond to placeholders. Missing keys will result in undefined being inserted.
fix
Always ensure params object contains all keys used in the query.
affects: >=1.0.0
gotchaOnly works with mysql (or mysql2) library; not compatible with other database drivers or raw mysql2 prepared statements.
fix
Use with mysql or mysql2 connection. For other drivers, consider alternatives.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: npEscape is not a function
Using named export instead of default import in ES module.
fix
Use `import npEscape from 'mysql-named-params-escape'` or `const npEscape = require('mysql-named-params-escape')`
Error: ER_PARSE_ERROR: You have an error in your SQL syntax
Using single colon (:) for identifiers or double colon (::) for values.
fix
Use :: for identifiers (e.g., ::table) and : for values (e.g., :value).
ReferenceError: params is not defined
Passing undefined or missing params object to npEscape.
fix
Ensure params is an object with matching keys for all placeholders in the query.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
mysql-named-params-escape — npm install mysql-named-params-escape · libregistry