Sync Gateway filtering not working

Issue - Not able to filter documents at SyncGateway using SyncGateway JSON configuration

Requirement - I need to pass only those documents which have doc.data.edit = “yes” to the couchbase bucket using import_filter. But the import_filter part is not working and instead, it is sending all those documents which have doc.data.edit = “no” as well.

SyncGateway Version - 2.8.0 (Enterprise Version)

Adding sample document which is added to the couchbase bucket

SyncGateway JSON

{
    "log": [  "*" ],
    "databases":
    {
        "my-db":
        {
            "server": "http://127.0.0.1:8091",
            "bucket": "TestBucket",
            "username": "user",
            "password": "password",
            "enable_shared_bucket_access": true,
            "import_docs": true,
            "num_index_replicas": 0,
            "users":
            {
                "GUEST":
                {
                    "disabled": false,
                    "admin_channels":
                    [
                        "*"
                    ]
                }
            },
            "import_filter": 
                `function(doc) {
                    if (doc && doc.data && 'edit' in doc.data && doc.data.edit === 'no') {
                        return false;
                    }else{
                        return true;
                    }
                } `,
            "sync":
                `function (doc) {
                    if (doc && doc.data && 'edit' in doc.data && doc.data.edit === 'yes') {
                        channel (doc.channels);
                    }
                }`
        }
    }
}

Please help me with this as I’m new to this and let me know if anything else is required. Thanks

Hi,

Can you clarify your use-case?

Import filters are designed to allow documents written in the Couchbase Server bucket to be available to mobile clients, which sounds like the opposite of what you want.

To limit what documents end up in the Couchbase Server bucket, you need to have your sync function reject writes when doc.data.edit === 'no'.
You can use throw() for this, or something more advanced like requireAdmin() depending on use-case.

"sync":
    `function (doc) {
        if (doc && doc.data && 'edit' in doc.data && doc.data.edit === 'no') {
            throw({forbidden: "doc is non-editable!"})
        }
    }`
1 Like

Thanks @bbrks using this I’m able to solve the issue

"sync":
    `function (doc) {
        if (doc && doc.data && 'edit' in doc.data && doc.data.edit === 'no') {
            throw({forbidden: "doc is non-editable!"})
        }
    }`

Requirement: Filter documents at SyncGateway so that I will not end up sending all the documents to the CouchBase bucket.

In a nutshell what I’m working on

  • Using PouchDB to store data on the local file system
  • Using SyncGateway to sync edited documents to CouchBase bucket
  • And later form couchBase bucket using eventing to call a rest API call

With this JSON configuration, I’m facing one more issue

{
    "log": [  "*" ],
    "databases":
    {
        "my-db":
        {
            "server": "http://127.0.0.1:8091",
            "bucket": "TestBucket",
            "username": "user",
            "password": "password",
            "enable_shared_bucket_access": true,
            "import_docs": true,
            "num_index_replicas": 0,
            "users":
            {
                "GUEST":
                {
                    "disabled": false,
                    "admin_channels":
                    [
                        "*"
                    ]
                }
            },
            "sync":
               `function (doc) {
                    if (doc && doc.data && 'edit' in doc.data && doc.data.edit === 'no') {
                        throw({forbidden: "doc is non-editable!"})
                    }
                }`
        }
    }
}

The issue is when I delete documents from my PouchDB it deletes the documents in the CouchBase bucket as well

Please suggest how I can stop synced document deletion from the CouchBase bucket.

In a similar fashion, you can make the sync function reject deletions to prevent them from being synced to the bucket from clients.

There is some information and examples about this in the documentation. Hopefully it helps:

1 Like

Thanks, @bbrks 2nd if condition helps in not deleting documents from CouchBase

"sync": 
 `function (doc) {
      if (doc && doc.data && 'edit' in doc.data && doc.data.edit === 'no') {
            throw({forbidden: "doc is non-editable!"})
      }
      if (doc._deleted) {
            throw({forbidden: "doc is non-deletable!"})
      }
  }`

Once again thanks for getting all of this information to me and I really appreciate your quick response.

Hi @satish_kumar

Just so you know Eventing and SGW sharing the same bucket (or keyspace) can create some headaches please refer to Unable to deploy a source mutating Eventing Function when Sync Gateway is deployed on same source bucket for work arounds

Best

Jon Strabala