How to set a push replication filter with REST api in Couchbase Lite?

Hello,

I am using Couchbase Lite on Cordova platform and Sync Gateway to sync mobile device data to Couchbase Server, I can make the push replication work, but there are different types of docs in one mobile Couchbase lite database, and I only want to push part of docs to the server.

When user pull some docs to mobile devices, I also want to avoid user to push those docs changes back to the server (single direction replication from server to mobile client).

I searched the forum and know there are methods in ios and android native way by defining and specify a filter, but I can not figure how to define a filter in couchbase lite and specify filter params with RESTful api.

any help will be appreciated!

Hanzhi

On iOS you can define a filter function in a design document, as documented here. That page only talks about using filters for the changes feed, but you can also set a filter on a push replication.

That feature’s not implemented yet on Android, but I think it’s being added soon.

Thanks for your help!

I make a filter like below:

by_uid: function (doc, req) {
            if (!req) {
                return true;
            }

            return doc.t === 'P' && doc.u === req.query.uid;

        }.toString()

Then set filter name and query_params to the sync request, when start sync, there are warnings:
*** WARNING: JS function threw exception: TypeError: undefined is not an object (evaluating ‘a.query.uid’)

I tried several times and found out the second parameter seems is just the query part. so I changed the filter to:

by_uid: function (doc, req) {
            if (!req) {
                return true;
            }

            return doc.t === 'P' && doc.u === req.uid;

        }.toString()

Now it works, not sure if it really works as designed?

“That feature’s not implemented yet on Android, but I think it’s being added soon.”
@hideki pls confirm if push filter works in android?

Basically no REST API to setFilter to DB?

We are below error “No ReplicationFilter registered for filter”.

https://github.com/couchbase/couchbase-lite-java-core/blob/e276993f4fdd4beced1aea942b6a266eb848c648/src/main/java/com/couchbase/lite/replicator/PusherInternal.java#L248

    //create filter
    var docid =  "_design/pushfilter";
    var pushJSON = {
                   filters: {
                                  bystate: function(doc, req) {
                                                 if(doc.state == 'pending' || doc.state == 'deletePending') {
                                                                return true;
                                                 } else {
                                                                return false;
                                                 }
                                  }.toString()
                   }
    };
    cblDB.put(docid, pushJSON, function (err, ok) {
                   Logger.info(JSON.stringify([PUT response = ", err, ok]));    
    });

    //push filter 
     push = {
                   source : dbName,
                   target : remote,
                   continuous : true,
                   filter: "pushfilter/bystate",
                   query_params: { state:  "pending"}
    },

    //design doc created
    [03/06 04:43:02.769] [3] CBLite: ---> Added: {_design/pushfilter #1-f23269fbad6bac335f8cc02aeba6a116 @1} as seq 1
    [03/06 04:43:02.772] [3] Database: [changesSince()] docID=_design/pushfilter seq=1 conflicted=false

    //CBL error
    [03/06 04:43:03.296] [7] Sync: PusherInternal{https://---:---@syncqc7.kodiakhandset.com/ptxdata, push, 8511f}: No ReplicationFilter registered for filter 'pushfilter/bystate'; ignoring

Thanks
Nithin

@Nithin

Filter should be supported by REST API.
https://wiki.apache.org/couchdb/Replication#Filtered_Replication

Thanks,

@hideki we followed the same link and we end up getting

“No ReplicationFilter registered for filter ‘pushfilter/bystate’; ignoring”

error…

REST:

filter: “pushfilter/bystate”,

Native:

String filter { get; set; }

REST:
How is DB setFilter handled in Listener when REST request is made?

Native:

void setFilter(String name, FilterDelegate delegate)

We dont see any rest API for the same… pls confirm…

Example:

//design doc

03-07 04:13:31.279 14314-14837/com.kodiak.ui D/[HTML5LOG]: jscde.ptx.ptxSyncCB()-> processData()-> {"error":null,"data":{"seq":7,"doc":{"_rev": "1-c73ba28c68880272758ebb003ce47e6f",
"_id": "_design/pushfilter",
"filters": {
               "bystate": "function(doc, req) {if(doc.state == \"pending\" || doc.state == \"deletePending\") {return true;} else {return false;}}"
}
},"id":"_design/pushfilter","changes":[{"rev":"1-c73ba28c68880272758ebb003ce47e6f"}]}}


//PUSH Filter 

03-07 04:14:03.789 14314-14837/com.kodiak.ui D/[HTML5LOG]: cblPtxInterface.syncManager()-> doStartPost()-> Start PUSH Sync = {
               "source": "ptxdata",
               "target": {
                              "url": "https://919986279039:8e599e5c26e9ba5a671885d2064d21da@syncqc7.kodiakhandset.com/ptxdata"
               },
               "continuous": true,
               "filter": "pushfilter/bystate"
}

//CBL error

03-07 04:14:03.879 14314-17603/com.kodiak.ui W/System.err: [03/07 04:14:03.892] [12] Sync: PusherInternal{https://---:---@syncqc7.kodiakhandset.com/ptxdata, push, 2f1a4}: No ReplicationFilter registered for filter 'pushfilter/bystate'; ignoring

Thanks
Nithin

.

Hi @Nithin,
I check our source codes, but I am not sure why the javascript filter does not work with this case. Can you file the ticket for this problem?
Thanks!

@jens we sse this issue in iOS as well… pls share your thoughts… are we missing something here?

[03/07 07:49:33:932] [T 0x15dd7990] Type-WARNING , Message-CBLDatabase[<0x15ee1d70>ptxdata]: 'pushfilter/bystate' {at -[CBL_ReplicatorSettings compilePushFilterForDatabase:status:]:240}
[03/07 07:49:34:223] [T 0x15dd7990] Type-WARNING , Message-Exception caught in CBL_Router:
Invalid parameter not satisfying: db

That’s not a Cocoa log message; is it .NET? I don’t work on the .NET version, that would be @borrrden

It’s not .NET, this is phonegap right?