mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-21 21:19:17 +00:00
feat(adapter): implementation of json file adapter
This commit is contained in:
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"conventionalCommits.scopes": ["hook", "contributing", "cli"]
|
"conventionalCommits.scopes": ["hook", "contributing", "cli", "adapter"]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,12 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dotenv": "^16.0.3",
|
"dotenv": "^16.0.3",
|
||||||
"mongodb": "^4.11.0"
|
"mongodb": "^4.11.0",
|
||||||
|
"stormdb": "^0.6.0"
|
||||||
},
|
},
|
||||||
"exports": {
|
"exports": {
|
||||||
"./mock": "./lib/mock/index.cjs",
|
"./mock": "./lib/mock/index.cjs",
|
||||||
"./mongo": "./lib/mongo/index.cjs"
|
"./mongo": "./lib/mongo/index.cjs",
|
||||||
|
"./json-file": "./lib/json-file/index.cjs"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,4 +21,13 @@ module.exports = [
|
|||||||
},
|
},
|
||||||
plugins: [commonjs()],
|
plugins: [commonjs()],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: join(__dirname, 'src', 'json-file', 'index.js'),
|
||||||
|
output: {
|
||||||
|
banner: banner['banner.output'].join(''),
|
||||||
|
file: join(__dirname, 'lib', 'json-file', 'index.cjs'),
|
||||||
|
format: 'cjs',
|
||||||
|
},
|
||||||
|
plugins: [commonjs()],
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|||||||
47
packages/database/src/json-file/index.js
Normal file
47
packages/database/src/json-file/index.js
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
const Path = require('path')
|
||||||
|
const StormDB = require('stormdb')
|
||||||
|
const engine = new StormDB.localFileEngine(Path.join(__dirname, './db.stormdb'))
|
||||||
|
|
||||||
|
class JsonFileAdapter {
|
||||||
|
db
|
||||||
|
listHistory = []
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.init().then()
|
||||||
|
}
|
||||||
|
|
||||||
|
init() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
this.db = new StormDB(engine)
|
||||||
|
this.db.default({ history: [] })
|
||||||
|
resolve(this.db)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
getPrevByNumber = async (from) => {
|
||||||
|
const response = await this.db.get('history')
|
||||||
|
const { history } = response.state
|
||||||
|
|
||||||
|
if (!history.length) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = history.filter((res) => res.from === from).pop()
|
||||||
|
|
||||||
|
return {
|
||||||
|
...result,
|
||||||
|
options: JSON.parse(result.options),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
save = async (ctx) => {
|
||||||
|
await this.db
|
||||||
|
.get('history')
|
||||||
|
.push({ ...ctx, options: JSON.stringify(ctx.options) })
|
||||||
|
.save()
|
||||||
|
console.log('Guardado en DB...', ctx)
|
||||||
|
this.listHistory.push(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = JsonFileAdapter
|
||||||
Reference in New Issue
Block a user