npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsondir-livedb

v0.0.3

Published

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

Downloads

2

Readme

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.