Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including jsondir-livedb with all npm packages installed. Try it out:

var jsondirLivedb = require("jsondir-livedb")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

jsondir-livedb v0.0.3

Watch your filesystem-directories-and-JSON based storage, keep all data in RAM and HDD.

jsondir-livedb

Watch your filesystem-directories-and-JSON based storage, keep all data in RAM and HDD.

Install

$ npm install --save jsondir-livedb

API

Constructor

const LiveDB = require('jsondir-livedb');
var DB = new LiveDB(settings);

Settings is the object containing these keys:

  • root (required) - storage directory
  • instantPush sets whether any change will be applied to storage instantly
  • liveIgnore sets whether changes in filesystem will be ignored (default: false)
  • pathSep sets separator for relative paths in tree (default: '/')
  • unwatch sets whether watcher is not supposed to be used
  • watch represents options for watcher (https://www.npmjs.com/package/watch#watchwatchtreeroot-options-callback)

Usage example

Root folder must be in your working directory already. In example it is called storage.

Now you can put some files into this folder. Be it any folders with markdown and other meta files, library will touch JSON files only.

Dedicated database

const LiveDB = require('jsondir-livedb');
var DB = new LiveDB({
    root: 'storage'
});

Create, check and then delete file

if (DB.set('unnecessary/file.json')) {
    console.log('Succesfully created');

    console.log(DB.get('unnecessary/file.json')); // returns {}

    console.log(DB.tree['unnecessary']['file.json']); // returns {}
    console.log(DB.tree.unnecessary['file.json']); // returns {}

    console.log(DB.tree.unnecessary.file); // returns undefined

    if (DB.delete('unnecessary/file.json')) {
        console.log('Succesfully deleted');
    }
}

Create new JSON file with initial data

DB.set('users/1/common.json', null, {
    name: 'admin',
    password: 'admin',
    class: 5
});

Inspect the structure of tree

console.log(require('util').inspect(DB.tree, {colors: true, depth: 5}));

Add a key

DB.set('users/1/common.json', 'authKey', 'big secret');

Delete some key

DB.delete('users/1/common.json', 'class');

Putting runtime changes to storage

All previous changes were made in runtime only. So make them in the storage too:

DB.push();

Put a setting instantPush: true if you want to apply changes (set, delete) to storage instantly without calling function above.

...
var DB = new LiveDB({
    root: 'storage',
    instantPush: true
});
...

Check live functionality

By default your database has ability to watch and fetch changes in storage in runtime.

Set interval to output JSON file contents:

setInterval(() => {
    var contents = DB.tree.users['1']['common.json'];
    console.log('\n'+ require('util').inspect(contents, {colors: true, depth: 5}));
}, 5000);

Now make some changes in file users/1/common.json via any other program. See the difference.

Remove object

So delete JSON files in users now and push the changes:

DB.delete('users');
DB.push();// if you prefer not to use `instantPush`

Note that folders were not deleted. You can remove empty directories in your storage with tools like ded, remove-empty-directories, etc.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free