← All @molecule/* packages · App templates

@molecule/app-storage-async-storage

Provider bond · storage · App (browser) · v1.0.1 · Apache-2.0

React Native AsyncStorage provider for @molecule/app-storage

npm install @molecule/app-storage-async-storage

npm · Source on GitHub · Implements @molecule/app-storage

How it works

@molecule/app-storage-async-storage is a provider bond on the app (browser) side: it implements the storage core interface (@molecule/app-storage) with a concrete vendor or library behind it.

Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.

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')

Reference

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.ts JSDoc, 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-storage

API

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): StorageProvider
  • config — 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: StorageProvider

Core 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-storage

  • Set a prefix in real apps. With no prefix, clear() calls AsyncStorage's global clear — wiping keys owned by OTHER libraries (navigation state, auth SDKs). A prefix scopes clear() and keys() to this app's entries.

  • get() returns null (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-storage is a peer dependency loaded on first use — install it with npm install @react-native-async-storage/async-storage.

  • Values round-trip through JSON.stringify/JSON.parse by default (Dates become strings; undefined/functions are dropped) — pass custom serialize/deserialize for anything richer.