A simple JSON file store

JSON file store

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
var Store = require("jfs");
var db = new Store("data");
var d = {
  foo: "bar"
// save with custom ID 
db.save("anId", d, function(err){
  // now the data is stored in the file data/anId.json 
// save with generated ID 
db.save(d, function(errid){
  // id is a unique ID 
// save synchronously 
var id = db.saveSync("anId", d);
db.get("anId", function(errobj){
  // obj = { foo: "bar" } 
// pretty print file content 
var prettyDB = new Store("data",{pretty:true});
var id = prettyDB.saveSync({foo:{bar:"baz"}});
// now the file content is formated in this way: 
  "foo": {
    "bar": "baz"
// instead of this: 
// get synchronously 
var obj = db.getSync("anId");
// get all available objects 
  // objs is a map: ID => OBJECT 
// get all synchronously 
var objs = db.allSync()
// delete by ID 
db.delete("myId", function(err){
  // the file data/myId.json was removed 
// delete synchronously 

If you want to store all objects in a single file, set the type option to single:

var db = new Store("data",{type:'single'});

or point to a JSON file:

var db = new Store("./path/to/data.json");

If you don't want to persist your data, you can set type to memory:

var db = new Store("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

var db = new Store("data",{saveId:'myKey'});
npm test

This project is licensed under the MIT License.