Community Health Toolkit

Syncing Documents from PouchDB

I recently implemented some logic within a view to limit the documents that sync to pouchDB by date and type. The view is working as expected, but it also turns out that the view was also replicated and is limiting documents from being synced upwards to CouchDB. PouchDB ignored the documents and synced those that were not limited by the view, and created a checkpoint. Now trying to sync the other documents that were not synced before the checkpoint is not working, because I assume Pouchdb is doing an incremental push. How can I sync these documents without altering the application using PouchDB?

Hi atria!

Could you please share some more details about:

  1. What your view does? (actual code) and where’s it’s added.
  2. How you actually do the sync itself? Where is this sync code located? Which endpoints are you calling?

@atria share requested details :point_up:

@diana @kitsao, The view i worked with is http://domain_name/_utils/#database/medic/_design/medic/_view/docs_by_replication_key/edit

and below is the function

// filtering for chv
if (doc.form == "death_report_undo_approval_request" || doc.form == "death_report_approval_request" || doc.form == "death_report_confirmation" ||  doc.form == "death_report_reject" ||
  doc.form == "death_report_confirm" || doc.form == "death_report_undo_confirm" || doc.form == "death_report_undo_reject" ||
  doc.form == "muting-approve-request" || doc.form == "household_mute_task" || doc.form == "muting_approved" || doc.form == "muting_rejected"  && doc.reported_date >= Date.now() - 2592000000){
  return;
}
// filtering for chv 
if(doc.form == "assessment" || doc.form == "muac_follow_up" || doc.form == "u1_follow_up" || doc.form == "assessment_follow_up" || doc.form == "assessment_cbds" || doc.form == "family_survey" || doc.form == "home_visit" &&  doc.reported_date >= Date.now() - 1546300800000){
  return;
}

shared screenshot shows where it as implemented

Regarding syncing, Once logged in, Clicking on “sync now” as seen in the screenshot below enabled syncing of some documents from PouchDB to CouchDB

Hi @atria

Your assessments are correct:

  1. the docs_by_replication_key view affects both upwards and downwards replication. This cannot be changed without major framework changes.
  2. If you edit this view in a significant way, your users need to wipe their devices (or reset their checkpointer and restart replication) to get the correct list of docs. This, again, cannot be changed, without major framework changes.

If you’d like to limit replication of certain docs, you could use purging to filter out some unwanted docs.
However, purging does come with some performance impact on the server, especially on high doc counts.
Please have a look at documentation for purging: Purging | Community Health Toolkit

1 Like

Thanks for sharing this, will investigating purging using the documentation you mentioned