prabuganesan
prabuganesan

Reputation: 66

Pouchdb filtered replication from couchdb not filtering when I change filter function

I have ionic app with pouchdb and couchdb. I have write filter document in couchdb for sync docs in pouchdb. It will work perfectly. If I change the filter functions in couchdb then filter not working properly.

My couchdb have below documents

Employee

{
  "_id": "employee_2_073489FC-786E-4843-BD3F-B7E191BB532D",
  "_rev": "1-f9f43e1416744675ac14c9f3f7323598",
  "data": {
    "name": "prabu1",
    "empid": "2",
    "type": "employee"
  }
}

Department

{
  "_id": "department_2_DD6D808A-57A8-22A4-A5FF-A610F50AC678",
  "_rev": "2-99fde11786d64ef9928ddca590998588",
  "data": {
    "name": "ios",
    "deptid": "1",
    "type": "department"
  }
}

This my filter document in couch(Design document)

{
  "_id": "_design/live_filter",
  "_rev": "6-d02296cc19f98676cb674339e8563d69",
  "language": "javascript",
  "filters": {
    "live_filter": "function(doc) { if (doc._id === '_design/live_filter') {  return true;  } else if (doc.data) {  if (doc['data'].type === 'employee') {  return true;  } else {   return false;  }  } else {  return false;  } } "
  }
}

Sync code in ionic app

var opt = {
                live: true,
                retry: true,
                filter: "live_filter",
                include_docs: true
            }
this.db.sync(remoteurl, opt)

It is working properly and sync employee document only.

If I change my filter document in couchdb like below(Design document)

{
  "_id": "_design/live_filter",
  "_rev": "6-d02296cc19f98676cb674339e8563d69",
  "language": "javascript",
  "filters": {
    "live_filter": "function(doc) { if (doc._id === '_design/live_filter') {  return true;  } else if (doc.data) {  if (doc['data'].type === 'employee' || doc['data'].type === 'department') {  return true;  } else {   return false;  }  } else {  return false;  } } "
  }
}

Here I have added department object additionally. After this change employee only sync not department doc sync. How to get department docs? If I modify the department doc in couchdb then it will synced.

Upvotes: 0

Views: 418

Answers (1)

Alexis Côté
Alexis Côté

Reputation: 3690

The replication process process the documents in the order that the changes occured.

How the replication works?

So if you're at the sequence 1001 and you update your filter replication, the changes after the sequence 1001 will be processed with the updated filter function.

Replicate from start

If you want to replicate from the first sequence, you need to cancel your replication and start over with the updated filter function.

Note: The document that have been already replicated from be filtered from the target database. They already passed the filter once!

Fresh replicate

If you want the output of your replication to completly reflect your filter function, you need to replicate to a new database.

Upvotes: 0

Related Questions