3.1.3 • Published 4 years ago

storage-utility v3.1.3

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

STORAGE UTILITY

Target - Front End Projects

  • Supported Storage Engines - localStorage, React-Native's AsyncStorage

How do I get set up?

  • Run 'npm i storage-utility --save'
  • If using in React Native, setup environment by calling InitializeStorageUtils at the root component's constructor
    InitializeStorageUtils({
        storeName: 'Test', // default - 'STORAGE_UTILITY'
        engine: AsyncStorage, // default - localStorage
        engineName: 'AsyncStorage' // identifier, default - 'localStorage'
    });

Sample

    import React from 'react';
    import { GetItem, SetItem } from 'storage-utility';

    const StorageTest = () => {

        testFunction() { 
            SetItem('testVar', { anything: 'any value' });
            const storagevalue = GetItem('testVar');
            console.log(storagevalue);
        }
    }

APIs

  • InitializeStorageUtils - setup env for package (can be skipped in case of web )

  • NOTE - incase of web, this method can be skipped unless there is need to override the storeName

  
    /**
     * sets up environment for storage engine
     * useful when using in react native (asyncStorage)
     * @param  {object} {engine - AsyncStorage object in case of react native. 
     * @param  {string} engineName} - name of engine is required to distinguish the platform and fetch data according
     * // since asyncstorage works asynchronously, hence its important to send engineName, which bydefault is localStorage
     * @param  {string} storeName{optional} - all the values would be stored under <storeName> keyword
     */
    InitializeStorageUtils({ engine, engineName, storeName });

    // sample api call
    InitializeStorageUtils({ engine: AsyncStorage, engineName: 'AsyncStorage', storeName: 'TestStorage' });
  • SetItem('key', 'value', { span, nonVolatile = false }) - Stores under volatile category, if nonVolatile is true, stores given value under nonvolatile category
    /**
     * Setter in storage engine
     * Stores value against the keyword. 
     * SetItem also takes span(in minutes), which is timeduration after which value under the key will become stale and flushedout
     * it works as cookie which validates against the given time
     * @param  {string} key - key value against which value is stored and being fetched by passing same key
     * @param  {any} payload - payload is the data to be stored
     *  {
            * @param  {number} span() - span is the value in minutes which is the life duration of the data, once this time is passed, data is flushed out
            * @param  {boolean} isNonVolatile=false}= - all data are broadly stored under two category, volatile and nonVolatile
            * reason for doing so is that there might be a usecase when business wants to delete particular set of data after certain activity for e.g. after logout we would like to delete all the user related data from storage
            * having this categorisation makes it easy to delete all the volatile data after some activity has happened
    *  }
    */
    SetItem(key, payload, { timestamp = new Date().getTime(), span, isNonVolatile = false });


    // sample api calls
    SetItem('test', 1);

    SetItem('temporayData', 1, { span: 1 }) // span will cause data to be flushed out after 1 minute
  • GetItem('key', nonVolatile = false) - Returns value from the storage
    /**
     * returns data stored under the provided key
     * @param  {string} key 
     * @param  {boolean} nonVolatile - (optional)
     */
    GetItem(key, isNonVolatile = false)

    // sample api call
    const storedValue = GetItem('test');
    const temporaryStoredValue = GetItem('temporaryData');
  • await GetItemAsync('key', nonVolatile = false) - Asynchronously returns value from the storage
    /**
     * returns data stored under the provided key
     * @param  {string} key 
     * @param  {boolean} nonVolatile - (optional)
     */
    await GetItemAsync(key, isNonVolatile = false)

    // sample api call
    const storedValue = await GetItemAsync('test');
    const temporaryStoredValue = await GetItemAsync('temporaryData');
  • RemoveItem({ clearVolatileStorage = true, clearNonVolatileStorage = false }) - Removes item from particular category.
    /**
     * Removes localstorage value
     * based on parameter, can remove particular key or whole volatile or nonVolatile storage from localStorage
     * @param  {boolean} clearLocalStorage - (optional)
     * @param  {boolean} clearNonVolatileStorage} - (optional)
     */
    RemoveItem({ clearVolatileStorage, clearNonVolatileStorage })

Document Author

Shubham_Kesarwani

3.1.3

4 years ago

3.1.2

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago