Git Product home page Git Product logo

node-jsondir-livedb's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.