← All platforms

React Native quickstart

By the end of this page you will have a React Native app with an end-to-end encrypted document store, powered by native Rust-based Automerge and native crypto — no WebAssembly, no JavaScript crypto overhead.

Prerequisites

  • Expo SDK 52+ with a development build (not Expo Go)
  • Node.js 20+
  • iOS 13+ or Android 6.0+ (API level 23+)
Why a dev build?

MindooDB uses native modules (Rust Automerge via JSI, native crypto via NitroModules) that require a compiled binary. Expo Go cannot load native modules, so you need npx expo run:ios or npx expo run:android.

1) Install dependencies

In your existing Expo / React Native project, install MindooDB and the native Automerge backend:

npm install mindoodb react-native-automerge-generated

2) Run the setup helper

This copies required patches (for react-native-quick-crypto and React Native), installs additional dependencies (native crypto, polyfills, patch-package), and prints Metro configuration guidance.

npx mindoodb setup-react-native

Follow the instructions printed by the helper. For the full list of what gets installed, see the React Native guide.

3) Initialize polyfills & native Automerge

Replace your index.js entry point with the following. Polyfills must load first; then the native Automerge backend is registered before any app code runs.

// index.js — FIRST lines, before any other imports
import "./mindoodb-polyfills";

import { UseApi } from "@automerge/automerge/slim";
import { nativeApi } from "react-native-automerge-generated";
UseApi(nativeApi);

import { registerRootComponent } from "expo";
import App from "./App";
registerRootComponent(App);

The mindoodb-polyfills.js file is created by the setup helper in step 2. It provides TextEncoder, TextDecoder, URL, and crypto polyfills.

4) Add MindooDB code

Create a file (e.g. mindoo-demo.js) with the following. The key difference from Node.js: you pass a QuickCryptoAdapter that uses native OpenSSL via react-native-quick-crypto for all cryptographic operations.

import {
  BaseMindooTenantFactory,
  InMemoryContentAddressedStoreFactory,
  QuickCryptoAdapter,
} from "mindoodb";
import * as quickCrypto from "react-native-quick-crypto";

export async function runMindooDemo() {
  // React Native uses native crypto via react-native-quick-crypto
  const cryptoAdapter = new QuickCryptoAdapter(quickCrypto);
  const storeFactory = new InMemoryContentAddressedStoreFactory();
  const factory = new BaseMindooTenantFactory(storeFactory, cryptoAdapter);

  // One call creates admin + user keys, KeyBag, and directory
  const { tenant, adminUser, appUser, keyBag } =
    await factory.createTenant({
      tenantId: "my-first-tenant",
      adminName: "cn=admin/o=demo",
      adminPassword: "admin-password",
      userName: "cn=alice/o=demo",
      userPassword: "user-password",
    });

  // Open a database and create a todo document
  const db = await tenant.openDB("todos");
  const todo = await db.createDocument();
  await db.changeDoc(todo, async (d) => {
    const data = d.getData();
    data.title = "Pay invoices";
    data.done = false;
  });

  // Read it back
  const ids = await db.getAllDocumentIds();
  const loaded = await db.getDocument(ids[0]);
  console.log("Loaded todo:", loaded.getData());
  return loaded.getData();
}

5) Build & run

Native modules require a compiled build. Run one of the following:

# iOS
npx expo prebuild --clean
npx expo run:ios

# Android
npx expo prebuild --clean
npx expo run:android

Call runMindooDemo() from your App component (e.g. in a useEffect). Check the Metro console for:

Loaded todo: { title: 'Pay invoices', done: false }
You now have MindooDB running natively on your device. All cryptographic operations use native Rust (Automerge) and native OpenSSL (crypto) — no WebAssembly, no JavaScript crypto overhead.
Expo Go fallback

Expo Go can be used for quick prototyping but runs crypto in JavaScript, which is significantly slower. For realistic performance, always use a dev build or production build with native modules.

Au-delà des champs simples

Le démarrage rapide ne définit que des champs de premier niveau. Un document MindooDB est un CRDT Automerge, et chaque champ a un type qui détermine la fusion des modifications simultanées : texte collaboratif, texte enrichi, chaînes atomiques, compteurs, horodatages, listes et maps. Dans changeDoc(), vous pouvez aussi modifier des champs imbriqués et des listes (push, splice), et getFieldType() indique comment un champ est stocké.

import { MindooValue } from "mindoodb";

await db.changeDoc(todo, (d) => {
  const data = d.getData();
  data.status = MindooValue.atomic("open"); // replaced as a whole
  data.votes = MindooValue.counter(0);      // concurrent +1s add up
  data.tags = ["home"];
  data.tags.push("urgent");                 // list insert, merges
  d.updateText("title", "Buy oat milk");    // only the changed characters
});

const doc = await db.getDocument(todo.getId());
doc.getFieldType("status"); // "atomic"

Lire et écrire des documents : tous les types de champs avec exemples →