Encrypted Pouch - v0.1.0
    Preparing search index...

    Class EncryptedPouch

    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.

    const db = new PouchDB('myapp');
    const store = new EncryptedPouch(db, 'my-password', {
    onChange: (changes) => {
    changes.forEach(({ table, docs }) => {
    console.log(`${docs.length} docs changed in ${table}`);
    });
    },
    onDelete: (deletions) => console.log('Deleted:', deletions)
    });

    await store.loadAll();
    await store.put('expenses', { _id: 'lunch', amount: 15 });
    const doc = await store.get('expenses', 'lunch');
    Index

    Constructors

    Methods

    • 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.

      Returns Promise<void>

      If documents fail to decrypt (reported via onError callback)

      const store = new EncryptedPouch(db, 'password', { onChange, onDelete });
      await store.loadAll(); // Loads existing docs and starts listening
    • 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.

      Parameters

      • table: string

        Document type/table (e.g., "expenses", "tasks")

      • doc: NewDoc

        Document to store. Include _rev for updates.

      Returns Promise<Doc>

      The saved document with _id and _rev populated

      If there's a revision conflict

      // Create new document
      const doc = await store.put('expenses', { amount: 15, desc: 'Lunch' });

      // Update existing document
      const updated = await store.put('expenses', {
      _id: doc._id,
      _rev: doc._rev,
      amount: 20
      });
    • Retrieves a document by table and ID.

      Parameters

      • table: string

        Document table name

      • id: string

        Document ID within the table

      Returns Promise<Doc | null>

      The decrypted document, or null if not found

      const expense = await store.get('expenses', 'lunch');
      if (expense) {
      console.log(expense.amount);
      }
    • Deletes a document from the specified table.

      Parameters

      • table: string

        Document table name

      • id: string

        Document ID within the table

      Returns Promise<void>

      await store.delete('expenses', 'lunch');
      
    • 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.

      Returns Promise<void>

      await store.deleteAllLocal(); // Clear local data only
      
    • 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().

      Returns Promise<void>

      If sync is not connected

      await store.connectRemote({ url: 'http://localhost:5984/mydb' });
      await store.deleteAllAndSync(); // Delete everything locally and remotely
    • Retrieves all documents, optionally filtered by table.

      Parameters

      • Optionaltable: string

        Optional table name to filter by

      Returns Promise<Doc[]>

      Array of decrypted documents

      const allExpenses = await store.getAll('expenses');
      const allDocs = await store.getAll(); // All tables
    • Connects to a remote CouchDB server for bidirectional sync.

      Parameters

      Returns Promise<void>

      // 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.

      Returns void

    • 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.

      Returns Promise<void>

    • Manually resolves a document conflict by choosing the winning version.

      Parameters

      • table: string

        Document table name

      • id: string

        Document ID within the table

      • winningDoc: Doc

        The document version to keep (must include _rev)

      Returns Promise<void>

      // 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.

      Parameters

      • table: string

        Document table name

      • id: string

        Document ID within the table

      Returns Promise<ConflictInfo | null>

      Conflict information if conflicts exist, null otherwise

      const conflict = await store.getConflictInfo('expenses', 'lunch');
      if (conflict) {
      console.log('Winner:', conflict.winner);
      console.log('Losers:', conflict.losers);
      // Manually resolve the conflict
      await store.resolveConflict('expenses', 'lunch', conflict.winner);
      }
    • Re-subscribes to the PouchDB changes feed.

      Useful after disconnect/reconnect scenarios or if the change feed needs to be restarted.

      Returns void

      store.reconnect(); // Restart change detection