1
votes

I am replicating docs from DB A to DB B, every time a Doc from DB A arrives in DB B I want to run a 'stored procedure' to remove most of the fields from DB A (DB A is private, but has attachments that I want to be publicly available)

So far I've seen that this might be achieved using the _changes feed (continuous)and then running an 'update' handler on each document.

The document update handlers doc: https://wiki.apache.org/couchdb/Document_Update_Handlers

This seems like something that CouchDB would implement for me... (and I'm not really sure yet how to do the above).

Is there something like a 'hook' that can be run on every document that enters the database?

== EDIT ==

It seems that I would want to somehow include the update handler command in the replication trigger?

1
You can't use update handlers during replication, you're likely going to need to have a separate process that reads from the _changes feed from DB A and pushes documents into DB B. - Dominic Barnes

1 Answers

1
votes

It sounds like with some changes to how your storing documents you may be able to benefit from CouchDB's filtered replication. You'd need to store the attachments in documents that could be equivalently copied (without modification) between the two databases.

If that's not an option, then you could potentially use transform-pouchdb plus PouchDB's .replicate.from() method to manage the replication.

Some quick pseudo-code for this idea looks a bit like this:

var PouchDB = require('pouchdb');
PouchDB.plugin(require('transform-pouch'));

var dbA = new PouchDB('a'); // "a" could be a URL to CouchDB or Cloudant
var dbB = new PouchDB('b');

dbB.transform({
  incoming: function (doc) {
    // do something to the document before storage
    return doc;
  }
});

dbB.replicate.from(dbA);

In theory, that (or something like it) should do what you're wanting...or at least giving you the framework in which to do what you're wanting. ^_^

Hope that helps!