mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-18 03:29:15 +00:00
fix(adapter): conflict resolution
This commit is contained in:
@@ -10,11 +10,13 @@
|
||||
"dependencies": {
|
||||
"dotenv": "^16.0.3",
|
||||
"mongodb": "^4.11.0",
|
||||
"stormdb": "^0.6.0"
|
||||
"stormdb": "^0.6.0",
|
||||
"mysql2": "^2.3.3"
|
||||
},
|
||||
"exports": {
|
||||
"./mock": "./lib/mock/index.cjs",
|
||||
"./mongo": "./lib/mongo/index.cjs",
|
||||
"./json-file": "./lib/json-file/index.cjs"
|
||||
"./json-file": "./lib/json-file/index.cjs",
|
||||
"./mysql": "./lib/mysql/index.cjs"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,12 +22,20 @@ module.exports = [
|
||||
plugins: [commonjs()],
|
||||
},
|
||||
{
|
||||
input: join(__dirname, 'src', 'json-file', 'index.js'),
|
||||
input: join(__dirname, 'src', 'mysql', 'index.js'),
|
||||
output: {
|
||||
banner: banner['banner.output'].join(''),
|
||||
file: join(__dirname, 'lib', 'json-file', 'index.cjs'),
|
||||
file: join(__dirname, 'lib', 'mysql', 'index.cjs'),
|
||||
format: 'cjs',
|
||||
},
|
||||
plugins: [commonjs()],
|
||||
},
|
||||
{
|
||||
input: join(__dirname, 'src', 'json-file', 'index.js'),
|
||||
output: {
|
||||
banner: banner['banner.output'].join(''),
|
||||
file: join(__dirname, 'lib', 'json-file', 'index.cjs'),
|
||||
},
|
||||
plugins: [commonjs()],
|
||||
},
|
||||
]
|
||||
|
||||
69
packages/database/src/mysql/index.js
Normal file
69
packages/database/src/mysql/index.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const mysql = require('mysql2')
|
||||
|
||||
class MyslAdapter {
|
||||
db
|
||||
listHistory = []
|
||||
credentials = { host: null, user: null, database: null }
|
||||
|
||||
constructor(_credentials) {
|
||||
this.credentials = _credentials
|
||||
this.init().then()
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.db = mysql.createConnection(this.credentials)
|
||||
|
||||
await this.db.connect((error) => {
|
||||
if (!error) {
|
||||
console.log(`Solicitud de conexión a base de datos exitosa`)
|
||||
}
|
||||
|
||||
if (error) {
|
||||
console.log(`Solicitud de conexión fallida ${error.stack}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
getPrevByNumber = (from) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const sql = `SELECT * FROM history WHERE phone=${from} ORDER BY id DESC`
|
||||
this.db.query(sql, (error, rows) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
}
|
||||
|
||||
if (rows.length) {
|
||||
const [row] = rows
|
||||
row.options = JSON.parse(row.options)
|
||||
resolve(row)
|
||||
}
|
||||
|
||||
if (!rows.length) {
|
||||
resolve(null)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
save = (ctx) => {
|
||||
const values = [
|
||||
[
|
||||
ctx.ref,
|
||||
ctx.keyword,
|
||||
ctx.answer,
|
||||
ctx.refSerialize,
|
||||
ctx.from,
|
||||
JSON.stringify(ctx.options),
|
||||
],
|
||||
]
|
||||
const sql =
|
||||
'INSERT INTO history (ref, keyword, answer, refSerialize, phone, options ) values ?'
|
||||
|
||||
this.db.query(sql, [values], (err) => {
|
||||
if (err) throw err
|
||||
console.log('Guardado en DB...', values)
|
||||
})
|
||||
this.listHistory.push(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MyslAdapter
|
||||
Reference in New Issue
Block a user