Registry / database / express-myconnection

express-myconnection

JSON →
library1.0.4jsnpmunverified

Connect/Express middleware for MySQL connection lifecycle management. Version 1.0.4 (last released 2018). Provides three strategies: 'single' (singleton connection per app), 'pool' (connection pool, auto-released at response end), and 'request' (new connection per request, auto-closed). Uses node-mysql as the underlying driver. Differentiator: auto-closing/releasing connections for pool and request strategies, preventing connection leaks. No major updates since 2018; considered stable but not actively maintained.

npm install express-myconnection
INSTALL
IMPORT
SIG · EXPRESS-MYCONNECTI
E
express-myconnection
databasejavascriptv1.0.4
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.

express-myconnection
const myConnection = require('express-myconnection');
import myConnection from 'express-myconnection';
CommonJS package; ESM import not supported natively.
myConnection(mysql, dbOptions, strategy)
app.use(require('express-myconnection')(mysql, dbOptions, 'pool'));
app.use(require('express-myconnection').default(mysql, dbOptions, 'pool'));
The module exports a function directly, not an object with default property.
req.getConnection(callback)
req.getConnection(function(err, connection) { ... });
const connection = req.getConnection(); // synchronously
getConnection is asynchronous; must use callback.

Sets up Express app with mysql connection pool; uses req.getConnection to run a query.

const mysql = require('mysql'); const myConnection = require('express-myconnection'); const express = require('express'); const app = express(); const dbOptions = { host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'root', password: process.env.DB_PASS ?? '', port: process.env.DB_PORT ?? 3306, database: process.env.DB_NAME ?? 'test' }; // Use pool strategy for automatic connection release app.use(myConnection(mysql, dbOptions, 'pool')); app.get('/', function(req, res, next) { req.getConnection(function(err, connection) { if (err) return next(err); connection.query('SELECT 1 AS result', function(err, results) { if (err) return next(err); res.send({ result: results[0].result }); }); }); }); app.listen(3000, () => console.log('Server running on port 3000'));
Debug
Known issues
deprecatedThe mysql package (node-mysql) is in maintenance mode. Use mysql2 instead.
fix
Replace require('mysql') with require('mysql2') and adapt if needed.
affects: >=0.0.0
breakingUsing 'single' strategy without manual connection management can cause connection timeouts if app is idle.
fix
Use 'pool' or 'request' strategy for production.
affects: >=0.0.0
gotchareq.getConnection callback may not fire if middleware is not applied before routes.
fix
Ensure app.use(myConnection(...)) is placed before route definitions.
affects: >=0.0.0
gotchaStrategy name must be lowercase: 'single', 'pool', or 'request'. Uppercase fails silently.
fix
Always pass lowercase string e.g., 'pool'.
affects: >=0.0.0
Errors
Common errors & fixes
Cannot find module 'mysql'
mysql is a peer dependency not auto-installed.
fix
Run: npm install mysql
req.getConnection is not a function
Middleware not applied or applied after routes using req.getConnection.
fix
Place app.use(myConnection(...)) before all routes that use req.getConnection.
TypeError: myConnection is not a function
Incorrect import (e.g., using default import from ESM) or missing require.
fix
Use: const myConnection = require('express-myconnection');
Upgrade
Version history
1.0.4latest on npm
Audit
Dependencies
mysqlrequiredNode-mysql driver is required as a peer dependency; passed as first argument to middleware.
Agent activity
4 hits · last 30 days
node
4
Resources
express-myconnection — npm install express-myconnection · libregistry