refactor: migrate to Tencent Cloud backend
这个提交包含在:
@@ -0,0 +1,48 @@
|
||||
const mysql = require('mysql2/promise')
|
||||
const config = require('../config')
|
||||
|
||||
let pool
|
||||
|
||||
function getPool() {
|
||||
if (!pool) {
|
||||
pool = mysql.createPool({
|
||||
host: config.db.host,
|
||||
port: config.db.port,
|
||||
user: config.db.user,
|
||||
password: config.db.password,
|
||||
database: config.db.database,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 5,
|
||||
namedPlaceholders: true,
|
||||
timezone: '+08:00'
|
||||
})
|
||||
}
|
||||
return pool
|
||||
}
|
||||
|
||||
async function query(sql, params) {
|
||||
const [rows] = await getPool().execute(sql, params || {})
|
||||
return rows
|
||||
}
|
||||
|
||||
async function one(sql, params) {
|
||||
const rows = await query(sql, params)
|
||||
return rows[0] || null
|
||||
}
|
||||
|
||||
async function transaction(work) {
|
||||
const conn = await getPool().getConnection()
|
||||
try {
|
||||
await conn.beginTransaction()
|
||||
const result = await work(conn)
|
||||
await conn.commit()
|
||||
return result
|
||||
} catch (err) {
|
||||
await conn.rollback()
|
||||
throw err
|
||||
} finally {
|
||||
conn.release()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { getPool, query, one, transaction }
|
||||
在新工单中引用
屏蔽一个用户