Giter Site home page Giter Site logo

bchr02 / filejson Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 19 KB

Seamlessly syncs a JavaScript value to a JSON file in the background whenever that value changes.

License: MIT License

JavaScript 100.00%
javascript json file database orm proxies document-store nosql document autosave

filejson's People

Contributors

bchr02 avatar martinec avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

martinec

filejson's Issues

using delete to delete an object does not save the results

test case:

var Filejson = require("../filejson/app.js");
var file1 = new Filejson();

file1.load("file1.json", proceed); // file1.json contains {"abc": "123"}

function proceed(error, file) {
    if(error) {
        console.error(error);
        return;
    }

    console.log(file.contents); // outputs {"abc": "123"}

    delete file.contents["msg"];

    console.log(file.contents); // outputs {}, however file1.json still contains {"abc": "123"}

}

Check me out

Hello,

Since I noticed you have posted in other similar modules I thought I would CC you on this announcement.

filejson is a new module that allows you to store a value simply by doing file.contents = value and have that and any future changes automatically saved to a JSON file on your drive. This will allow the variable to persist across app restarts. The important difference between this module and others is that you don't have to call a function every time you want to alter your objects value. So, instead of having to wrap every update in a function/callback and creating a pyramid (aka callback hell):

var jsonfile = require('jsonfile');

var file = '/tmp/data.json';
var obj = {};

jsonfile.writeFile(file, obj, function (err) {
  if(err) {
    console.error(err);
    return;
  }
  obj.msg = "Hello World!";
  jsonfile.writeFile(file, obj, function (err) {
    if(err) {
      console.error(err);
      return;
    }
    obj.user = {"name": "Bob"};
    jsonfile.writeFile(file, obj, function (err) {
      if(err) {
        console.error(err);
        return;
      }
    });
  });
});

With jsonfile you only need to wrap once and that's it:

var Filejson = require("filejson");
var f = new Filejson();

f.load("file.json", function(error, file) {
  if(error) {
    console.error(error);
    return;
  }
  file.contents = {};
  file.contents.msg = "Hello World!";
  file.contents.user = {"name": "Bill"};
});

cc: @pinn3, @tankerkiller125, @hobberwickey, @YounessBenmouloud, @Lazarus118, @Sahas, @ajsingh007, @dudleyjosh, @SCBuergel, @kamikazechaser, @Pizylon, @Egoscio, @maxime1992, @bajtos, @piranna, @sampathsris

To do / things to consider

To do:

  • use streaming approach to JSON so we don't block the thread. Maybe using jimhigson/oboe
  • add more documentation to README.md
  • finish adding JSDoc formatting
  • add tests
  • add benchmarks to determine the performance to access large objects

Things to consider:

  • auto-reload
  • the use of sapienlab/jsonpack (a compression algorithm for JSON)
  • handling of object duplicates and reference cycles. look at cycle.js.
  • add intelligent mapping to split the objects in several files and allow faster loading and access

-

_Pull requests are welcomed! :-) and if you have any additional ideas please post here. Thank you._

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.