
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
A simple and easy-to-use MySQL query executor for Node.js applications. Built on top of MySQL2 with connection pooling and multiple database support.
npm install mysqlexec --save
const mysqlexec = require('mysqlexec');
const { myexec } = require('mysqlexec');
// Initialize with default configuration
await mysqlexec.initialize();
// Execute queries
const result = await myexec('SELECT * FROM users WHERE id = ?', [1]);
console.log(result);
Create a .env
file in your project root:
# MySQL Configuration
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=yourpassword
MYSQL_DATABASE=yourdatabase
MYSQL_PORT=3306
MYSQL_CONN_LIMIT=10
MYSQL_MAX_IDLE=10
MYSQL_IDLE_TIMEOUT=60000
MYSQL_TIMEZONE=Z
Initialize a connection pool.
// Using environment variables (recommended)
await mysqlexec.initialize();
// Using custom configuration
await mysqlexec.initialize({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydb',
port: 3306,
timezone: 'local'
});
// Multiple databases with aliases
await mysqlexec.initialize({
host: 'localhost',
user: 'root',
password: 'password',
database: 'analytics',
poolAlias: 'analytics'
});
Execute a SQL query.
// Simple query
const users = await myexec('SELECT * FROM users');
// Query with parameters
const user = await myexec('SELECT * FROM users WHERE id = ?', [123]);
// Query on specific database pool
const analytics = await myexec('SELECT * FROM events', [], 'analytics');
// Named parameters (object)
const result = await myexec(
'SELECT * FROM users WHERE name = :name AND age > :age',
{ name: 'John', age: 25 }
);
Execute multiple queries in a transaction.
const { myexectrans } = require('mysqlexec');
const queries = [
{
query: 'INSERT INTO users (name, email) VALUES (?, ?)',
parameters: ['John Doe', 'john@example.com']
},
{
query: 'UPDATE profiles SET updated_at = NOW() WHERE user_id = ?',
parameters: [1]
}
];
try {
await myexectrans(queries);
console.log('Transaction completed successfully');
} catch (error) {
console.error('Transaction failed:', error);
}
const mysqlexec = require('mysqlexec');
const { myexec } = require('mysqlexec');
async function setupDatabases() {
// Main database (default pool)
await mysqlexec.initialize({
host: 'localhost',
user: 'root',
password: 'password',
database: 'main_app'
});
// Analytics database
await mysqlexec.initialize({
host: 'analytics-server',
user: 'analytics_user',
password: 'analytics_pass',
database: 'analytics',
poolAlias: 'analytics'
});
}
async function queryDatabases() {
// Query main database
const users = await myexec('SELECT * FROM users');
// Query analytics database
const events = await myexec('SELECT * FROM user_events', [], 'analytics');
}
Option | Type | Default | Description |
---|---|---|---|
host | string | localhost | MySQL server hostname |
user | string | root | MySQL username |
password | string | '' | MySQL password |
database | string | test | Database name |
port | number | 3306 | MySQL server port |
connectionLimit | number | 10 | Maximum number of connections |
maxIdle | number | 10 | Maximum idle connections |
idleTimeout | number | 60000 | Idle connection timeout (ms) |
timezone | string | Z | Timezone setting |
poolAlias | string | default | Pool identifier for multiple databases |
All configuration options can be set via environment variables:
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=yourpassword
MYSQL_DATABASE=yourdatabase
MYSQL_PORT=3306
MYSQL_CONN_LIMIT=10
MYSQL_MAX_IDLE=10
MYSQL_IDLE_TIMEOUT=60000
MYSQL_TIMEZONE=Z
NODE_ENV=production
const express = require('express');
const mysqlexec = require('mysqlexec');
const { myexec } = require('mysqlexec');
const app = express();
// Initialize database connection
async function initializeApp() {
try {
await mysqlexec.initialize();
console.log('Database connected successfully');
app.listen(3000, () => {
console.log('Server running on port 3000');
});
} catch (error) {
console.error('Failed to initialize database:', error);
process.exit(1);
}
}
// API endpoint
app.get('/api/users/:id', async (req, res) => {
try {
const result = await myexec(
'SELECT * FROM users WHERE id = ?',
[req.params.id]
);
if (result.length > 0) {
res.json(result[0]);
} else {
res.status(404).json({ error: 'User not found' });
}
} catch (error) {
console.error('Database error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
initializeApp();
// ✅ Good - prevents SQL injection
const user = await myexec('SELECT * FROM users WHERE id = ?', [userId]);
// ❌ Bad - vulnerable to SQL injection
const user = await myexec(`SELECT * FROM users WHERE id = ${userId}`);
// ✅ Initialize once at application startup
async function startApp() {
await mysqlexec.initialize();
// Start your application
}
// ✅ Use transactions for data consistency
const queries = [
{ query: 'INSERT INTO orders (...) VALUES (?)', parameters: [...] },
{ query: 'UPDATE inventory SET quantity = quantity - ? WHERE id = ?', parameters: [...] }
];
await myexectrans(queries);
try {
const result = await myexec('SELECT * FROM users WHERE id = ?', [123]);
console.log('User found:', result[0]);
} catch (error) {
console.error('Database error:', error.message);
// Handle error appropriately
}
The library automatically adjusts behavior based on NODE_ENV
:
NODE_ENV=dev
): Query logging, connection details loggingNODE_ENV=production
): Silent operation, error logging onlyConnection Refused
Error: connect ECONNREFUSED 127.0.0.1:3306
Authentication Failed
Error: Access denied for user 'username'@'host'
poolAlias
from MySQL2 connection config to eliminate deprecation warning[2.1.1] - 2025-08-13
poolAlias
from MySQL2 connection config to eliminate deprecation warningpoolAlias
property is now properly extracted before creating MySQL2 connection poolFAQs
Running MySQL queries made easier
The npm package mysqlexec receives a total of 3 weekly downloads. As such, mysqlexec popularity was classified as not popular.
We found that mysqlexec demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.