balena-settings-storage
Version:
Balena settings storage utilities
185 lines (140 loc) • 6.21 kB
Markdown
balena-settings-storage
----------------------
[](http://badge.fury.io/js/balena-settings-storage)
[](https://david-dm.org/balena-io-modules/balena-settings-storage.png)
[](https://travis-ci.org/balena-io-modules/balena-settings-storage)
[](https://ci.appveyor.com/project/balena-io-modules/balena-settings-storage)
Join our online chat at [](https://gitter.im/balena-io/chat)
Balena settings storage utilities.
Role
----
The intention of this module is to provide low level access to how balena persists settings in both the filesystem and the browser.
**THIS MODULE IS LOW LEVEL AND IS NOT MEANT TO BE USED BY END USERS DIRECTLY**.
Unless you know what you're doing, use the [balena SDK](https://github.com/balena-io/balena-sdk) instead.
Installation
------------
Install `balena-settings-storage` by running:
```sh
$ npm install --save balena-settings-storage
```
Documentation
-------------
* [storage](#module_storage)
* [.getStorage(options)](#module_storage.getStorage) ⇒ <code>storage</code>
* [~set(name, value)](#module_storage.getStorage..set) ⇒ <code>Promise</code>
* [~get(name)](#module_storage.getStorage..get) ⇒ <code>[ 'Promise' ].<\*></code>
* [~has(name)](#module_storage.getStorage..has) ⇒ <code>[ 'Promise' ].<Boolean></code>
* [~remove(name)](#module_storage.getStorage..remove) ⇒ <code>Promise</code>
* [~clear()](#module_storage.getStorage..clear) ⇒ <code>Promise</code>
<a name="module_storage.getStorage"></a>
### storage.getStorage(options) ⇒ <code>storage</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Get an instance of storage module
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| options | <code>Object</code> | options |
| [options.dataDirectory] | <code>String</code> \| <code>False</code> | the directory to use for storage in Node.js or false to create an isolated in memory instance. Values other than false are ignored in the browser. |
**Example**
```js
// with es6 imports
import { getStorage } from 'balena-settings-storage';
// or with node require
const { getStorage } = require('balena-settings-storage');
const storage = getStorage({
dataDirectory: '/opt/cache/balena'
});
```
* [.getStorage(options)](#module_storage.getStorage) ⇒ <code>storage</code>
* [~set(name, value)](#module_storage.getStorage..set) ⇒ <code>Promise</code>
* [~get(name)](#module_storage.getStorage..get) ⇒ <code>[ 'Promise' ].<\*></code>
* [~has(name)](#module_storage.getStorage..has) ⇒ <code>[ 'Promise' ].<Boolean></code>
* [~remove(name)](#module_storage.getStorage..remove) ⇒ <code>Promise</code>
* [~clear()](#module_storage.getStorage..clear) ⇒ <code>Promise</code>
<a name="module_storage.getStorage..set"></a>
#### getStorage~set(name, value) ⇒ <code>Promise</code>
**Kind**: inner method of [<code>getStorage</code>](#module_storage.getStorage)
**Summary**: Set a value
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| name | <code>String</code> | name |
| value | <code>\*</code> | value |
**Example**
```js
storage.set('token', '1234')
```
<a name="module_storage.getStorage..get"></a>
#### getStorage~get(name) ⇒ <code>[ 'Promise' ].<\*></code>
**Kind**: inner method of [<code>getStorage</code>](#module_storage.getStorage)
**Summary**: Get a value
**Returns**: <code>[ 'Promise' ].<\*></code> - value or undefined
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| name | <code>String</code> | name |
**Example**
```js
storage.get('token').then((token) => {
console.log(token)
});
```
<a name="module_storage.getStorage..has"></a>
#### getStorage~has(name) ⇒ <code>[ 'Promise' ].<Boolean></code>
**Kind**: inner method of [<code>getStorage</code>](#module_storage.getStorage)
**Summary**: Check if the value exists
**Returns**: <code>[ 'Promise' ].<Boolean></code> - has value
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| name | <code>String</code> | name |
**Example**
```js
storage.has('token').then((hasToken) => {
if (hasToken) {
console.log('Yes')
} else {
console.log('No')
});
```
<a name="module_storage.getStorage..remove"></a>
#### getStorage~remove(name) ⇒ <code>Promise</code>
**Kind**: inner method of [<code>getStorage</code>](#module_storage.getStorage)
**Summary**: Remove a value
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| name | <code>String</code> | name |
**Example**
```js
storage.remove('token')
```
<a name="module_storage.getStorage..clear"></a>
#### getStorage~clear() ⇒ <code>Promise</code>
**Kind**: inner method of [<code>getStorage</code>](#module_storage.getStorage)
**Summary**: Remove all values
**Access**: public
**Example**
```js
storage.clear()
```
Support
-------
If you're having any problem, please [raise an issue](https://github.com/balena-io-modules/balena-settings-storage/issues/new) on GitHub and the balena team will be happy to help.
Tests
-----
Run the test suite by doing:
```sh
$ npm test
```
Contribute
----------
- Issue Tracker: [github.com/balena-io-modules/balena-settings-storage/issues](https://github.com/balena-io-modules/balena-settings-storage/issues)
- Source Code: [github.com/balena-io-modules/balena-settings-storage](https://github.com/balena-io-modules/balena-settings-storage)
Before submitting a PR, please make sure that you include tests, and that [coffeelint](http://www.coffeelint.org/) runs without any warning:
```sh
$ npm run lint
```
License
-------
The project is licensed under the Apache 2.0 license.