Skip to content

Document filters

Patrick Titzler edited this page Sep 1, 2016 · 3 revisions

Custom document filters

To use a custom server-side or client-side filter clone the repository

$ git clone https://github.com/ibm-cds-labs/couchdb-db-transform.git
$ cd couchdb-db-transform

and follow the instructions below.

###Server-side filters

To use a custom server-side filter,

  1. Create a JSON file containing the filter definition
{
"_id": "_design/transform_service",
"filters": {
  "exclude_deleted_docs": "function(doc, req) { if(doc._deleted) { return false; }  else { return true; }}",
  "exclude_design_docs": "function(doc, req) { if(doc._id.startsWith('_design/')) { return false; }  else { return true; }}"
},
"language": "javascript"
}
  1. Create the design document in the source database
$ curl -X PUT $SOURCE_COUCH_DB_URL$/_design/transform_service --data-binary @sample_filter_design_docs/server_side_filter_design_doc.json
  1. Deploy the service
$ cf push --no-start
  1. Declare the source and target databases
$ cf set-env couchdb-db-copy-and-transform-service SOURCE_COUCH_DB_URL https://$USERNAME:$PASSWORD@$REMOTE_USERNAME.cloudant.com/$SOURCE_DATABASE_NAME
$ cf set-env couchdb-db-copy-and-transform-service TARGET_COUCH_DB_URL https://$USERNAME:$PASSWORD@$REMOTE_USERNAME.cloudant.com/$TARGET_DATABASE_NAME
  1. Register the filter with the service
$ cf set-env couchdb-db-copy-and-transform-service SERVER_FILTER transform_service/exclude_deleted_docs
  1. Start the service
$ cf start couchdb-db-copy-and-transform-service

###Client-side filters

To use a custom client-side filter,

  1. Create a Javascript file containing the filter definition in the application directory (or the desired subdirectory)
/*
* Sample client-side filter function that removes design documents from the processing pipeline.
* Refer to http://guide.couchdb.org/draft/notifications.html for details about the change parameter
* @param {Object} change - a document change that was received in the change feed
* @returns {Boolean} - true if the document is not a design document, false otherwise
*/
module.exports = function(change) {
  if((! change) || (! change.doc) || (change.doc._id.startsWith('_design/'))) {
  	return false;
  }
  else {
  	return true;
  }
};
  1. Push the updated application to Bluemix.
$ cf push --no-start
  1. Declare the source and target databases
$ cf set-env couchdb-db-copy-and-transform-service SOURCE_COUCH_DB_URL https://$USERNAME:$PASSWORD@$REMOTE_USERNAME.cloudant.com/$SOURCE_DATABASE_NAME
$ cf set-env couchdb-db-copy-and-transform-service TARGET_COUCH_DB_URL https://$USERNAME:$PASSWORD@$REMOTE_USERNAME.cloudant.com/$TARGET_DATABASE_NAME
  1. Register the function by defining environment variable CLIENT_FILTER.
$ cf set-env couchdb-db-copy-and-transform-service CLIENT_FILTER sample_filter_functions/ignore_design_documents.js
  1. Start the application
$ cf start couchdb-db-copy-and-transform-service
Clone this wiki locally