This commit is contained in:
2022-06-02 15:45:35 +08:00
parent 10aebe67e7
commit 9bfa5a9b9c
9 changed files with 182 additions and 11 deletions

31
bin/mysql/base.js Normal file
View File

@@ -0,0 +1,31 @@
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){
throw new Error("select error")
}else {
c.destroy()
res(data)
}
})
})
}

24
bin/mysql/index.js Normal file
View File

@@ -0,0 +1,24 @@
import { mysqlConfig } from "../../config/index.js"
import { query } from "./base.js"
const dataname = mysqlConfig.database
/**
* 判断数据表是否存在
* @param {String} name 数据表名
* @return {Boolend} true 存在 false 不存在
*/
export async function isTable(name){
const list = await query("show tables")
for(const i in list){
console.log(list[i]["Tables_in_" + dataname])
if(name == list[i]["Tables_in_" + dataname]){
return true
}
}
return false
}
console.log(await isTable("aaa"))