Serialising/deserialising CAS


I’m noticing that I have to use the exact cas object returned from a “get” operation as the cas option in a subsequent “set” operation. If I make a copy of the cas object e.g. cas = JSON.parse(JSON.stringify(cas)) then the “set” call fails with keyAlreadyExists.

So the question is, how to I manage to implement cas update conflict prevention when the object retrieved by “get” required serialisation before it is modified, and subsequently “set”?

(In my case this is because the document modification occurs on a remote client communicating with the server over http REST interface, which surely must be a very common use case?)



So I got round to making an example illustrating my issue. The two set methods (one commented out) I would expect to behave identically, but using “copiedCas” fails with [Error: Bad CAS] code: 4098 (or with keyAlreadyExists if you use the Mock server).

var should = require(“should”);
var couchbase = require(‘couchbase’);
var connection = new couchbase.Connection({ host: ‘localhost:8091’, bucket: ‘unit_tests’ });
//var connection = new couchbase.Mock.Connection();

describe(‘issue with copying cas’, function() {
it(‘should succeed’, function(done){
var testObj = { foo: “bar” };
connection.set(‘test’, testObj, function(err, result) { // Insert with no cas option i.e. overwrite any existing data
connection.get(‘test’, function(err, result) { // Retrieve the just added document
should.exist(result.value); = “bar2”;
var copiedCas = JSON.parse(JSON.stringify(result.cas));; // The two cas values are equal
connection.set(‘test’, result.value, { cas : copiedCas }, function(err, result) { // Doing it this way fails
// connection.set(‘test’, result.value, { cas : result.cas }, function(err, result) { // Doing it this way succedes


Hmm. Sorry about that formatting