Registry / database / egg-mysql

egg-mysql

JSON →
library5.0.0jsnpmunverified

MySQL plugin for the Egg.js framework, providing a convenient wrapper around @eggjs/rds (ali-rds). Version 5.0.0 requires Node >=18.0.0. It supports both single and multiple data sources, CRUD operations, and manual/automatic transaction management. The plugin extends the app and agent with mysql/mysqls instances, enabling straightforward SQL queries. Key differentiators: tight integration with Egg.js lifecycle, singleton pattern for multiple DB clients, and full TypeScript type definitions.

database
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.

The plugin is not imported directly; it is configured in config/plugin.ts as an Egg.js plugin.

// config/plugin.ts export default { mysql: { enable: true, package: 'egg-mysql' } };

The configuration must include 'client' object; using top-level fields directly is invalid.

// config/config.default.ts export default { mysql: { client: { host: 'localhost', port: '3306', user: 'root', password: 'pass', database: 'test' }, app: true } };

The plugin injects on app; do not import the package directly. app.mysql is available after enabling the plugin.

await app.mysql.query('SELECT * FROM users');

Configures egg-mysql plugin with a single MySQL connection, then uses app.mysql to run a query returning the current time.

// config/plugin.ts export default { mysql: { enable: true, package: 'egg-mysql', }, }; // config/config.default.ts export default { mysql: { client: { host: process.env.MYSQL_HOST ?? 'localhost', port: process.env.MYSQL_PORT ?? '3306', user: process.env.MYSQL_USER ?? 'root', password: process.env.MYSQL_PASSWORD ?? '', database: process.env.MYSQL_DATABASE ?? 'test', }, app: true, agent: false, }, }; // app.ts (or controller) import { Application } from 'egg'; export default class PostController { async index(ctx: any) { const { app } = ctx; const result = await app.mysql.query('SELECT NOW() as current_time'); ctx.body = result; } }
Debug
Known footguns
breakingVersion 5.0.0 requires Node >=18.0.0 and drops support for older Node versions.
deprecatedThe internal library moved from ali-rds to @eggjs/rds. Old configurations may reference ali-rds directly.
gotchaThe plugin is exclusively for Egg.js framework; it cannot be used outside of an Egg application context.
gotchaConfiguration property 'mysqls' (plural) is only available after enabling multiple data sources via 'clients' config. Using it with 'client' (singular) will result in undefined.
gotchaIn TypeScript, app.mysql type is inferred from @eggjs/rds; you might need to declare module augmentation for correct types.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
7 hits · last 30 days
gptbot
3
ahrefsbot
1
Resources