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

https://github.com/tiaanduplessis/react-native-modest-storage

💾 A modest wrapper around the AsyncStorage API
https://github.com/tiaanduplessis/react-native-modest-storage

asyncstorage asyncstorage-wrapper modest react-native

Last synced: 10 months ago
JSON representation

💾 A modest wrapper around the AsyncStorage API

Awesome Lists containing this project

README

          

# 💾 react-native-modest-storage 💾

[![package version](https://img.shields.io/npm/v/react-native-modest-storage.svg?style=flat-square)](https://npmjs.org/package/react-native-modest-storage)
[![package downloads](https://img.shields.io/npm/dm/react-native-modest-storage.svg?style=flat-square)](https://npmjs.org/package/react-native-modest-storage)
[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![package license](https://img.shields.io/npm/l/react-native-modest-storage.svg?style=flat-square)](https://npmjs.org/package/react-native-modest-storage)
[![make a pull request](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

> A modest wrapper around the AsyncStorage API

## Table of Contents

- [About](#about)
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Contribute](#contribute)
- [License](#License)

## About

This module is just a modest wrapper around [React Native](https://facebook.github.io/react-native/)'s [AsyncStorage](https://facebook.github.io/react-native/docs/asyncstorage.html) API. It's purpose is to provide a cleaner API for persisting data.

## Install

This project uses [node](https://nodejs.org) and [npm](https://www.npmjs.com).

```sh
$ npm install react-native-modest-storage
$ # OR
$ yarn add react-native-modest-storage
```

## Usage

```js
import storage from 'react-native-modest-storage'

async function doStuff() {
await storage.set('aKey', { Hello: 'World'})
await storage.set('otherKey', 5)
await storage.get('aKey').then(console.log) // {Hello: "World"}
await storage.keys().then(console.log) // ['otherKey', 'aKey']
await storage.update('aKey', {Hello: 'is it me your looking for.'})
await storage.get('aKey').then(console.log) // {Hello: "is it me your looking for."}
await storage.remove('otherKey')
await storage.get('otherKey').then(console.log) // null
await storage.set([['key1', {foo: 'bar'}], ['key2', 'baz'], ['test', {obj: 9}]])
await storage.get('key1').then(console.log) // {foo: "bar"}
await storage.get('test').then(console.log)
await storage.clear()
}

doStuff()
```

## API

#### Table of Contents

- [get](#get)
- [Parameters](#parameters)
- [Examples](#examples)
- [set](#set)
- [Parameters](#parameters-1)
- [update](#update)
- [Parameters](#parameters-2)
- [remove](#remove)
- [Parameters](#parameters-3)
- [Examples](#examples-1)
- [clear](#clear)
- [keys](#keys)

### get

Retreive value from AsyncStorage based on key.
Wrapper around getItem & multiGet.

#### Parameters

- `key`
- `def` **Any** Default value

#### Examples

```javascript
storage.get('foo').then(console.log).catch(console.error)
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** value of key

### set

Persist a value to AsyncStorage.
Wrapper around setItem & multiSet.

#### Parameters

- `key`
- `value` **Any** to persist

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**

### update

Update key with value by merging.
Wrapper around mergeItem & multiMerge.

#### Parameters

- `key`
- `value` **any** to update

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**

### remove

Remove a key from AsyncStorage.
Wrapper around removeItem & multiRemove.

#### Parameters

- `key`

#### Examples

```javascript
storage.remove(key).then(console.log).catch(console.error)
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**

### clear

Erase all AsyncStorage data.

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**

### keys

Retreive all the keys stored in AsyncStorage of application.
Wrapper around getAllKeys

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**

## Contribute

1. Fork it and create your feature branch: git checkout -b my-new-feature
2. Commit your changes: git commit -am 'Add some feature'
3. Push to the branch: git push origin my-new-feature
4. Submit a pull request

## License

MIT