json-updater

1.2.0 • Public • Published

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);
});

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.2.0
    3
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.2.0
    3
  • 1.1.0
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i json-updater

Weekly Downloads

3

Version

1.2.0

License

MIT

Last publish

Collaborators

  • archuanickie