4.0.3 • Published 4 years ago

@geofreak/json-storage v4.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

@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
-------------------------------------------------

License

MIT

4.0.3

4 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.2.0

5 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago