├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Carl Craig 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | react-native-key-value-store 2 | ============================== 3 | 4 | > A wrapper for React-Native AsyncStorage 5 | 6 | Installation 7 | ------------ 8 | 9 | ``` 10 | npm i --save react-native-key-value-store 11 | ``` 12 | 13 | Basic Usage 14 | ----------- 15 | 16 | ```js 17 | import Storage from 'react-native-key-value-store'; 18 | 19 | let value = await Storage.get('foo', 'default value'); 20 | console.log(value); // should be default value if foo hasn't already been set 21 | 22 | await Storage.set('foo', 'bar'); 23 | 24 | value = await Storage.get('foo'); 25 | console.log(value); // should be 'bar' as it has been set 26 | ``` 27 | 28 | > All methods on the `KeyValueStore` are `async` and therefore return `promises`. 29 | 30 | Methods 31 | ------- 32 | 33 | ### `get(key, defaultValue)` 34 | 35 | This will get a value based on the key provided. You can also set a default value 36 | in case the key does not exist in the store. 37 | 38 | ### `set(key, value)` 39 | 40 | This will set a value based for the key provided. 41 | 42 | ### `merge(key, value)` 43 | 44 | This will merge a value into the existing value in storage 45 | 46 | ### `remove(key)` 47 | 48 | This will delete the key from the store. 49 | 50 | ### `keys()` 51 | 52 | This will return an array of all keys in the store. 53 | 54 | Creating Namespaced Stores 55 | -------------------------- 56 | 57 | The default key value store has a namespace of `@Default` 58 | 59 | You can create your own key value stores with different namespaces 60 | 61 | ```js 62 | import { KeyValueStore } from 'react-native-key-value-store'; 63 | 64 | const appStore = new KeyValueStore('@App'); 65 | ``` 66 | 67 | This will allow you to isolate different key/values, and will also affect the 68 | return value from `keys()` method, as it will check the namespace. 69 | 70 | License 71 | ------- 72 | 73 | react-native-key-value-store is licensed with the MIT license. 74 | 75 | See LICENSE for more details. 76 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import { AsyncStorage } from 'react-native'; 2 | 3 | export class KeyValueStore { 4 | 5 | constructor(namespace = `@Default`, separator = `::`) { 6 | this.namespace = namespace; 7 | this.separator = separator; 8 | } 9 | 10 | async get(key, defaultValue = null) { 11 | let value = defaultValue; 12 | try { 13 | value = await AsyncStorage.getItem(`${this.__createKey(key)}`); 14 | value = JSON.parse(value); 15 | } catch (error) { 16 | value = defaultValue; 17 | } 18 | return value; 19 | } 20 | 21 | async set(key, value) { 22 | return await AsyncStorage.setItem(`${this.__createKey(key)}`, JSON.stringify(value)); 23 | } 24 | 25 | async merge(key, value) { 26 | return await AsyncStorage.mergeItem(`${this.__createKey(key)}`, JSON.stringify(value)); 27 | } 28 | 29 | async remove(key) { 30 | return await AsyncStorage.removeItem(this.__createKey(key)); 31 | } 32 | 33 | async keys() { 34 | let keys = await AsyncStorage.getAllKeys(); 35 | return keys.filter((key) => { 36 | return key.startsWith(this.__createPrefix()); 37 | }).map((key) => { 38 | return key.replace(new RegExp(`^${this.__createPrefix()}`), '') 39 | }); 40 | } 41 | 42 | __createKey(key) { 43 | return `${this.__createPrefix()}${key}`; 44 | } 45 | 46 | __createPrefix() { 47 | return `${this.namespace}${this.separator}`; 48 | } 49 | } 50 | 51 | const defaultKeyValueStore = new KeyValueStore(); 52 | 53 | export default defaultKeyValueStore; 54 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-key-value-store", 3 | "version": "1.0.1", 4 | "description": "A wrapper for React-Native AsyncStorage", 5 | "homepage": "https://github.com/carlcraig/react-native-key-value-store/", 6 | "author": "Carl Craig ", 7 | "license": "MIT", 8 | "main": "index.js", 9 | "keywords": [ 10 | "React Native", 11 | "AsyncStorage", 12 | "data", 13 | "store", 14 | "key value", 15 | "iOS", 16 | "Android" 17 | ], 18 | "files": [ 19 | "index.js", 20 | "package.json", 21 | "README.md", 22 | "LICENSE" 23 | ], 24 | "repository": { 25 | "type": "git", 26 | "url": "https://github.com/carlcraig/react-native-key-value-store.git" 27 | } 28 | } 29 | --------------------------------------------------------------------------------