Registry / database / ruoyi-eggjs-mysql

ruoyi-eggjs-mysql

JSON →
library1.1.15jsnpmunverified

Egg.js MySQL plugin based on mysql2, providing connection pool management and a simple API wrapper (select, insert, update, delete). Version 1.1.15 supports egg 2.x and 3.x (not 1.x). Key features: optional camelCase field conversion (v1.1.7+), timezone and dateStrings options (v1.1.14+), built-in transaction support, and automatic SQL execution time logging in development. Distinguishes from egg-mysql by being based on mysql2, offering camelCase conversion, and stricter Node.js version requirement (>=16).

npm install ruoyi-eggjs-mysql
INSTALL
IMPORT
SIG · RUOYI-EGGJS-MYSQL
R
ruoyi-eggjs-mysql
databasejavascriptv1.1.15
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.

plugin configuration
// config/plugin.js module.exports = { mysql: { enable: true, package: 'ruoyi-eggjs-mysql', }, };
// common mistake: using require in plugin config module.exports = { mysql: { enable: true, package: 'egg-mysql' } // wrong package name }
Must be configured as an Egg.js plugin, not imported directly in code.
app.mysql
const result = await app.mysql.select('SELECT * FROM users');
// common mistake: destructuring app.mysql const { mysql } = app; // mysql is undefined because it is a getter
app.mysql is a getter that returns a proxy object; direct destructuring may not work.
app.mysql.get
const db = app.mysql.get('db1');
const db = app.mysql.db1; // not a property
For multi-instance, use get() method, not property access.

Shows plugin configuration, single-instance client setup, and a simple select query with parameterized SQL.

// config/plugin.js module.exports = { mysql: { enable: true, package: 'ruoyi-eggjs-mysql', }, }; // config/config.default.js config.mysql = { client: { host: process.env.MYSQL_HOST ?? '127.0.0.1', port: 3306, user: process.env.MYSQL_USER ?? 'root', password: process.env.MYSQL_PASSWORD ?? '', database: 'test', }, }; // app/controller/user.js async queryUser() { const { app } = this.ctx; const users = await app.mysql.select('SELECT * FROM users WHERE age > ?', [18]); console.log(users); }
Debug
Known issues
breakingNode.js >= 16 required; egg 1.x is not supported (only 2.x and 3.x).
fix
Upgrade Node.js to >=16 and egg to 2.x or 3.x.
affects: >=1.0.0
deprecatedcamelCase option may change default behavior. It is false by default, but enabling it in existing projects can break field mapping.
fix
If you enable camelCase, update all field references from snake_case to camelCase.
affects: >=1.1.7
gotchaThe plugin sets dateStrings to true by default, returning dates as strings. If you expect Date objects, set dateStrings: false in config.
fix
Add dateStrings: false to mysql config for Date object returns.
affects: >=1.1.14
gotchaParameterized queries use the mysql2 pattern (placeholders with ?), not named placeholders. Using named placeholders will fail silently.
fix
Always use ? for parameters. For named placeholders, use a separate library.
affects: >=1.0.0
breakingThere is no default 'end' method on the app.mysql object; closing the connection pool is handled automatically on app close.
fix
Do not call app.mysql.end() or similar; pool lifecycle is automatic.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'select')
app.mysql is undefined because the plugin is not enabled or configured
fix
Ensure plugin is enabled in config/plugin.js and mysql config is set in config/config.default.js
Error: getaddrinfo EAI_AGAIN (or ECONNREFUSED) on connect to MySQL
MySQL host or port is unreachable
fix
Check MYSQL_HOST environment variable or config; ensure MySQL server is running and accessible.
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host'
Wrong credentials or user permissions
fix
Verify MYSQL_USER, MYSQL_PASSWORD, and that the user has access to the specified database.
Upgrade
Version history
1.1.15latest on npm
Audit
Dependencies
mysql2requiredMySQL database driver
Agent activity
7 hits · last 30 days
node
4
Meta
2
Resources
ruoyi-eggjs-mysql — npm install ruoyi-eggjs-mysql · libregistry