Search code examples
couchbasecouchbase-litecouchbase-sync-gateway

Couchbase: How to remove channel access for a document through Sync function


I'm new to couchbase. I want to update channels for some of the documents through sync functions. But right now, it is not updating but adding an extra channel to the document's meta but not removing the existing channel. Can anyone suggest how I can remove the existing channel in the document?

Sync Function:

function(doc, oldDoc) {
  //....
  if (doc.docType === "differentType") {
    channel("differentChannel");
    expiry(2332800);
    return;
  }
  //.......
}

Document:

{
  "channels": [
    "abcd"
  ],
  "docType": "differentType",
  "_id" : "asjnc"
}

Metadata:

{
  "meta": {
    "id": "asjnc",
    "rev": "64-1b500000000",
    "expiration": 1650383285,
    "flags": 0,
    "type": "json"
  },
  "xattrs": {
    "_sync": {
      "rev": "1-db30e607872",
      "sequence": 777,
      "recent_sequences": [
        777
      ],
      "history": {
        "revs": [
          "1-db30e607872"
        ],
        "parents": [
          -1
        ],
        "channels": [
          [
            "differentChannel"
          ]
        ]
      },
      "channels": {
        "differentChannel": null
      }
    }
  }
}

Expectation of the document with the same metadata:

{
  "channels": [ ], // <--- no channels
  "docType": "differentType",
  "_id" : "asjnc"
}

With this sync function, for the document of type differentType, the channel differentChannel is set in the xattrs section in the metadata. But the channel that was added earlier from the couchbaseLite is not getting removed. Can anyone help?


Solution

  • I answered this in the Couchbase Forums: https://forums.couchbase.com/t/remove-channels-from-a-document/33212

    The "channels" property in a document is counter-intuitively not describing what channels the document is currently in - it's just a user-definable field that happens to be the default routing for channels if you don't specify a sync function. It's up to the writer of the document what it should contain.

    If you have another means of channel assignments (like "docType" in your case), then you don't need to specify "channels" in the document. The sync metadata shows that the document is in "differentChannel" at revision 1-db30e607872 but the contents of the document can be arbitrary.