Using Version-Based Consistency

Version-based consistency is used on a per-operation basis. It ensures that a read performed on a replica is at least as current as some previous write performed on the master.

An example of how this might be used is a web application that collects some information from a customer (such as her name). It then customizes all subsequent pages presented to the customer with her name. The storage of the customer's name is a write operation that can only be performed by the master node, while subsequent page creation is performed as a read-only operation that can occur at any node in the store.

Use of this consistency policy might require that version information be transferred between processes in your application.

To create a version-based consistency policy, use the Types.VersionConsistency() function. When you do this, you must provide the following information:

For example, the following code performs a store write, collects the version information, then uses it to construct a version-based consistency policy.

...
// Store handle configuration and open skipped for brevity
...

store.on('open', function () {
   console.log('Store opened');

    var row = {item: "Bolts",
              description: "Hex head, stainless",
              count: 5,
              percentage: 0.2173913};


   var matchVersion = null;
   store.put('myTable', row,
           function (err, putResult) {
                if (err)
                    throw err;
                else {
                    console.log("Row inserted.");
                    matchVersion =     
                        putResult.currentRowVersion;
                    store.close();
                }
           });
}).on('close', function() {
    console.log('Store closed.');
}).on('error', function(error) {
    console.log(error);
});
store.open(); 

At some other point in this application's code, or perhaps in another application entirely, we use the matchVersion captured above to create a version-based consistency policy.

...
// Store handle configuration and open skipped for brevity
...

store.on('open', function () {
   console.log('Store opened');

   var versionConsistency =  
        new Types.VersionConsistency(
                matchVersion,
                1000);

   var primaryKey = {item: "Bolts"};

   var readOptions = nosqldb.Types.ReadOptions (
                        versionConsistency,
                        1000);

   store.get('myTable', primaryKey, readOptions,
           function (err, result) {
                if (err)
                    throw err;
                else {
                    console.log("Row retrieved.");
                    console.log(result.currentRow);
                    store.close();
                }
           });

}).on('close', function() {
    console.log('Store closed.');
}).on('error', function(error) {
    console.log(error);
});
store.open();