@molecule/app-storage-async-storage
v1.0.1
Published
AsyncStorage provider for @molecule/app-storage (React Native)
Maintainers
Readme
@molecule/app-storage-async-storage
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
AsyncStorage provider for @molecule/app-storage.
This package provides a React Native AsyncStorage-based implementation of the molecule StorageProvider interface, with support for key prefixing and custom serialization.
Quick Start
import { createAsyncStorageProvider } from '@molecule/app-storage-async-storage'
import { setProvider } from '@molecule/app-storage'
const storage = createAsyncStorageProvider({
prefix: 'myapp_',
})
setProvider(storage)
// Use via `@molecule/app-storage`
import { get, set, remove } from '@molecule/app-storage'
await set('user', { name: 'John' })
const user = await get<User>('user')
await remove('user')Type
provider
Installation
npm install @molecule/app-storage-async-storage @molecule/app-i18n @molecule/app-logger @molecule/app-storage @react-native-async-storage/async-storageAPI
Interfaces
AsyncStorageConfig
AsyncStorage-specific configuration.
interface AsyncStorageConfig {
/**
* Key prefix for all stored values.
* Useful for namespacing storage in shared environments.
*/
prefix?: string
/**
* Custom serializer function.
* @default JSON.stringify
*/
serialize?: <T>(value: T) => string
/**
* Custom deserializer function.
* @default JSON.parse
*/
deserialize?: <T>(value: string) => T
}StorageProvider
Storage provider interface.
All storage providers must implement this interface.
interface StorageProvider {
/**
* Gets a value from storage.
*/
get<T = unknown>(key: string): Promise<T | null>
/**
* Sets a value in storage.
*/
set<T = unknown>(key: string, value: T): Promise<void>
/**
* Removes a value from storage.
*/
remove(key: string): Promise<void>
/**
* Clears all values from storage.
*/
clear(): Promise<void>
/**
* Gets all keys in storage.
*/
keys(): Promise<string[]>
/**
* Gets multiple values from storage.
*/
getMany?<T = unknown>(keys: string[]): Promise<Map<string, T | null>>
/**
* Sets multiple values in storage.
*/
setMany?<T = unknown>(entries: Array<[string, T]>): Promise<void>
/**
* Removes multiple values from storage.
*/
removeMany?(keys: string[]): Promise<void>
}Functions
createAsyncStorageProvider(config)
Creates an AsyncStorage-based storage provider that implements the molecule StorageProvider interface.
Uses React Native's @react-native-async-storage/async-storage as the underlying storage engine.
Supports key prefixing and custom serialization/deserialization.
function createAsyncStorageProvider(config?: AsyncStorageConfig): StorageProviderconfig— Optional configuration for key prefix and serialization.
Returns: A StorageProvider backed by React Native AsyncStorage.
Constants
provider
Default AsyncStorage provider created with no prefix and JSON serialization.
const provider: StorageProviderCore Interface
Implements @molecule/app-storage interface.
Bond Wiring
Setup function to register this provider with the core interface:
import { setProvider } from '@molecule/app-storage'
import { provider } from '@molecule/app-storage-async-storage'
export function setupStorageAsyncStorage(): void {
setProvider(provider)
}Injection Notes
Requirements
Peer dependencies:
@molecule/app-i18n^1.0.1@molecule/app-logger^1.0.1@molecule/app-storage^1.0.1@react-native-async-storage/async-storage^2.0.0
Runtime Dependencies
@molecule/app-i18n@molecule/app-logger@molecule/app-storage@react-native-async-storage/async-storageSet a
prefixin real apps. With no prefix,clear()calls AsyncStorage's global clear — wiping keys owned by OTHER libraries (navigation state, auth SDKs). A prefix scopesclear()andkeys()to this app's entries.get()returnsnull(with a logged warning) on any failure, including a value that fails to deserialize — a corrupted entry is indistinguishable from a missing one at the call site.@react-native-async-storage/async-storageis a peer dependency loaded on first use — install it withnpm install @react-native-async-storage/async-storage.Values round-trip through
JSON.stringify/JSON.parseby default (Dates become strings;undefined/functions are dropped) — pass customserialize/deserializefor anything richer.
