# Class: Persist

## Persist

#### new Persist()

The persistent storage class handles loading and saving data to browser storage.

Source:
  - Persist.js, line 26

### Methods

- (static) addStep(encode, decode, indexopt)
- (static) addStep(encode, decode)
- (static) addStep(obj)
- decode(val, finished)
- driver(val) → {*}
- drop(key, callbackopt)
- encode(val, finished)
- init(db)
- load(key, callbackopt)
- mode(type) → {*}
- persistedSize(target, callback)
- save(key, data, callbackopt)

#### (static) addStep(encode, decode, indexopt)

Adds an encode/decode step to the persistent storage system so that you can add custom functionality.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`encode` | function |  | The encode method called with the data from the previous encode step. When your method is complete it MUST call the callback method. If you provide anything other than false to the err parameter the encoder will fail and throw an error.
`decode` | function |  | The decode method called with the data from the previous decode step. When your method is complete it MUST call the callback method. If you provide anything other than false to the err parameter the decoder will fail and throw an error.
`index` | Number | <optional> | Optional index to add the encoder step to. This allows you to place a step before or after other existing steps. If not provided your step is placed last in the list of steps. For instance if you are providing an encryption step it makes sense to place this last since all previous steps will then have their data encrypted by your final step.

Source:
  - Persist.js, line 251

#### (static) addStep(encode, decode)

Adds an encode/decode step to the persistent storage system so that you can add custom functionality.

##### Parameters:

Name | Type | Description
--- | --- | ---
`encode` | function | The encode method called with the data from the previous encode step. When your method is complete it MUST call the callback method. If you provide anything other than false to the err parameter the encoder will fail and throw an error.
`decode` | function | The decode method called with the data from the previous decode step. When your method is complete it MUST call the callback method. If you provide anything other than false to the err parameter the decoder will fail and throw an error.

Source:
  - Persist.js, line 233

#### (static) addStep(obj)

Adds an encode/decode step to the persistent storage system so that you can add custom functionality.

##### Parameters:

Name | Type | Description
--- | --- | ---
`obj` | function | The object to encode / decode.

Source:
  - Persist.js, line 222

#### decode(val, finished)

Starts a decode waterfall process.

##### Parameters:

Name | Type | Description
--- | --- | ---
`val` | * | The data to be decoded.
`finished` | function | The callback to pass final data to.

Source:
  - Persist.js, line 201

#### driver(val) → {*}

Gets / sets the driver used when persisting data.

##### Parameters:

Name | Type | Description
--- | --- | ---
`val` | String | Specify the driver type (LOCALSTORAGE, WEBSQL or INDEXEDDB)

Source:
  - Persist.js, line 171

##### Returns:

Type
  *

#### drop(key, callbackopt)

Deletes data in persistent storage stored under the passed key.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | String |  | The key to drop data for in the storage.
`callback` | function | <optional> | The method to call when the data is dropped.

Source:
  - Persist.js, line 457

#### encode(val, finished)

Starts an encode waterfall process.

##### Parameters:

Name | Type | Description
--- | --- | ---
`val` | * | The data to be encoded.
`finished` | function | The callback to pass final data to.

Source:
  - Persist.js, line 212

#### init(db)

The init method that can be overridden or extended.

##### Parameters:

Name | Type | Description
--- | --- | ---
`db` | Db | The ForerunnerDB database instance.

Source:
  - Persist.js, line 39

#### load(key, callbackopt)

Loads and decodes data from the passed key.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | String |  | The key to retrieve data from in the persistent storage.
`callback` | function | <optional> | The method to call when the load process has completed.

Source:
  - Persist.js, line 430

#### mode(type) → {*}

Gets / sets the persistent storage mode (the library used to persist data to the browser - defaults to localForage).

##### Parameters:

Name | Type | Description
--- | --- | ---
`type` | String | The library to use for storage. Defaults to localForage.

Source:
  - Persist.js, line 156

##### Returns:

Type
  *

#### persistedSize(target, callback)

determine byte size of a persisted object, either entire DB or specific collection

##### Parameters:

Name | Type | Description
--- | --- | ---
`target` | string | object | either a string (collection name) or an object (DB)
`callback` | persistedSizeCallback | method to call when size determination is done - mandatory for obtaining a result!

Source:
  - Persist.js, line 512

#### save(key, data, callbackopt)

Encodes passed data and then stores it in the browser's persistent storage layer.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | String |  | The key to store the data under in the persistent storage.
`data` | Object |  | The data to store under the key.
`callback` | function | <optional> | The method to call when the save process has completed.

Source:
  - Persist.js, line 396

### Members

- localforage

#### localforage

The local forage library.

Source:
  - Persist.js, line 33