PouchDB Replicator Plugin

4.2.0 · active · verified Wed Apr 22

pouchdb-replicator is a PouchDB plugin that simulates the functionality of CouchDB's `_replicator` database daemon. Instead of initiating one-off or programmatic replication calls (e.g., `db.replicate()`), this plugin allows developers to define and manage persistent replication jobs declaratively by simply writing documents to a special `_replicator` database. This enables scenarios like continuous synchronization across application restarts and simplifies the management of complex replication topologies. The current stable version is 4.2.0, part of the pouchdb-server monorepo. It receives regular maintenance updates, focusing on bug fixes, dependency updates, and minor enhancements. Its key differentiator is providing a CouchDB-compatible declarative replication interface directly within PouchDB, making it ideal for applications requiring robust offline-first capabilities and seamless synchronization with CouchDB or other PouchDB instances.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to register `pouchdb-replicator` as a plugin and then initiate a continuous replication job by creating a document in the special `_replicator` database. This simulates CouchDB's declarative replication management.

import PouchDB from 'pouchdb';
import PouchDBReplicator from 'pouchdb-replicator';

// Register the PouchDB Replicator plugin
PouchDB.plugin(PouchDBReplicator);

async function setupPersistentReplication() {
  // Initialize a local PouchDB database
  const localDb = new PouchDB('my_local_db');

  // Define a remote PouchDB/CouchDB instance URL
  const remoteDbUrl = 'http://localhost:5984/my_remote_db'; // Ensure CouchDB is running or this is a valid PouchDB instance

  // The special _replicator database to manage replication jobs
  const replicatorDb = new PouchDB('_replicator');

  try {
    // Create a continuous replication job by posting a document to _replicator
    // This will instruct PouchDB-Replicator to start and manage the replication.
    const replicationJob = {
      _id: 'my-first-continuous-sync',
      source: localDb.name,        // Source database: 'my_local_db'
      target: remoteDbUrl,       // Target database: 'http://localhost:5984/my_remote_db'
      continuous: true,          // Keep replication running continuously
      live: true,                // Also track future changes (often implied by continuous)
      retry: true,               // Automatically retry on failure
      // For authenticated replication to a remote CouchDB:
      // auth: {
      //   username: process.env.COUCHDB_USERNAME ?? 'admin',
      //   password: process.env.COUCHDB_PASSWORD ?? 'password'
      // }
    };

    const response = await replicatorDb.put(replicationJob);
    console.log('Replication job started successfully:', response);

    // To stop or update the replication, you would modify or delete this document from _replicator
    // e.g., await replicatorDb.remove(response.id, response.rev);

  } catch (error: any) {
    console.error('Failed to set up replication job:', error);
    // Handle common errors like network issues, authentication failures, etc.
  }
}

setupPersistentReplication();

view raw JSON →