A simple JSON file store for node.js.
WARNING: Don't use it if you want to persist a large amount of objects. Use a real DB instead.
npm install jfs --save
varStore=require("jfs");vardb=newStore("data");vard={foo: "bar"};// save with custom IDdb.save("anId",d,function(err){// now the data is stored in the file data/anId.json});// save with generated IDdb.save(d,function(err,id){// id is a unique ID});// save synchronouslyvarid=db.saveSync("anId",d);db.get("anId",function(err,obj){// obj = { foo: "bar" }})// pretty print file contentvarprettyDB=newStore("data",{pretty:true});varid=prettyDB.saveSync({foo:{bar:"baz"}});// now the file content is formated in this way:{"foo": {"bar": "baz"}}// instead of this:{"foo":{"bar":"baz"}}// get synchronouslyvarobj=db.getSync("anId");// get all available objectsdb.all(function(err,objs){// objs is a map: ID => OBJECT});// get all synchronouslyvarobjs=db.allSync()// delete by IDdb.delete("myId",function(err){// the file data/myId.json was removed});// delete synchronouslydb.delete("myId");If you want to store all objects in a single file,
set the type option to single:
vardb=newStore("data",{type:'single'});or point to a JSON file:
vardb=newStore("./path/to/data.json");If you don't want to persist your data, you can set type to memory:
vardb=newStore("data",{type:'memory'});By default the ID is not stored within your object.
If you like, you can change that behavior by setting saveId to true
or a custom ID
vardb=newStore("data",{saveId:'myKey'});We use uuid v4 for ID generation if you don't pass an id when save a data. If you want, you can pass custom generator.
vari=0;vardb=newStore("data",{idGenerator: function(){i=i+1;returni;}});npm test
This project is licensed under the MIT License.