Storage Utility

3.1.3 · active · verified Sun Apr 19

This `storage-utility` package, currently at version 3.1.3, provides a unified and persistent key-value storage solution for both web environments (using `localStorage` by default) and React Native applications (integrating with `AsyncStorage`). It aims to simplify cross-platform storage management. Since its 2.0.0 release, it supports time-based data expiration, allowing developers to store values with a defined lifespan, similar to HTTP cookies. A key feature introduced in version 3.0.0 is the transition to promise-based getter functions, primarily addressed through `GetItemAsync`, to robustly handle the asynchronous nature of storage engines like `AsyncStorage` and prevent race conditions encountered in earlier synchronous approaches. Additionally, the library offers a unique categorization of data into 'volatile' and 'nonVolatile' sets, enabling targeted data cleanup operations, such as clearing all user-specific 'volatile' data upon logout. It has an active release cycle, with significant updates in major versions focusing on stability, performance, and expanded functionality. Its core differentiator lies in its dual-platform support and advanced storage policies like time-based invalidation and data categorization.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing `storage-utility` for React Native (or browser), setting a time-sensitive item, and asynchronously retrieving it.

import React, { useEffect, useState } from 'react';
import { GetItemAsync, SetItem, InitializeStorageUtils } from 'storage-utility';
import AsyncStorage from '@react-native-async-storage/async-storage'; // Ensure this is installed for RN

// This setup would typically be in your app's entry point or root component constructor
// For a browser environment, InitializeStorageUtils can often be skipped.
InitializeStorageUtils({
    storeName: 'MyTestStore',
    engine: AsyncStorage, // Use localStorage for web: window.localStorage
    engineName: 'AsyncStorage' // Use 'localStorage' for web
});

const StorageExample = () => {
    const [value, setValue] = useState(null);
    const key = 'myPersistentData';

    useEffect(() => {
        const storeAndRetrieve = async () => {
            console.log('Attempting to set item...');
            // Store data that expires in 1 minute and is volatile
            SetItem(key, { message: 'Hello from storage!', timestamp: Date.now() }, { span: 1, isNonVolatile: false });
            console.log('Item set. Waiting for retrieval...');

            // Retrieve the data using the async getter
            const stored = await GetItemAsync(key);
            setValue(stored);
            console.log('Retrieved:', stored);
        };

        storeAndRetrieve();
    }, []);

    return (
        <div>
            <h1>Storage Utility Example</h1>
            <p>Stored Value: {value ? JSON.stringify(value) : 'No value found or expired'}</p>
            <p>Check console for logs.</p>
        </div>
    );
};

export default StorageExample;

view raw JSON →