message
This commit is contained in:
31
bin/mysql/base.js
Normal file
31
bin/mysql/base.js
Normal 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
24
bin/mysql/index.js
Normal 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"))
|
||||
Reference in New Issue
Block a user