Skip to content

BerendKemper/files-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 

Repository files navigation

filesJSON

Open a json file, read the content into this and write the this's content back to the json file. CAUTION: Versions 2.^0.0 use callbacks!

npm i files-json
const { filesJSON, FileJSON } = require("files-json");

Class: FileJSON

fileJSON.write(callback)

    callback <Function> The callback will be executed 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 the fileJSON 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 particular filepath 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, callback)

    filepath <string> The filepath will be added to the filesJSON object.
    callback <Function>
      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.
    The callback will be executed when the internal fileJSON has finished creating the fileJSON.

filesJSON

keys The filepath
values The fileJSON created from new FileJSON().

Example

const test1 = callback => {
	new FileJSON("monkey.json", monkey1 => {
		monkey1.says = "hoehoehaha";
		console.log("test1, monkey1:", monkey1); 
		// "test1, monkey1: FileJSON { says: 'hoehoehaha' }" 
		monkey1.write(() => { // monkey.json did not exist yet and will be created
			new FileJSON("monkey.json", monkey2 => {
				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")
				callback();
			});
		});
	});
	new FileJSON("monkey.json", monkey4 => {
		console.log("this monkey was queued:", monkey4);
		monkey4.close();
	});
};
const test2 = () => {
	new FileJSON("monkey.json", monkey3 => { // reads data from file
		console.log("test2, monkey3:", monkey3);
		// "test2, monkey3: FileJSON { says: 'hoehoehaha' }"
		monkey3.close();
	});
};
(function test() {
	test1(() => {
		// all references to the FileJSON("monkey.json") are now gone, it is garbage collected
		test2();
	});
})();

About

open a json file, read the content into an object and write the content from the object back to the file

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published