filesJSON
Open a json file, read the content into this and write the this's content back to the json file. Version that uses Promises!
npm i files-json
const { filesJSON, FileJSON } = require("files-json");
FileJSON
Class:
fileJSON.write()
Returns <Promise>
resolve
<Promise.resolve>
err
<Null> | <Error>
Is an error in case fs.writeFile() had failed.
Promise
that can be awaited and it resolves after the content from fileJSON
has been passed through JSON.stringify() and the string has been written into the json file at the filepath
. In case the json file does not exist the file will be created. Prototype method and prototype properties will never be pasred by JSON.stringify() and therefore are never written into the json file. This allows developers to create a self implemented class that can extend from the FilesJSON
class and make new methods dedicated to a particular configuration file.
fileJSON.close()
Memory
In case thefileJSON
is not closed be aware of abundant memory usage because objects are being stored and not used. When the number of connections to a fileJSON
at a particular filepath
have reached 0 then the fileJSON
will be removed from the internal Map.
data-synchronization
If a json file at that particularfilepath
is actively opened in a fileJSON
object and if the content of that json file at that particular filepath
had been modified outside of the fileJSON
object these modifications do not reflect back to the fileJSON
object. Therefore when a fileJSON
had not been closed when it was not used anymore data may be out of sync. However if the particular json file is never modified outside of the fileJSON
object, there is nothing to worry about.
new FileJSON(filepath)
Returns <Promise>
resolve
<Promise.resolve>
fileJSON
<object>
On reading either the content of a json file will be passed through JSON.parse() or a new empty fileJSON
object is created and passed by the callback
. In case the json file at the filepath
was already opened, that fileJSON
will be be passed over by the callback
. In case the internal new FileJSON()
was still reading and at the same time the same filepath
is opened somewhere else by another new FileJSON()
the latter will be put into a readQueue and return the same object as the first.
fileJSON
has finished creating the fileJSON
.
filesJSON
keys
Thefilepath
values
ThefileJSON
created from new FileJSON()
.
Example
const test1 = async () => {
const monkey1 = await new FileJSON("monkey.json");
monkey1.says = "hoehoehaha";
console.log("test1, monkey1:", monkey1);
// "test1, monkey1: FileJSON { says: 'hoehoehaha' }"
await monkey1.write();
// monkey.json did not exist yet and will be created
const monkey2 = await new FileJSON("monkey.json");
console.log("monkey1 === monkey2:", monkey1 === monkey2);
monkey1.close();
console.log(filesJSON);
monkey2.close();
console.log(filesJSON, "<-- empty, BUT WAIT monkey2-->", monkey2);
// class FileJSON has removed all references to FileJSON("monkey.json")
// However, monkey1 and monkey2 within this scope
// still reference to FileJSON("monkey.json")
};
const test2 = () => {
new FileJSON("monkey.json").then(monkey3 => { // reads data from file
console.log("test2, monkey3:", monkey3);
// "test2, monkey3: FileJSON { says: 'hoehoehaha' }"
monkey3.close();
});
};
(async function test() {
await test1();
// all references to the FileJSON("monkey.json") are now gone, it is garbage collected
test2();
})();