4.0.3 • Published 4 years ago
@geofreak/json-storage v4.0.3
@geofreak/json-storage
You are looking for an easy way to save and load JSON files in a human-readable structure? Then you should use this module.
You do not need a database, just a directory in the file system.
The way how this module saves the JSON files is very simple. It follows the following structure:
- directory1
- file1.json
- file2.json
- file3.json
- directory2
- file1.json
- file2.json
- ...
- ...
Getting started
Install:
npm i @geofreak/json-storage
Synchronous usage:
const JsonStorage = require(`@geofreak/json-storage`);
// init storage
const store = new JsonStorage(`${__dirname}/storage`);
// set files
store.setSync(`users`, `user001`, { name: `Thomas` });
store.setSync(`users`, `user002`, { name: `Tom` });
// get files
console.log(store.getSync(`users`, `user001`)); // -> { name: `Thomas` }
console.log(store.getSync(`users`, `user002`)); // -> { name: `Tom` }
// iterate over all files
store.forEachSync(`users`, (json, file) => {
console.log(file + `: ` + json.name);
});
// user001: Thomas
// user002: Tom
Asynchronous usage:
const JsonStorage = require(`@geofreak/json-storage`);
// init storage
const store = new JsonStorage(`${__dirname}/storage`);
(async () => {
// set files
await store.set(`users`, `user001`, { name: `Thomas` });
await store.set(`users`, `user002`, { name: `Tom` });
// get files
console.log(await store.get(`users`, `user001`)); // -> { name: `Thomas` }
console.log(await store.get(`users`, `user002`)); // -> { name: `Tom` }
// iterate over all files
await store.forEach(`users`, (json, file) => {
console.log(file + `: ` + json.name);
});
// user001: Thomas
// user002: Tom
})();
Enable git support (git installation required):
// all changes will be committed in a git repository
const store = new JsonStorage(`${__dirname}/storage`, { git: true });
More details are available at the API documentation and at the examples directory.
Documentation
Developer
Install:
git clone https://github.com/brokfeld/json-storage
cd json-storage
npm install
Build API documentation:
npm run build-api-doc
Run the speedtest on your machine:
npm run speedtest 100
npm run speedtest 1000
npm run speedtest 10000
npm run speedtest 100000
Speedest on TeamGroup SSD L5 LITE 240GB
:
#npm run speedtest 1000
--sync-------------------------------------------
setSync 1000 files: 618.236ms
forEachSync 1000 files: 204.298ms
getSync 1 file: 0.251ms
-------------------------------------------------
--async------------------------------------------
set 1000 files: 779.386ms
forEach 1000 files: 287.908ms
get 1 file: 0.253ms
-------------------------------------------------
#npm run speedtest 10000
--sync-------------------------------------------
setSync 10000 files: 7142.441ms
forEachSync 10000 files: 2354.168ms
getSync 1 file: 0.339ms
-------------------------------------------------
--async------------------------------------------
set 10000 files: 11657.295ms
forEach 10000 files: 2011.547ms
get 1 file: 0.229ms
-------------------------------------------------