react-native-general-storage
v1.0.5
Published
The wrapper of AsyncStorage to support multi-level key and key prefix.
Downloads
1
Maintainers
Readme
react-native-general-storage
It is the wrapper of AsyncStorage
to support multi-level key and key prefix.
Install
Install by Yarn:
yarn add react-native-general-storage
Install by NPM:
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
install dependencies see react-native-community/async-storage
detail
Usage
Import the module in the file:
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:
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:
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:
AsyncStorage.defaultSeperator = '#';
Storage Key
The key stored in React Native AsyncStorage
is composed by prefix, keys and seperator:
storageKey = [...prefix, ...keys].join(seperator);
Interface
- set:
(keys, content, prefix) => Promise<void>
- get:
(keys, prefix) => Promise<object>
- remove:
(keys, prefix) => Promise<void>
- merge:
(keys, content, prefix) => Promise<void>
- clear:
(keys, prefix) => Promise<void>
- getKeys:
(keys, prefix) => Promise<{string: object}>
- multiGet:
(keys, prefix) => Promise<object[]>
- multiSet:
(keys, values, prefix) => Promise<void>
- multiRemove:
(keys, prefix) => Promise<void>
Parameters:
keys
is a string or an array of string, you can seeKeys 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.