Last active
October 28, 2020 22:49
-
-
Save gagregrog/dbc39522d31703b91365dabcfbbaf290 to your computer and use it in GitHub Desktop.
Storage solution for simple JSON data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const fs = require('fs-extra'); | |
const ROOT = path.resolve(`${__dirname}/..`); | |
const RESULTS = `${ROOT}/results.json`; | |
if (!fs.pathExistsSync(RESULTS)) { | |
fs.writeJSONSync(RESULTS, []); | |
} | |
const getResults = async () => fs.readJSON(RESULTS); | |
const getLastResults = async () => { | |
const results = await getResults(); | |
return results[results.length - 1] || null; | |
}; | |
const updateResults = async (row) => { | |
const results = await getResults(); | |
results.push(row); | |
await fs.writeJSON(RESULTS, results, { spaces: 2 }); | |
return results; | |
}; | |
module.exports = { | |
getResults, | |
getLastResults, | |
updateResults, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment