Skip to content
Auto
English

React Native

A write applies to the cache and waits in an outbox until the engine acknowledges it. In a browser that outbox is IndexedDB, replayed when the page loads. React Native has no IndexedDB: the default queue is memory-only, so a write made in a tunnel, or one still pending when iOS suspends the app, is gone after a relaunch.

AsyncStorageOutbox is the durable version, and it ships in the core: one storage key per pending write, listed back and replayed in the order the writes were made when the app starts again.

Terminal window
npm install @tapedb/tapedb @tapedb/react-native @react-native-async-storage/async-storage

@react-native-async-storage/async-storage (^1.17, ^2 or ^3) is the only peer. There is no React in that line: the durable queue is createClient with a storage, and the hooks live in @tapedb/react, which you add only if you want them. Nothing here imports react-native, so all of it loads anywhere JavaScript runs, and the queue only ever touches the storage you pass it.

The queue does not belong to the React bindings. createClient from @tapedb/tapedb takes it directly, so an app that does not use React for its data has the same durability:

import AsyncStorage from "@react-native-async-storage/async-storage";
import { AsyncStorageOutbox, createClient } from "@tapedb/tapedb";
const db = createClient({
schema,
url,
workspace,
userId,
getToken,
outbox: new AsyncStorageOutbox(AsyncStorage),
});

AsyncStorageOutbox takes the store first and an optional { prefix } second. Pass scopedOutboxName(url, userId) + ":" to scope the queue per account the way the browser scopes its own.

It takes createClient’s options — schema, url, workspace, userId, getToken, onWriteRejected — and returns the same client. The hooks are the same module too: @tapedb/react-native re-exports @tapedb/react unchanged, so useLive, useLivePage, usePresence and useBroadcast import from either name.

import { createReactNativeClient, useLive } from "@tapedb/react-native";
const db = createReactNativeClient({ schema, url, workspace, userId, getToken });
const { data } = useLive(db.task.findMany({ where: { done: false } }));

Two options are added. storage replaces the store behind the queue — anything with getItem, setItem, removeItem and getAllKeys will do, so MMKV behind a promise or a SQLite driver works as well as AsyncStorage. outbox is createClient’s own and still wins: pass one to take the queue over entirely.

An outbox is four methods. AsyncStorageOutbox implements them, and so must anything you inject.

Method Returns Does
add(entry) Promise<void> stores a pending write, replacing an earlier attempt with the same clientTxId
list(workspaceId) Promise<OutboxEntry[]> every pending write for that workspace, oldest first
remove(clientTxId) Promise<void> drops one acknowledged write
clear(workspaceId?) Promise<void> drops one workspace’s writes, or all of them

Pass it to createClient({ outbox }). An injected outbox replaces the browser’s userId requirement: you have already decided where the queue lives and how it is scoped. It also accepts a factory, called once for the client and the $across clients it creates, so every workspace in one app shares one queue.

AsyncStorageOutbox(storage, { prefix }) writes one key per entry, <prefix><clientTxId>, holding the entry as JSON. Three consequences:

  • Order is createdAt, not storage order or key order, so a store that hands keys back differently still replays the writes in the order they were made.
  • A retry of the same optimistic write overwrites its own key instead of queueing a second copy.
  • A key that does not parse is one lost write; it does not wedge the queue.

Scope the queue with prefix: scopedOutboxName(url, userId) + ":", so one account’s pending writes are never replayed with the next account’s claim. createReactNativeClient does that for you; construct the outbox yourself only when you have taken the queue over:

import { AsyncStorageOutbox } from "@tapedb/react-native";
import { createClient, scopedOutboxName } from "@tapedb/tapedb";
const outbox = new AsyncStorageOutbox(myKeyValueStore, {
prefix: scopedOutboxName(url, userId) + ":",
});
const db = createClient({ schema, url, workspace, userId, outbox });

A setItem that throws — a full disk, a locked app group — does not cost the caller the write: the entry stays in the outbox it was written to, where a later flush still sees it. That memory is not durable, though. With the default queue an entry survives a failed write and not the process, so a relaunch loses it. list() merges what it read from storage with those in-memory entries, and the in-memory attempt wins a tie, because it is the later one.

Reads fail the same way: a getAllKeys or getItem that throws falls back to memory for that call. clear() deletes in a batch, through multiRemove on v1 and v2, removeMany on v3, or one key at a time on a store that has neither.

AsyncStorageOutbox accepts @react-native-async-storage/async-storage v1, v2 and v3. The batch delete is multiRemove on v1 and v2 and removeMany on v3, and on a store that offers neither it removes one key at a time.

AsyncStorage 3.1.1’s ESM build imports extensionless paths, so importing it in a plain Node process fails with ERR_MODULE_NOT_FOUND for lib/module/createAsyncStorage. That is a property of that package rather than of this adapter, which never imports it: a bundler or React Native resolves it normally.

See Writes & offline for how the queue behaves in a browser, and React for the hooks.