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

observable-json-storage

v1.0.5

Published

Easily read & write persistent data in Node & Electron apps

Downloads

11

Readme

ovservable-json-storage

Easily read & write persistent, observable data in Node & Electron apps

npm downloads npm version dependencies

Both Node and Electron lack easy ways to persist data for your application. observable-json-storage implements an API similar to localStorage to read and write JSON objects to your application using observable (RxJS) methods.

Installation

Install observable-json-storage by running:

$ npm install --save observable-json-storage

In Electron, you can require this module from either the main or renderer process (with and without remote).

Documentation

storage.setPath(directory, [replace])

If running in Node, the default directory will be set to the project's root folder.

If running in Electron, it will be the app's default userData location.

By default, directory will be appended relative to the default storage location. Passing true to replace will overwrite the default directory completely.

Kind: static method of storage
Summary: Change the default read/write path
Access: public

| Param | Type | Default | Description | | --- | --- | --- | --- | | directory | String | | directory to change to | | [replace] | Boolean | false | completely replace the directory or not |

Example

const storage = require('observable-json-storage');
// setting path "foo" relative to app's default storage location
storage.setPath('./foo'); // /DEFAULT/PATH/TO/APP/STORAGE/foo

Example (Node)

const storage = require('observable-json-storage');
// completely replace absolute path with Node's root directory
storage.setPath(__dirname, true); // /PATH/TO/NODE/APP

Example

const storage = require('observable-json-storage');
// completely replace absolute path with anything
storage.setPath('/new/path/to/anything', true); // /new/path/to/anything

storage.getPath() ⇒ String

Kind: static method of storage
Summary: Return current read/write path
Returns: String - current read/write path
Access: public
Example

const storage = require('observable-json-storage');

var currentStoragePath = storage.getPath();
console.log(currentStoragePath)

storage.set(key, value)

Kind: static method of storage
Summary: Write JSON data
Access: public

| Param | Type | Description | | --- | --- | --- | | key | String | key | | value | Object | value to save |

Example

const storage = require('observable-json-storage');

storage.set('foobar', { foo: 'bar' }).subscribe(
  function(){
    console.log('successful save');
  },
  function(error) {
    console.log(error)
  }
)

Example (shortened)

const storage = require('observable-json-storage');

storage.set('foobar', { foo: 'bar' }).subscribe().catch((function(err){ console.log(err) }));

storage.get(key) ⇒ Observable.<(Object|Boolean)>

If the key does not exist, false is returned.

Kind: static method of storage
Summary: Read JSON data
Access: public

| Param | Type | Description | | --- | --- | --- | | key | String | key |

Example

const storage = require('observable-json-storage');

storage.get('foobar').subscribe(
  function(data) {
    if (!data) { console.log(`Foobar does not exist`); return; }
    console.log(data);
  },
  function(error) {
    console.log(error);
  }
)

storage.has(key) ⇒ Observable.<Boolean>

Kind: static method of storage
Summary: Check if a key exists
Returns: Observable.<Boolean> - returns true or false if the key exists or doesn't
Access: public

| Param | Type | Description | | --- | --- | --- | | key | String | key |

Example

const storage = require('observable-json-storage');

storage.has('foobar').subscribe(
  function(hasKey) {
     console.log(hasKey);
  },
  function(error) {
     console.log(error);
  }
)

storage.keys() ⇒ Observable.<Array>

Kind: static method of storage
Summary: Get the list of saved keys
Returns: Observable.<Array> - array of key strings
Access: public
Example

const storage = require('observable-json-storage');

storage.keys().subscribe(
  function(keys) {
    console.log(keys);
  },
  function(error) {
    console.log(error);
  }
)

storage.remove(key)

Notice this function does nothing, nor throws any error if the key doesn't exist.

Kind: static method of storage
Summary: Remove a key
Access: public

| Param | Type | Description | | --- | --- | --- | | key | String | key |

Example

const storage = require('observable-json-storage');

storage.remove(key).subscribe(
  function() {
    console.log('removed');
  },
  function(error) {
    console.log(error);
  }
)

Example (shortened)

const storage = require('observable-json-storage');
storage.remove('foobar').subscribe().catch((function(err){ console.log(err) }));

storage.clear()

Kind: static method of storage
Summary: Clear all stored JSON data
Access: public
Example

const storage = require('observable-json-storage');

storage.clear().subscribe(
  function() {
    console.log('cleared');
  },
  function(error) {
    console.log(error);
  }
)

Example (shortened)

const storage = require('observable-json-storage');
storage.clear().subscribe().catch((function(err){ console.log(err) }));

License

The project is licensed under the MIT license.