1.0.0 • Published 4 years ago

react-native-store-manager v1.0.0

Weekly downloads
-
License
-
Repository
-
Last release
4 years ago

react-native-store-manager

This library is used to store data into the following data storage: 1. Cache: An object is used to store data to cache. This type of data store uses RAM memory. This kind of datastore is not suggested for saving heavy data. Data is not persisted in the fresh application launch. 2. File system: React native AsyncStorge library is a wrapper on the file system. So AsyncStorge is used to store data to file systems. Data is not persisted in the fresh application launch. 3. User Defaults: react-native-default-preference library is used to store data into User defaults. This library uses bridge method to communicate react-native and SharedPreferences (Android) and UserDefaults (iOS) with React Native 4. DataBase: Realm is used to store information in the database. Realm is a mobile database that runs directly inside phones, tablets, or wearables.

React native library of crypto is used for encryption/decryption. AES encryption is used to encryption data.

Installation

Use the following command to install npm package

npm i react-native-store-manager

Install native dependies.

react-native link react-native-default-preference
react-native link realm
react-native link @react-native-community/async-storage

Usage

Import

import { Storage, storageTypes } from 'react-native-store-manager';

Init

Storage instance is a singleton object. We have to initialize storage instance with default properties. \ Storage type enums: 1. FILE_SYSTEM: File stor type 2. USER_DEFAULT: SharedPreferences (Android) and UserDefaults (iOS) 3. CACHE 4. REALM_DB

Expire: \ expire time, default: 0 day (1000 3600 0 milliseconds). \ if value is null or 0, which means data never expire.

Encrypted: \ If encrypted value is true then AES encryption will be used to encrypt and decryption data.

secertKey: \ secertKey value is used in AES encryption to encrypt/decrypt data

let storageOptions = {
    storeType: storageTypes.FILE_SYSTEM,
    expire: 5 * 2500 *1000 * 24,
    encrypted: true,
    secertKey:'secertKey'
};
const StorageInstance = Storage.instance(storageOptions);

Save

storage.saveItem({
   key: 'admin',
   data: {
    name: 'Govind',
     number: '99999999',
     Address: 'Gurgaon',
   },
   // if expires, store type, encrypted values are not specified, the initialise value will be applied instead.
   storeType: storageTypes.FILE_SYSTEM,
    expire: 5 * 2500 *1000 * 24,
    encrypted: true,
 });

//-----------------------------------------------------------

storage.saveItem({
    key: 'admin',
   data: {
    name: 'Govind',
     number: '99999999',
     Address: 'Gurgaon',
   },
 });

//-----------------------------------------------------------

// id should be unique
storage.saveMultiItem({
  key: 'user',
  data:[{ name: 'Amit',id: '88888888', Address: 'Gurgaon'},
        {name: 'Prasoon', id: '7777777', Address: 'Gurgaon'}],
  expires: 1000 * 60
});

Get

storage.getItem(
   {key: 'admin'},
   (data) => console.warn(data),
   (error) => Alert.alert(error.message),
 );

//-----------------------------------------------------------

storage.getMultiItem(
  {key: 'user', ids: ['1001', '1002']},
   (data) => console.warn(data),
  (error) => Alert.alert(error.message),
 );

Search using prefix

storage.searchMultiItem(
  {key: 'user', id: '100'},
   (data) => console.warn(data),
  (error) => Alert.alert(error.message),
 );

Delete

storage.removeItem(
  {key: 'user', id: '1001'},
   (data) => console.warn(data),
  (error) => Alert.alert(error.message),
 );

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update the tests as appropriate.

License

MIT