From 4b7de0f6901524fa2c09271c3a99c364e6b3c260 Mon Sep 17 00:00:00 2001 From: vicente1992 Date: Wed, 7 Dec 2022 15:07:38 -0500 Subject: [PATCH] feat(adapter): sql is added to create the table --- .vscode/settings.json | 3 ++- packages/database/src/mysql/index.js | 39 +++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d35c0e4..ee40dd0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "contributing", "cli", "bot", - "provider" + "provider", + "adapter" ] } diff --git a/packages/database/src/mysql/index.js b/packages/database/src/mysql/index.js index 63e6e7d..a557f3e 100644 --- a/packages/database/src/mysql/index.js +++ b/packages/database/src/mysql/index.js @@ -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