import { readFile, writeFile } from 'fs';
* Add a JS Object to an array of Objects in a JSON file
* @param {string} filename - Name of JSON file
* @param {object} jsonContentObj - The content to write to the JSON file
* @param {function} callback - The callback function to execute on error or success
* Callback takes write error as 1st param and JS Object as 2nd param.
export function write(filename, jsonContentObj, callback) {
// Convert content object to string before writing
const jsonContentStr = JSON.stringify(jsonContentObj);
writeFile(filename, jsonContentStr, (writeErr) => {
console.error('Write error', jsonContentStr, writeErr);
// Allow each client app to handle errors in their own way
callback(writeErr, null);
console.log('Write success!');
// Call client-provided callback on successful write
callback(null, jsonContentStr);
* Add a JS Object to an array of Objects in a JSON file
* @param {string} filename - Name of JSON file
* @param {function} callback - The callback function to execute on error or success
* Callback takes read error as 1st param and JS Object as 2nd param.
export function read(filename, callback) {
const handleFileRead = (readErr, jsonContentStr) => {
console.error('Read error', readErr);
// Allow client to handle error in their own way
// Convert file content to JS Object
const jsonContentObj = JSON.parse(jsonContentStr);
// Call client callback on file content
callback(null, jsonContentObj);
readFile(filename, 'utf-8', handleFileRead);
* Add a JS Object to an array of Objects in a JSON file
* @param {string} filename - Name of JSON file
* @param {function} callback - The callback function to execute on error or success
* Callback takes read error as 1st param and JS Object as 2nd param.
export function edit(filename, readCallback, writeCallback) {
// Read contents of target file and perform callback on JSON contents
read(filename, (readErr, jsonContentObj) => {
// Exit if there was a read error
console.error('Read error', readErr);
readCallback(readErr, null);
// Perform custom edit operations here.
// jsonContentObj mutated in-place because object is mutable data type.
readCallback(null, jsonContentObj);
// Write updated content to target file.
write(filename, jsonContentObj, writeCallback);
* Add a JS Object to an array of Objects in a JSON file
* @param {string} filename - Name of JSON file
* @param {string} key - The key in the JSON file whose value is the target array
* @param {string} input - The value to append to the target array
* @param {function} callback - The callback function to execute on error or success
* Callback takes read or write error as 1st param and written string as 2nd param.
export function add(filename, key, input, callback) {
(err, jsonContentObj) => {
// Exit if there was an error
console.error('Edit error', err);
// Exit if key does not exist in DB
if (!(key in jsonContentObj)) {
console.error('Key does not exist');
// Call callback with relevant error message to let client handle
callback('Key does not exist');
// Add input element to target array
jsonContentObj[key].push(input);
// Pass callback to edit to be called after edit completion