Creates a new EncryptedPouch instance.
PouchDB database instance
Encryption password (will be derived using PBKDF2 by default)
Optionallistener: PouchListenerOptional callbacks for document changes, deletions, etc.
Optionaloptions: EncryptedPouchOptionsOptional configuration (e.g., passphraseMode)
Loads all existing documents from the database and starts change detection.
This should be called once after creating the EncryptedStore instance. It will decrypt all documents, trigger onChange callbacks (batched by table), and set up real-time change listeners.
Creates or updates a document in the specified table.
If the document has no _id, one will be auto-generated.
If the document has an _id and _rev, it will be updated.
If the _rev doesn't match the current revision, a conflict error is thrown.
Document type/table (e.g., "expenses", "tasks")
Document to store. Include _rev for updates.
The saved document with _id and _rev populated
Retrieves a document by table and ID.
Document table name
Document ID within the table
The decrypted document, or null if not found
Deletes all documents from the local database only.
Automatically disconnects sync first to prevent deletions from propagating to remote. Use this when you want to clear local data without affecting the remote server.
Deletes all documents locally AND propagates deletions to remote server.
Waits for sync to complete before returning.
The remote connection must be established first with connectRemote().
Retrieves all documents, optionally filtered by table.
Optionaltable: stringOptional table name to filter by
Array of decrypted documents
Connects to a remote CouchDB server for bidirectional sync.
Remote server configuration
// Continuous sync (live updates)
await store.connectRemote({
url: 'http://localhost:5984/mydb',
live: true,
retry: true
});
// One-time sync only (manual control)
await store.connectRemote({
url: 'http://localhost:5984/mydb',
live: false,
retry: false
});
await store.syncNow(); // Manually trigger sync
Disconnects from the remote sync server.
Stops continuous sync if it was enabled.
Trigger an immediate one-time sync with the remote. Requires that connectRemote() has been called first. Returns a promise that resolves when the sync completes.
Manually resolves a document conflict by choosing the winning version.
Document table name
Document ID within the table
The document version to keep (must include _rev)
// In onConflict callback
onConflict: async (conflicts) => {
for (const conflict of conflicts) {
// Pick the version with the latest timestamp
const latest = [conflict.winner, ...conflict.losers]
.sort((a, b) => b.timestamp - a.timestamp)[0];
await store.resolveConflict(conflict.table, conflict.id, latest);
}
}
Retrieves conflict information for a document without triggering the callback.
Document table name
Document ID within the table
Conflict information if conflicts exist, null otherwise
Encrypted document store with change detection and sync capabilities.
This class provides a simple API for storing encrypted documents in PouchDB with real-time change detection and optional sync to CouchDB servers.
Example