An open API service indexing awesome lists of open source software.

https://github.com/alireza29675/handy-storage

🗃️ Handy Storage is a simple way to store your data in a JSON file
https://github.com/alireza29675/handy-storage

database json storage

Last synced: 11 months ago
JSON representation

🗃️ Handy Storage is a simple way to store your data in a JSON file

Awesome Lists containing this project

README

          

![handy-storage logo](https://user-images.githubusercontent.com/2771377/36355655-d20e813a-14fb-11e8-8c15-4741799c9090.png)

# HandyStorage
**HandyStorage** is a simple way to store your data in a JSON file


## Installation
```bash
npm install --save handy-storage
```


## Example of Usage
```javascript
const HandyStorage = require('handy-storage');

const storage = new HandyStorage({
beautify: true
});

storage.connect('./information.json');

storage.setState({
name: 'Alireza',
lastname: 'Sh',
friends: [
'Jane',
'John'
],
visited: storage.state.visited || 0
})

storage.setState({
visited: storage.state.visited + 1
})
```

## Manual Refrence

### HandyStorage
**Kind**: global class

* [HandyStorage](#HandyStorage)
* [new HandyStorage([path])](#new_HandyStorage_new)
* [.connect(path)](#HandyStorage+connect)
* [.state](#HandyStorage+state)
* [.setState([changes])](#HandyStorage+setState)
* [.save([options])](#HandyStorage+save) ⇒ Promise

### new HandyStorage([path], [options])
Represents a "Handy" storage

| Param | Type | Description |
| --- | --- | --- |
| [path] | string | Path of JSON file |
| [options] | Object | Additional Configurations |
| [options.beautify] | boolean | Should storage beautify JSON before storing? _(Default: **false**)_ |
| [options.autoSave] | boolean | Should storage auto save when you use **.setState()** method? _(Default: **true**)_ |

### handyStorage.connect(path)
Connects storage to a JSON file

**Kind**: instance method of [HandyStorage](#HandyStorage)

| Param | Type | Description |
| --- | --- | --- |
| path | string | Path of JSON file |

### handyStorage.state
State object of storage, including all stored data

**Kind**: instance property of [HandyStorage](#HandyStorage)

> **Tip 1**: you can change this object and call [.save()](#HandyStorage+save) to store it into JSON file.

> **Tip 2**: try to use [.setState()](#HandyStorage+setState) method instead of changing **.state** directly! it's much safer, also it supports **autoSave** mode.

### handyStorage.setState(changes) ⇒ Promise
Sets some changes on "state" object then saves it into the file

**Kind**: instance method of [HandyStorage](#HandyStorage)

| Param | Type | Description |
| --- | --- | --- |
| changes | Object | State changes |

**Returns**: Promise - State change callback promise

### handyStorage.save([options]) ⇒ Promise
Saves current state into the connected JSON file

**Kind**: instance method of [HandyStorage](#HandyStorage)

| Param | Type | Description |
| --- | --- | --- |
| [options] | Object | Additional save configurations |
| [options.sync] | boolean | Should save synchronous? _(Default: **false**)_ |

**Returns**: Promise - Saving state callback promise