Build your first encrypted, offline-first synced document
This is the shortest path from install to a synced document. Keep it local-first; add sync when you’re ready.
Step 0 — Install
npm install mindoodb Steps 1–4 — User, keys, tenant, first document
import {
BaseMindooTenantFactory,
InMemoryContentAddressedStoreFactory,
KeyBag,
PUBLIC_INFOS_KEY_ID,
} from "mindoodb";
const storeFactory = new InMemoryContentAddressedStoreFactory();
const factory = new BaseMindooTenantFactory(storeFactory);
// 1) Create user + KeyBag
const user = await factory.createUserId("CN=alice/O=acme", "user-password");
const keyBag = new KeyBag(user.userEncryptionKeyPair.privateKey, "user-password");
// 2) Create required keys
const adminSigningKeyPair = await factory.createSigningKeyPair("admin-password");
const adminEncryptionKeyPair = await factory.createEncryptionKeyPair("adminenc-password");
const publicInfosKey = await factory.createSymmetricEncryptedPrivateKey("publicinfos-password");
await keyBag.decryptAndImportKey(PUBLIC_INFOS_KEY_ID, publicInfosKey, "publicinfos-password");
// 3) Create tenant + DB
const tenant = await factory.createTenant(
"acme",
adminSigningKeyPair.publicKey,
adminEncryptionKeyPair.publicKey,
"tenant-key-password",
user,
"user-password",
keyBag
);
const db = await tenant.openDB("contacts");
const doc = await db.createDocument();
await db.changeDoc(doc, (d) => { d.getData().name = "John Doe"; });
This is aligned with the usage patterns exercised by the MindooDB test suite.
Step 5 — Sync
// Pull missing encrypted entries from a peer/store:
await db.pullChangesFrom(remoteStore);
// Push what the peer is missing:
await db.pushChangesTo(remoteStore);
// Apply newly pulled changes to local in-memory state:
await db.syncStoreChanges(); Requirements & compatibility
Requires Node.js 20 or later. Use nvm use 20 to switch versions.
Works in modern browsers with Web Crypto API support (Chrome, Firefox, Safari, Edge).
Compatible with React Native for iOS and Android mobile applications.
Frequently asked questions
MindooDB is alpha software — APIs may change without notice. Core functionality (encryption, sync, CRDTs) is stable and well-tested, but we recommend thorough evaluation before production deployment.
- Core encryption and sync protocols
- Document CRDT operations and conflict resolution
- Virtual Views and incremental indexing
- Attachment storage and streaming
- API method names and signatures
- Configuration options and settings
- Internal data structures and formats
When APIs change, you'll need to update your code. We maintain backward compatibility for data formats, so your encrypted data will remain accessible. See migration patterns for guidance.
Troubleshooting
MindooDB operations can fail due to network issues, missing keys, or sync conflicts. Always handle errors gracefully:
try {
await db.pullChangesFrom(remoteStore);
} catch (error) {
if (error.type === 'NETWORK_ERROR') {
// Retry with exponential backoff
} else if (error.type === 'MISSING_KEY') {
// Request key from administrator
}
// Log error for debugging
console.error('Sync failed:', error);
} - Enable debug logging:
MINDOO_LOG_LEVEL=DEBUG - Check document state with
getDocument() - Verify sync status with
getAllIds() - Inspect change history with
iterateDocumentHistory()
See logging documentation for details.