observable-json-storage

1.0.5 • Public • Published

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.

Dependents (0)

Package Sidebar

Install

npm i observable-json-storage

Weekly Downloads

0

Version

1.0.5

License

MIT

Last publish

Collaborators

  • andrewdw