基于 MySQL2 封装的 MySQL 客户端,提供更加便捷的 API 调用
'use strict'
const Mysql = require('@wac/mysql-client')
const client = new Mysql({
host: 'localhost',
user: 'root',
database: 'test'
})
const results = await client.query('select * from table')
事务
const tran = await client.beginTransaction()
await tran.query('update table set name = `Tony` where id = 101')
await tran.query('update table set name = `Rick` where id = 102')
await tran.commit()
回滚
const tran = await client.beginTransaction()
try {
await tran.query('update table set name = `Tony` where id = 101')
await tran.commit()
} catch (err) {
await tran.rollback()
}
使用标准 MySQL2 的 API,详情参考:https://www.npmjs.com/package/mysql2#api-and-configuration
关闭连接池中所有连接
返回一个 Transaction 实例