1

The object returned by GET {db}/_design/{ddoc} injects a whole bunch of extra validation syntax into the 'map' value for a view which, when returned to the server in a PUT request, invalidates the view.

For example:

PUT {db}/_design/{ddoc}
{'views': 
  {'_test': 
    {'map': 
      'function(doc, meta) { if (doc.uid == "test1") { emit(null, null); } }'
    }
  }
}

creates:

GET {db}/_design/{ddoc}

{
'views': {
    '_test': {
        'map': 'function(doc,meta) {\n\t\t
            var sync = doc._sync;\n\t\t
            if (sync === undefined || meta.id.substring(0,6) == "_sync:")\n\t\t
            return;\n\t\t
            if ((sync.flags & 1) || sync.deleted)\n\t\t
            return;\n\t\t
            var channels = [];\n\t\t
            var channelMap = sync.channels;\n\t\t\t\t\t\t\t
            if (channelMap) {\n\t\t\t\t\t\t\t\t
            for (var name in channelMap) {\n\t\t\t\t\t\t\t\t\t
            removed = channelMap[name];\n\t\t\t\t\t\t\t\t\t
            if (!removed)\n\t\t\t\t\t\t\t\t\t\t
            channels.push(name);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t
            delete doc._sync;\n\t\t
            meta.rev = sync.rev;\n\t\t
            meta.channels = channels;\n\n\t\t
            var _emit = emit;\n\t\t
            (function(){\n\t\t\t
            var emit = function(key,value) {\n\t\t\t
            \t_emit(key,[channels, value]);\n\t\t\t
            };\n\t\t\t\t\t\t\t\t
            (function(doc, meta) { if (doc.uid == "test1") { emit(null, null); } }) (doc, meta);\n\t\t\t\t\t\t\t
            }());\n\t\t\t\t\t\t\t
            doc._sync = sync;\n\t\t\t\t\t\t}'
        }
    }
}

}

And, if this new object is returned in a subsequent design document PUT request, the view is not valid and effectively becomes eliminated.

This presents a problem when trying to update a design doc with a new view without removing all the previous views associated with it. Either all the (user defined) view definitions need to be stored somewhere or the application needs to parse the object returned by GET and remove all the junk code before re-submitting the updated design document.

R J
  • 4,473
  • 2
  • 22
  • 29

1 Answers1

0

Sync Gateway needs to wrap Couchbase Server views in order to add application-level security.

Are you always accessing the views through Sync Gateway? Eg, when you say you are trying to update the design doc, is this through Sync Gateway or are you going through Couchbase Server? The former is the recommended approach.

tleyden
  • 1,942
  • 2
  • 12
  • 17
  • That makes sense. In my case, both the GET and the PUT are calls through the Sync Gateway port. So, I am using your recommended approach... would it make a difference if the request went directly to CS? – R J Dec 14 '17 at 02:16
  • Maybe I'm not understanding the original problem, can you post a sequence of curl statements that reproduces the problem you are seeing? If they are too long to post directly in SO, can you put them into a gist or pastebin? – tleyden Dec 15 '17 at 00:22