egg-mysql
Aliyun rds client(support mysql portocal) for egg framework
Install
$ npm i egg-mysql --save
MySQL Plugin for egg, support egg application access to MySQL database.
This plugin based on ali-rds, if you want to know specific usage, you should refer to the document of ali-rds.
Configuration
Change ${app_root}/config/plugin.js
to enable MySQL plugin:
exportsmysql = enable: true package: 'egg-mysql';
Configure database information in ${app_root}/config/config.default.js
:
Simple database instance
exportsmysql = // database configuration client: // host host: 'mysql.com' // port port: '3306' // username user: 'test_user' // password password: 'test_password' // database database: 'test' // load into app, default is open app: true // load into agent, default is close agent: false;
Usage:
appmysql; // you can access to simple database instance by using app.mysql.
Multiple database instance
exportsmysql = clients: // clientId, access the client instance by app.mysql.get('clientId') db1: // host host: 'mysql.com' // port port: '3306' // username user: 'test_user' // password password: 'test_password' // database database: 'test' // ... // default configuration for all databases default: // load into app, default is open app: true // load into agent, default is close agent: false;
Usage:
const client1 = appmysql;client1; const client2 = appmysql;client2;
CRUD user guide
Create
// insertconst result = appmysql;const insertSuccess = resultaffectedRows === 1;
Read
// getconst post = appmysql;// queryconst results = appmysql;
Update
// update by primary key ID, and refreshconst row = id: 123 name: 'fengmk2' otherField: 'other field value' modifiedAt: appmysqlliteralsnow // `now()` on db server;const result = appmysql;const updateSuccess = resultaffectedRows === 1;
Delete
const result = appmysql;
Transaction
Manual control
- adventage:
beginTransaction
,commit
orrollback
can be completely under control by developer - disadventage: more handwritten code, Forgot catching error or cleanup will lead to serious bug.
const conn = appmysql; try conn; conn; conn; catch err // error, rollback conn; // rollback call won't throw err throw err;
Automatic control: Transaction with scope
- API:
*beginTransactionScope(scope, ctx)
scope
: A generatorFunction which will execute all sqls of this transaction.ctx
: The context object of current request, it will ensures that even in the case of a nested transaction, there is only one active transaction in a request at the same time.
- adventage: easy to use, as if there is no transaction in your code.
- disadvantage: all transation will be successful or failed, cannot control precisely
const result = appmysql; // ctx is the context of current request, access by `this.ctx`.// if error throw on scope, will auto rollback
Advance
Custom SQL splicing
const results = appmysql;
Literal
If you want to call literals or functions in mysql , you can use Literal
.
Inner Literal
- NOW(): The database system time, you can obtain by
app.mysql.literals.now
.
appmysql; // INSERT INTO `$table`(`create_time`) VALUES(NOW())
Custom literal
The following demo showed how to call CONCAT(s1, ...sn)
funtion in mysql to do string splicing.
const Literal = appmysqlliteralsLiteral;const first = 'James';const last = 'Bond'; appmysql; // INSERT INTO `$table`(`id`, `fullname`) VALUES(123, CONCAT("James", "Bond"))
Questions & Suggestions
Please open an issue here.