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.
Install
npm install jfs --save
Usage
var Store = ;var db = "data"; var d = foo: "bar"; // save with custom IDdb; // save with generated IDdb; // save synchronouslyvar id = db; db // pretty print file contentvar prettyDB = "data"pretty:true;var id = prettyDB;// now the file content is formated in this way: "foo": "bar": "baz" // instead of this:"foo":"bar":"baz" // get synchronouslyvar obj = db; // get all available objectsdball{ // objs is a map: ID => OBJECT}; // get all synchronouslyvar objs = db // delete by IDdb; // delete synchronouslydb;
Single file DB
If you want to store all objects in a single file,
set the type
option to single
:
var db = "data"type:'single';
or point to a JSON file:
var db = "./path/to/data.json";
In memory DB
If you don't want to persist your data, you can set type
to memory
:
var db = "data"type:'memory';
ID storage
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 = "data"saveId:'myKey';
custom ID generator
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.
var i = 0;var db = "data" { i = i + 1; return i; };
Tests
npm test
License
This project is licensed under the MIT License.