Skip to content

Instantly share code, notes, and snippets.

@gagregrog
Last active October 28, 2020 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gagregrog/dbc39522d31703b91365dabcfbbaf290 to your computer and use it in GitHub Desktop.
Save gagregrog/dbc39522d31703b91365dabcfbbaf290 to your computer and use it in GitHub Desktop.
Storage solution for simple JSON data
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