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.
npm install storage-utilityVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing `storage-utility` for React Native (or browser), setting a time-sensitive item, and asynchronously retrieving it.
Always use `await GetItemAsync('key')` for retrieving values, especially when using AsyncStorage or in contexts where values might be fetched asynchronously. If using `GetItem`, be aware it might return a Promise and should be awaited.Call `InitializeStorageUtils({ engine: AsyncStorage, engineName: 'AsyncStorage' })` once at the root of your React Native application or component. For web, it can be skipped unless you need to override the default `localStorage` engine or `storeName`.Review `SetItem` calls and update to `SetItem(key, payload, { span: <minutes>, isNonVolatile: false })` if time-based expiration or non-volatile categorization is intended.For data that should not be automatically purged by volatile cleanup mechanisms, explicitly set `isNonVolatile: true` in the options object: `SetItem(key, payload, { isNonVolatile: true })`.Ensure `InitializeStorageUtils` is called at application startup with a valid storage engine. For React Native, this means `InitializeStorageUtils({ engine: AsyncStorage, engineName: 'AsyncStorage' });` after importing `AsyncStorage`.Always `await` the result of `GetItemAsync('key')`. If using `GetItem` and encountering this, switch to `GetItemAsync` or ensure `await` is applied if `GetItem` is configured with an async engine.Install `@react-native-async-storage/async-storage` (`npm install @react-native-async-storage/async-storage`) and `import AsyncStorage from '@react-native-async-storage/async-storage';` in your React Native files.