feat(adapter): mysql adapter

feat(adapter): mysql adapter
This commit is contained in:
Leifer Mendez
2022-12-07 21:33:05 +01:00
committed by GitHub
3 changed files with 54 additions and 16 deletions

View File

@@ -4,6 +4,7 @@
"contributing",
"cli",
"bot",
"provider"
"provider",
"adapter"
]
}

View File

@@ -11,20 +11,6 @@ Video como hacer PR: https://youtu.be/Lxt8Acob6aU
<!-- readme: collaborators,contributors -start -->
<table>
<tr>
<td align="center">
<a href="https://github.com/leifermendez">
<img src="https://avatars.githubusercontent.com/u/15802366?v=4" width="50;" alt="leifermendez"/>
<br />
<sub><b>Leifer Mendez</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/aurik3">
<img src="https://avatars.githubusercontent.com/u/37228512?v=4" width="50;" alt="aurik3"/>
<br />
<sub><b>Null</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/vicente1992">
<img src="https://avatars.githubusercontent.com/u/57806030?v=4" width="50;" alt="vicente1992"/>
@@ -32,6 +18,13 @@ Video como hacer PR: https://youtu.be/Lxt8Acob6aU
<sub><b>Manuel Vicente Ortiz</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/leifermendez">
<img src="https://avatars.githubusercontent.com/u/15802366?v=4" width="50;" alt="leifermendez"/>
<br />
<sub><b>Leifer Mendez</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/leifermendezfroged">
<img src="https://avatars.githubusercontent.com/u/97020486?v=4" width="50;" alt="leifermendezfroged"/>
@@ -46,6 +39,13 @@ Video como hacer PR: https://youtu.be/Lxt8Acob6aU
<sub><b>Null</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/aurik3">
<img src="https://avatars.githubusercontent.com/u/37228512?v=4" width="50;" alt="aurik3"/>
<br />
<sub><b>Null</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/jzvi12">
<img src="https://avatars.githubusercontent.com/u/10729787?v=4" width="50;" alt="jzvi12"/>

View File

@@ -13,9 +13,10 @@ class MyslAdapter {
async init() {
this.db = mysql.createConnection(this.credentials)
await this.db.connect((error) => {
await this.db.connect(async (error) => {
if (!error) {
console.log(`Solicitud de conexión a base de datos exitosa`)
await this.checkTableExists()
}
if (error) {
@@ -64,6 +65,42 @@ class MyslAdapter {
})
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