mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-18 11:39:15 +00:00
fix(adapter): json db change is made
This commit is contained in:
@@ -1,27 +1,42 @@
|
||||
const StormDB = require('stormdb')
|
||||
const { join } = require('path')
|
||||
|
||||
const engine = new StormDB.localFileEngine(join(process.cwd(), './db.stormdb'))
|
||||
const { existsSync, writeFileSync, readFileSync } = require('fs')
|
||||
|
||||
class JsonFileAdapter {
|
||||
db
|
||||
pathFile
|
||||
listHistory = []
|
||||
|
||||
constructor() {
|
||||
this.pathFile = join(process.cwd(), 'db.json')
|
||||
this.init().then()
|
||||
}
|
||||
|
||||
init() {
|
||||
return new Promise((resolve) => {
|
||||
this.db = new StormDB(engine)
|
||||
this.db.default({ history: [] })
|
||||
resolve(this.db)
|
||||
})
|
||||
databaseExists() {
|
||||
return existsSync(this.pathFile)
|
||||
}
|
||||
|
||||
async init() {
|
||||
const dbExists = await this.databaseExists()
|
||||
|
||||
if (!dbExists) {
|
||||
const data = {
|
||||
history: [],
|
||||
}
|
||||
await this.saveData(data)
|
||||
}
|
||||
}
|
||||
|
||||
readDatabase() {
|
||||
const db = readFileSync(this.pathFile)
|
||||
return JSON.parse(db)
|
||||
}
|
||||
|
||||
saveData(data) {
|
||||
writeFileSync(this.pathFile, JSON.stringify(data))
|
||||
}
|
||||
|
||||
getPrevByNumber = async (from) => {
|
||||
const response = await this.db.get('history')
|
||||
const { history } = response.state
|
||||
const { history } = await this.readDatabase()
|
||||
|
||||
if (!history.length) {
|
||||
return null
|
||||
@@ -35,12 +50,14 @@ class JsonFileAdapter {
|
||||
}
|
||||
|
||||
save = async (ctx) => {
|
||||
await this.db
|
||||
.get('history')
|
||||
.push({ ...ctx })
|
||||
.save()
|
||||
console.log('Guardado en DB...', ctx)
|
||||
this.db = await this.readDatabase()
|
||||
|
||||
this.db.history.push(ctx)
|
||||
|
||||
await this.saveData(this.db)
|
||||
|
||||
this.listHistory.push(ctx)
|
||||
console.log('Guardado en DB...', ctx)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user