-
Notifications
You must be signed in to change notification settings - Fork 3
Document filters
Patrick Titzler edited this page Sep 1, 2016
·
3 revisions
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,
{
"_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"
}
- 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
- Deploy the service
$ cf push --no-start
- 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
- Register the filter with the service
$ cf set-env couchdb-db-copy-and-transform-service SERVER_FILTER transform_service/exclude_deleted_docs
- Start the service
$ cf start couchdb-db-copy-and-transform-service
###Client-side filters
To use a custom client-side filter,
- 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;
}
};
- Push the updated application to Bluemix.
$ cf push --no-start
- 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
- 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
- Start the application
$ cf start couchdb-db-copy-and-transform-service