feat(adapter): implementation of json file adapter

This commit is contained in:
vicente1992
2022-12-02 22:27:31 -05:00
parent 7d41699207
commit 5e1a373730
5 changed files with 537 additions and 422 deletions

View File

@@ -1,3 +1,3 @@
{
"conventionalCommits.scopes": ["hook", "contributing", "cli"]
"conventionalCommits.scopes": ["hook", "contributing", "cli", "adapter"]
}

View File

@@ -9,10 +9,12 @@
"license": "ISC",
"dependencies": {
"dotenv": "^16.0.3",
"mongodb": "^4.11.0"
"mongodb": "^4.11.0",
"stormdb": "^0.6.0"
},
"exports": {
"./mock": "./lib/mock/index.cjs",
"./mongo": "./lib/mongo/index.cjs"
"./mongo": "./lib/mongo/index.cjs",
"./json-file": "./lib/json-file/index.cjs"
}
}

View File

@@ -21,4 +21,13 @@ module.exports = [
},
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()],
},
]

View 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

895
yarn.lock

File diff suppressed because it is too large Load Diff