2022-06-02 15:45:35 +08:00
|
|
|
import mysql from "mysql"
|
|
|
|
import { mysqlConfig } from "../../config/index.js";
|
|
|
|
|
|
|
|
|
|
|
|
function connection(){
|
|
|
|
return new Promise((res,rej)=>{
|
|
|
|
const c = mysql.createConnection(mysqlConfig);
|
|
|
|
c.connect(function (err) {
|
|
|
|
if (err) {
|
|
|
|
throw new Error("connect mysql error",err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res(c)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function query(sql){
|
|
|
|
return new Promise(async (res)=>{
|
|
|
|
const c = await connection()
|
|
|
|
c.query(sql,(err,data)=>{
|
|
|
|
if(err){
|
2022-06-03 21:57:28 +08:00
|
|
|
// throw new Error("sql error",err)
|
|
|
|
console.log(err)
|
2022-06-02 15:45:35 +08:00
|
|
|
}else {
|
|
|
|
c.destroy()
|
|
|
|
res(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|