1.2.0 • Published 8 years ago

json-updater v1.2.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

json-updater

Inserts new object to your json file without overwriting the content.

why use this?

json-updater doesn't rewrite the whole file but appends the new object to your json file object, which makes it much faster.

installation

npm install --save json-updater

API

insert(filename, obj,space, callback)

obj object(s) to insert

The optional space argument formats your obj. Default 0 spaces.

Using default space:

var jsonUpdater = require('json-updater');
var file = __dirname + '/users.json';

var obj = {
	"1": {
   	"first_name": "Will",
       "last_name": "Smith"
   }
};

//you can also insert multiple objects
var multiObj = {
	"2": {
   	"first_name": "Google",
       "last_name": "Chrome"
   },
   "3": {
   	"first_name": "Mozilla",
       "last_name": "Firefox"
   },
   "arr": [
     "foo",
     "bar"
   ]
}

jsonUpdater.insert(file, obj, function (err) {
	if(err) return console.error(err);
   //-- your code
});

With space or tab:

jsonUpdater.insert(file, obj, 1, function (err){
	if(err) return console.error(err);
	//-- your code here...
});

jsonUpdater.insert(file, obj, '\t', function (err) {
	if(err) return console.error(err);
    //-- your code here...
});

read(filename,space, callback)

The callback gets two arguments (err, data) where data is the content of the file

The optional space argument formats the data from callback.

jsonUpdater.read(file, '\t', function (err, data) {
	if(err) return console.log(err);
    console.log(data);
});