egg-mysql
mysql plugin for Egg.js
NOTE: This plugin just for integrate mysql into Egg.js, more documentation please visit https://github.com/fuxingZhang/mysql-promise
Install
$ npm i @eggplugin/mysql --save
Configuration
exports.mysql = {
enable: true,
package: '@eggplugin/mysql',
};
see config/config.default.js for more detail.
Simple instance
exports.mysql = {
client: {
user: '',
host: '',
database: '',
password: ''
},
app: true,
agent: false,
};
Usage:
single query:
;(async () => {
const pool = app.mysql;
const { results, fields } = await pool.query('SELECT NOW()');
console.log({ results, fields });
})().catch(console.error);
check out a client:
;(async () => {
const pool = app.mysql;
const client = await pool.getConnection();
try {
const res = await client.query('SELECT * FROM users WHERE id = ?', [1]);
console.log(res.results[0]);
const { results, fields } = await client.query('SELECT NOW()');
console.log(results, fields);
} finally {
client.release();
}
})().catch(console.error);
more documentation please visit https://github.com/fuxingZhang/mysql-promise
Multiple instance
exports.mysql = {
default: {
port: 3306,
connectionLimit: 10,
},
clients: {
db1: {
user: '',
host: '',
database: '',
password: ''
},
db2: {
user: '',
host: '',
database: '',
password: ''
},
},
app: true,
agent: false,
};
Usage:
;(async () => {
const pool1 = app.mysql.get('db1');
const pool2 = app.mysql.get('db2');
}).catch(console.error);
more documentation please visit https://github.com/fuxingZhang/mysql-promise
Questions & Suggestions
Please open an issue here.
License
MIT