调研分析报告点击这里。
yarn
## run test
yarn test
npm i mysql-orm
对外暴露 4 个属性:
- pool: mysql2 创建的连接池,具体使用见下方
- Query: 类,提供快捷调用 CRUD 的 API,文档点这里
- Orm: 详见这里
- transaction: 用来执行事务,详见事务,Query 和 Orm 使用事务见各自文档
返回的连接池,提供 3 个接口:
getConnection
:获取一个连接实例query
: 直接执行 SQL 语句,一般用来执行复杂 SQLclose
: 关闭连接池
const { pool } = require('mysql-orm')(
type: 'raw'
);
pool.query('select 1 + 1').then((rows, fields) => {
//
})
// 或者自己指定数据库
const { pool } = require('mysql-orm')({
type: 'raw',
db: {
username: '',
password: '',
host: '',
port: '',
poolSize: 20, // default
dbname: '',
}
})
const [data] = await pool.query('select 1 + 1')
const { transaction, pool } = require('mysql-orm')();
const t = await transaction();
const [{ insertId }] = await t.query(
'INSERT INTO t_user SET name = "TRANSACTION test", age = 23'
);
await t.commit(); // 提交
// await t.rollback(); // 回滚
t.release(); // 回收资源