KEN CHEONG
KEN CHEONG

Reputation: 13

Couchbase Lite pull with filtered replication from CouchDB

I did not manage to pull replication data from CouchDB.

I am using CouchDB for my document storage.

CouchDB Version: Apache CouchDB 1.6.1
Couchbase Lite: Couchbase Lite 1.4.0

Below are my sample data:

{
   "_id": "ab33deeb074523e3c63c216b8c2952a8",
   "_rev": "6-e196bfb6aca85492e4f96f3af6fd1ee2",
   "type": "employee",
   "employee": {
       "employeeId": "1",
       "employeeName": "Test"
   }
 }

In CouchDB, I have created my own design filter:

{
   "_id": "_design/employee",
   "_rev": "35-00f59706402452291d30c3fb6e9a5356",
   "filters": {
       "byEmployeeId": "function(doc, req) {
                            if(doc.type != 'employee') {
                                return false;
                            } 

                             if(doc.employee.employeeId == req.query.employeeId) {
                                return true;
                            } else {
                                return false;
                            }
                        }"
   }
}

On the other hand, I am using Couchbase Lite as my Android Mobile project to replication pull the employee data:

Replication pull = this.getCouchbaseUtility().getDatabase().createPullReplication(
                    new URL("http://localhost:5984/testdb")
            );

            pull.setAuthenticator(authenticator);
            pull.setContinuous(false);

            pull.setFilter("employee/byEmployeeId");

            Map<String, Object> params = new HashMap<>();
            params.put("employeeId", "1");

            pull.setFilterParams(params);

            pull.addChangeListener(new Replication.ChangeListener() {
                @Override
                public void changed(Replication.ChangeEvent event) {
                    System.out.println(event.getStatus());
                }
            });

            pull.stop();
            pull.start();

The App runs with no error, but it did not successfully replicate the data to the mobile Couchbase storage.

If I change the design filter to return always true, App can replicate the employee document from CouchDB. Could it be req.query.employeeId not compatible with Couchbase and CouchDB?

May I know any part that I did wrong?

Upvotes: 0

Views: 604

Answers (3)

Fenil
Fenil

Reputation: 1204

Using filter params in query string is not a correct way to implement as query string has its own limitation. Instead one should use selector instead of filter if you are using CouchDB Server 2.0 and above. Only thing you need to make sure at your client side is, set the filter name = _selector and in filter params, pass params in following format -

{
  "selector": {
    "key_name_to_filter": "key_value_to_filter",
    "$or": [
      {
        "$and": [
          {
            "key_1": "value_1"
          },
          {
            "key_2": "value2"
          }
        ]
      },
      {
        "$and": [
          {
            "key_3": "value_3"
          },
          {
            "key_4": "value_4"
          }
        ]
      }
    ]
  }
}

Above format is just an example illustrating you can create complex queries in selector filter.

Upvotes: 1

Puneet Akhouri
Puneet Akhouri

Reputation: 41

Couchbase-lite filters will not work with any other server apart from Couchbase. The reason for the same is that in CouchDb, the _changes REST call is a GET request whereas going ahead from version 1.2, Couchbase-lite has converted the request to POST.

One way could be to fork the repository and make the changes in ChangeTracker.java and change the usePOST boolean variable to false and you will see it starts to work.

But then this is the path that Couchbase has chosen and going forward from version 2.0, the support for all non-Couchbase Backends will totally be removed because they will completely move away from http.

Upvotes: 0

Alan Adesalu
Alan Adesalu

Reputation: 21

Unfortunately, Couchbase lite only allows filtered pull replications when the backend is Couchbase Server.

You cannot perform a filtered replication when the backend is CouchDB, Cloudant or anything else.

Filtered pull from CouchDB, PouchDB or Cloudant Since Couchbase Lite 1.2, filter functions in pull replications with non-Couchbase databases are no longer available. There is an incompatibility in the way the filter parameter is handled in the POST /{db}/_changes request (see #1139).

From Couchbase Lite docs

Upvotes: 1

Related Questions