observable-json-storage
v1.0.5
Published
Easily read & write persistent data in Node & Electron apps
Downloads
11
Maintainers
Readme
ovservable-json-storage
Easily read & write persistent, observable data in Node & Electron apps
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])
- .getPath() ⇒ String
- .set(key, value)
- .get(key) ⇒ Observable.<(Object|Boolean)>
- .has(key) ⇒ Observable.<Boolean>
- .keys() ⇒ Observable.<Array>
- .remove(key)
- .clear()
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.