mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 12:39:24 +00:00
feat(adapter): sql is added to create the table
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -4,6 +4,7 @@
|
|||||||
"contributing",
|
"contributing",
|
||||||
"cli",
|
"cli",
|
||||||
"bot",
|
"bot",
|
||||||
"provider"
|
"provider",
|
||||||
|
"adapter"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,10 @@ class MyslAdapter {
|
|||||||
async init() {
|
async init() {
|
||||||
this.db = mysql.createConnection(this.credentials)
|
this.db = mysql.createConnection(this.credentials)
|
||||||
|
|
||||||
await this.db.connect((error) => {
|
await this.db.connect(async (error) => {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
console.log(`Solicitud de conexión a base de datos exitosa`)
|
console.log(`Solicitud de conexión a base de datos exitosa`)
|
||||||
|
await this.checkTableExists()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -64,6 +65,42 @@ class MyslAdapter {
|
|||||||
})
|
})
|
||||||
this.listHistory.push(ctx)
|
this.listHistory.push(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createTable = () =>
|
||||||
|
new Promise((resolve) => {
|
||||||
|
const tableName = 'history'
|
||||||
|
|
||||||
|
const sql = `CREATE TABLE ${tableName}
|
||||||
|
(id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
ref varchar(255) NOT NULL,
|
||||||
|
keyword varchar(255) NOT NULL,
|
||||||
|
answer longtext NOT NULL,
|
||||||
|
refSerialize varchar(255) NOT NULL,
|
||||||
|
phone varchar(255) NOT NULL,
|
||||||
|
options longtext NOT NULL
|
||||||
|
)`
|
||||||
|
|
||||||
|
this.db.query(sql, (err) => {
|
||||||
|
if (err) throw err
|
||||||
|
console.log(`Tabla ${tableName} creada correctamente `)
|
||||||
|
resolve(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
checkTableExists = () =>
|
||||||
|
new Promise((resolve) => {
|
||||||
|
const sql = "SHOW TABLES LIKE 'history'"
|
||||||
|
|
||||||
|
this.db.query(sql, (err, rows) => {
|
||||||
|
if (err) throw err
|
||||||
|
|
||||||
|
if (!rows.length) {
|
||||||
|
this.createTable()
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(!!rows.length)
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = MyslAdapter
|
module.exports = MyslAdapter
|
||||||
|
|||||||
Reference in New Issue
Block a user