← All platforms

Browser quickstart

By the end of this page you will have a web app running in your browser that creates an end-to-end encrypted document store using the Web Crypto API — no server-side keys, no plaintext on the wire.

1) Create a project & install

We use Vite as bundler here, but any bundler that supports ESM imports works (webpack, Parcel, etc.).

npm create vite@latest my-mindoodb-app -- --template vanilla
cd my-mindoodb-app
npm install mindoodb

2) Add the MindooDB code

Replace the contents of main.js with the code below. The key difference from Node.js: you import from mindoodb/browser and pass a cryptoAdapter that uses the browser's native Web Crypto API for all cryptographic operations.

import {
  BaseMindooTenantFactory,
  InMemoryContentAddressedStoreFactory,
  createCryptoAdapter,
} from "mindoodb/browser";

async function main() {
  // The browser build uses Web Crypto under the hood
  const cryptoAdapter = createCryptoAdapter();
  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 = "Ship web MVP";
    data.done = false;
  });

  // Read it back
  const ids = await db.getAllDocumentIds();
  const loaded = await db.getDocument(ids[0]);
  return loaded.getData();
}

main().then((data) => {
  console.log("Web todo:", data);
  document.querySelector("#app").innerHTML =
    `<h2>Todo: ${data.title} (done: ${String(data.done)})</h2>`;
});

3) Run the dev server

npm run dev

Open the URL shown in the terminal (usually http://localhost:5173). You should see:

Todo: Ship web MVP (done: false)

Check the browser console for the full object: Web todo: { title: 'Ship web MVP', done: false }

You now have MindooDB running entirely in the browser. All encryption and signing happens client-side via the Web Crypto API — no keys ever leave the browser.

Persistent storage

The example above uses in-memory storage. For data that survives page reloads, swap InMemoryContentAddressedStoreFactory for IndexedDBContentAddressedStoreFactory (also exported from mindoodb/browser).

Meer dan eenvoudige velden

De snelstart zet alleen velden op het hoogste niveau. Een MindooDB-document is een Automerge-CRDT, en elk veld heeft een type dat bepaalt hoe gelijktijdige wijzigingen worden samengevoegd: collaboratieve tekst, rich text, atomaire strings, tellers, tijdstempels, lijsten en maps. Binnen changeDoc() kun je ook geneste velden en lijsten bewerken (push, splice), en getFieldType() vertelt je hoe een veld is opgeslagen.

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"

Documenten lezen en schrijven: alle veldtypen met voorbeelden →