← All platforms

Node.js quickstart

By the end of this page you will have a running Node.js script that creates an end-to-end encrypted document store, writes a todo, and reads it back — all in under 5 minutes.

1) Install

Create a new project (or use an existing one) and install MindooDB. Node.js 20+ is required.

npm init -y
npm install mindoodb

2) Create index.mjs

Paste the following into index.mjs. The createTenant call generates all cryptographic keys (admin signing key, user signing key, encryption keys, tenant key) and registers the first user in a single step. After that, we open a database and create a document — every change is automatically signed and encrypted.

import {
  BaseMindooTenantFactory,
  InMemoryContentAddressedStoreFactory,
} from "mindoodb";

async function main() {
  const storeFactory = new InMemoryContentAddressedStoreFactory();
  const factory = new BaseMindooTenantFactory(storeFactory);

  // 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 = "Buy milk";
    data.done = false;
  });

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

main();

3) Run

node index.mjs

Expected output:

Loaded todo: { title: 'Buy milk', done: false }
You now have a local, end-to-end encrypted document store running in Node.js. Every change is cryptographically signed and encrypted before it is stored.

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 →