Conditional updates are implemented only when specific conditions are met. Conditional updates can be used for put_row, update_row, delete_row, and batch_write_row operations.

Parameters

Judgment conditions for conditional updates include row existence conditions and column-based conditions.
  • Row existence conditions include:
    • IGNORE
    • EXPECT_EXIST
    • EXPECT_NOT_EXIST
    Note When you update a row, the system first checks the row existence conditions. If the row existence conditions are not met, the operation fails and an error is returned.
  • Column-based conditions are used to make judgments based on the values of one or more columns. These conditions are similar to the conditions used by the Table Store filters. Column-based conditions include:
    • SingleColumnCondition
    • CompositeColumnCondition

Conditional updates can be used to implement optimistic locking. When you update a row, the system first obtains the value of a column. Assume that Column A has a value of 1. Use a conditional update with the condition of Column A = 1 and attempt to update the row and to change the condition to Column A = 2. If the update fails, the row has been updated by another client.

Example

Update a row based on the specified primary key. The update succeeds if the following conditions are met. Otherwise, the update fails.
  • The specified row exists.
  • The values of the name column is john and the value of the addr column is china.
   var params = {
    tableName: "sampleTable",
    primaryKey: [{ 'gid': Long.fromNumber(20013) }, { 'uid': Long.fromNumber(20013) }],
    updateOfAttributeColumns: [{ 'PUT': [{ 'col1': 'test6' }] }]
};

//Specify the row to be updated. The value of the name column is john and the value of the addr column is china.
var condition = new TableStore.CompositeCondition(TableStore.LogicalOperator.AND);
condition.addSubCondition(new TableStore.SingleColumnCondition('name', 'john', TableStore.ComparatorType.EQUAL));
condition.addSubCondition(new TableStore.SingleColumnCondition('addr', 'china', TableStore.ComparatorType.EQUAL));

params.condition = new TableStore.Condition(TableStore.RowExistenceExpectation.EXPECT_EXIST, condition);

client.updateRow(params,
    function (err, data) {
        if (err) {
            console.log('error:', err);
            return;
        }
        console.log('success:', data);
    });