Node JSON File Storage
A simple, lightweight node.js file storage for JSON data.
Installation
Download this project via Github and add it manually to your project
or install the JSON File Storage npm / yarn package:
npm i node-json-file-storage
/
yarn add node-json-file-storage
How to Use / API
Create a new Store
const JSONFileStorage = require('node-json-file-storage');
const file_uri = __dirname + "/your-storage-name.json";
const storage = new JSONFileStorage(file_uri);
Put
const obj_1 = 'some_content';
const obj_2 = {bar: 'baz'};
const id = storage.put(obj_1);
const ids = storage.putBulk([obj_1, obj_2]);
A Note on the Use of IDs
You can use the storage in db manner (auto-id) const id = storage.put('your_content');
or key-value manner by wrapping your content in an object and providing your own keys in an id field storage.put({id:'your_key', 'your_content'})
.
If you put an object with an id existing in the store, the existing and the new object will be merged, strings etc. will be replaced.
An example: the new object {'id': {'foo': 1, 'bar': 2}}
and the existing {'id': {'foo': 2, 'baz': 4}}
will result in {'id': {'foo': 1, 'bar': 2, 'baz': 4}}
.
Get (Everything)
const obj = storage.get('id_1');
const objs = storage.getBulk(['id_1', 'id_2']);
const everything = storage.all();
Remove (Everything)
const bool = storage.remove('id_1');
storage.remove('id_1');
storage.removeBulk(['id_1', 'id_2']);
storage.clear();
Donate
If you like the project and it saved you a while of coding, spend me a coffee (or all of your savings) to keep me motivated: paypal.me