Registry / database / cls-mysql

cls-mysql

JSON →
library1.0.3jsnpmunverified

Continuation-local storage shim for node-mysql that binds all callbacks passed to MySQL operations to a CLS namespace, enabling context propagation across asynchronous database calls. Current version 1.0.3 is stable but unmaintained. Requires peer dependencies `continuation-local-storage` ^3.1.3 and `mysql` ^2.5.4. Differentiates from similar CLS shims by specifically targeting the node-mysql driver. No significant release cadence; last update is years old.

npm install cls-mysql
INSTALL
IMPORT
SIG · CLS-MYSQL
C
cls-mysql
databasejavascriptv1.0.3
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.

cls-mysql
require('cls-mysql')(ns);
import clsMysql from 'cls-mysql'; clsMysql(ns);
Package provides only a CommonJS function; no default export for ES modules. It must be invoked with a CLS namespace.
ns
var ns = require('continuation-local-storage').createNamespace('NODESPACE');
Namespace is created via continuation-local-storage before passing to cls-mysql.
mysql
var mysql = require('mysql');
mysql module is required separately and configured as usual.

Shows basic setup: create CLS namespace, load cls-mysql shim, run a query inside the namespace and access context.

var cls = require('continuation-local-storage'); var ns = cls.createNamespace('NODESPACE'); var mysql = require('mysql'); require('cls-mysql')(ns); var connection = mysql.createConnection({ host: process.env.MYSQL_HOST ?? 'localhost', user: process.env.MYSQL_USER ?? 'root', password: process.env.MYSQL_PASSWORD ?? '' }); ns.run(function() { ns.set('user', 'Alice'); connection.query('SELECT 1 + 1 AS solution', function(err, results) { if (err) throw err; console.log('Result:', results[0].solution, ', user:', ns.get('user')); }); });
Debug
Known issues
deprecatedcontinuation-local-storage is deprecated; consider using AsyncLocalStorage from Node.js core.
fix
Migrate to AsyncLocalStorage-based context propagation (e.g., cls-hooked or direct AsyncLocalStorage).
affects: >=1.0.0
breakingRequires peer dependency mysql@^2.5.4; incompatible with mysql2 or newer mysql versions that break callback signatures.
fix
Stick to mysql@^2.5.4 or use a different CLS shim for mysql2.
affects: >=1.0.0
gotchaMust call shim BEFORE creating connections: require('cls-mysql')(ns) must precede mysql.createConnection.
fix
Move shim call before any mysql connection creation.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: cls-mysql is not a function
Importing as ES module or calling without namespace argument.
fix
Use require('cls-mysql')(ns) with a valid CLS namespace object.
Error: Cannot find module 'continuation-local-storage'
Missing peer dependency continuation-local-storage.
fix
Run npm install continuation-local-storage@^3.1.3
Error: Cannot find module 'mysql'
Missing peer dependency mysql.
fix
Run npm install mysql@^2.5.4
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
continuation-local-storagerequiredCore dependency providing the CLS namespace and context propagation
mysqlrequiredTarget MySQL driver whose callbacks are patched
Agent activity
6 hits · last 30 days
node
4
OpenAI (training)
1
Resources
cls-mysql — npm install cls-mysql · libregistry