https://github.com/gxsshallot/react-native-general-storage
The wrapper of AsyncStorage to support multi-level key and key prefix.
https://github.com/gxsshallot/react-native-general-storage
asyncstorage multi-level react-native
Last synced: about 1 year ago
JSON representation
The wrapper of AsyncStorage to support multi-level key and key prefix.
- Host: GitHub
- URL: https://github.com/gxsshallot/react-native-general-storage
- Owner: gxsshallot
- License: mit
- Created: 2018-09-01T02:12:05.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-09-24T06:03:36.000Z (almost 7 years ago)
- Last Synced: 2024-04-25T10:21:47.347Z (about 2 years ago)
- Topics: asyncstorage, multi-level, react-native
- Language: JavaScript
- Homepage:
- Size: 26.4 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# react-native-general-storage
[](https://www.npmjs.com/package/react-native-general-storage)
[](https://travis-ci.org/gaoxiaosong/react-native-general-storage)
[中文说明](https://www.jianshu.com/p/43c2f39992b4)
It is the wrapper of `AsyncStorage` to support multi-level key and key prefix.
## Install
Install by Yarn:
```shell
yarn add react-native-general-storage
```
Install by NPM:
```shell
npm install --save react-native-general-storage
```
## note
`import {AsyncStorage} from 'react-native'` will deprecated in RN Higher version. it move to [react-native-community/async-storage](https://github.com/react-native-community/async-storage)
install dependencies see `react-native-community/async-storage` detail
## Usage
Import the module in the file:
```javascript
import AsyncStorage from 'react-native-general-storage';
```
### Keys Structure
You can use an array of string as a key. Or a string is also valid, and will be converted to an array.
The array of string means the levels of key, such as:
```javascript
key1 = ['App', 'UserInfo'];
key2 = ['App', 'UserSetting'];
```
### Prefix
You can set the common prefix to added into key automatically. In application, we usually used two prefix:
```javascript
const commonPart = '__common__';
const userPart = '__user__';
// When app start, we set the common prefix
AsyncStorage.setPrefix(commonPart, commonPart);
// When user login, we set user prefix as default prefix
const userId = '12345';
AsyncStorage.setPrefix(userPart, userId, true);
// When user logout, we unset user prefix and remove default prefix
AsyncStorage.setPrefix(userPart, null);
```
`setPrefix` function has three parameters:
* key: Key prefix used as identifier when you call interface.
* value: Prefix value used in the storage key. If it is null or undefined, the prefix will be deleted.
* isDefault: Set as default key, only valid when value is not null or undefined.
### Seperator
Default seperator used in storage key. The default value is `'$'`. You can modify it globally:
```javascript
AsyncStorage.defaultSeperator = '#';
```
### Storage Key
The key stored in React Native `AsyncStorage` is composed by prefix, keys and seperator:
```javascript
storageKey = [...prefix, ...keys].join(seperator);
```
### Interface
* set: `(keys, content, prefix) => Promise`
* get: `(keys, prefix) => Promise`
* remove: `(keys, prefix) => Promise`
* merge: `(keys, content, prefix) => Promise`
* clear: `(keys, prefix) => Promise`
* getKeys: `(keys, prefix) => Promise<{string: object}>`
* multiGet: `(keys, prefix) => Promise`
* multiSet: `(keys, values, prefix) => Promise`
* multiRemove: `(keys, prefix) => Promise`
Parameters:
* `keys` is a string or an array of string, you can see `Keys Structure`.
* `prefix` is prefix key. Use default prefix if it is undefined.
* `content` is an object, an array, a string or a number.
**clear** and **getKeys**:
They are manipulate a set of keys, which has same prefix.
If one key is `['App', 'UserInfo']` and another is `['App', 'UserSetting']`, and both is `'userPart'` prefix. When `clear(['App'], userPart)`, they will be clear. Or `getKeys(['App'], userPart)`, they will be returned in promise.