0
votes

I have two instances of CouchDB and each replicates another. Problem here is every time a new database is created, I need to watch for newly created database and POST to create _replicator document for replication.

Is there a way to easily replicate the whole databases from one to another without triggering to insert _replicator document?

2

2 Answers

1
votes

Not that i know. I have something similar. I wrote a little node application to listen to the change notification. If I create a new database the change notification notifies my node application and the node application then creates everything else, e.g replication.

0
votes

Ok here is a snippet of my code, sorry for the german comments :)

/**
 * In die Log-Datei der CouchDB einen neuen
 * Eintrag hinzufügen.
 *
 * @param msg
 *
 * @author Robert Gregat
 */
 var log = function(msg) {
    console.log(JSON.stringify(["log", msg]));
 };


 /**
  * Dies ist eine Funktion die aus dem nano Node Module entnommen 
    wurde.
  *
  * @author https://github.com/dscape/nano
  */
  function _serializeAsUrl(db) {
    if (typeof db === 'object' && db.config && db.config.url &&  db.config.db) {
      return urlResolveFix(db.config.url, encodeURIComponent(db.config.db));
    } else {
      return db;
    }
  }

// CouchDB stellt eine API bereit die über stdin Befehle entgegen 
   nimmt.
var stdin = process.openStdin();

// Das Encoding auf utf8 einstellen.
stdin.setEncoding('utf8');


// Auswerten von empfangenen Daten der CouchDB. Wenn eine neue 
   Datenbank angelegt wurde, enthält das
// JSON Objekt den value created. Tritt dieser Fall ein, wird 
   automatisch aus der design und custom db
// alle Dokumente in die neue Datenbank repliziert.
stdin.on('data', function(d) {
    // Parsen des Ergebnis
    if (d !== null) {
        parsedData.push(JSON.parse(d));
    }
});

/**
 * Die Applikation muss in einer Endlosschleife laufen.
 *
 * @author Robert Gregat
 */
 var checker = function() {
   for (var index in parsedData) {
     if (parsed.type === "created" && parsed.db !== "design" && parsed.db !== "custom") {
       // do stuff
     }
   }
   parsedData = [];

   setTimeout(checker, 1000);
 }

 // Endlosschleife starten.
checker();
  • The function log helps me to do some logging to the couchdb.log file.

  • The function _serializeAsUrl is a function from the node module nano to do some formatting.

  • With process.openStdin() I start to listen for emitted events from the couchdb.

  • With stdin.on() I can capture the emitted events.

  • The function checker takes the collected events and is doing what ever i want based on the delivered information (This function must be in an infinite loop).

In the couchdb config you have to add the following:

[update_notification]   
[name] = nodejs [path/to/your/node_application]

For more information take a look on this site: http://docs.couchdb.org/en/1.6.1/config/externals.html